Write and test a C program that implements a stack based integer-based calculato
ID: 3753185 • Letter: W
Question
Write and test a C program that implements a stack based integer-based calculator. A sample run is included. The program accepts input until q is entered. Inputs control the calculator as follows
$ ./a.out
: w
Error: unrecognized command
: 8
: 10
: 4
: 2
: 9
: 8
: 7
Error: stack overflow
: +
: d
17
2
4
10
8
: p
: d
2
4
10
8
: -
: d
2
10
8
: *
: d
20
8
:6
: d
6
20
8
: /
: d
3
8
: ^
: d
512
: +
Error: not enough operands for operation
: 4
: 4
: -
: d
0
512
: /
Error: attempt to divide by 0
: =
0
: c
: d
: p
Error: stack empty
: q
Goodbye
Write and test a C program that implements a stack based integer-based calculator. The program accepts input until q is entered Inputs control the calculator as follows Action Input Non-zerò integer Pushed on the stack Pop the top stack itenm Quit the program Clear the stack Display the stack top down Print the top stack item Replace the top 2 stack items by their sum Replace the top 2 stack items by their product Replace the top 2 stack items by their difference Replace the top 2 stack items by their quotient AReplace the top 2 stack items by second raised to the power top Your program should check for error conditions - see example program run. You can make the maximum stack size small in order to test for errors.Explanation / Answer
#include <stdio.h>
#include <math.h>
int stack[100];
int pointer=0;
void pop();
void clear();
void display();
void print();
void add();
void multiply();
void subtract();
void division();
void power();
void pop()
{
if(pointer==0)
printf("Stack is empty, nothing to pop ");
else
pointer--;
}
void clear()
{
pointer=0;
}
void display()
{
int i;
if(pointer==0)
printf("Error: Stack is empty, nothing to display ");
else
{
for(i=0;i<pointer;i++)
printf("%d ",stack[i] );
}
}
void print()
{
printf("%d ",stack[pointer-1] );
}
void add()
{
if(pointer<2)
printf("Error: Nor enough numbers present in the stack to perform this operation ");
else
{
int ans=stack[pointer-1]+stack[pointer-2];
stack[pointer-2]=ans;
pointer--;
}
}
void multiply()
{
int ans=stack[pointer-1]*stack[pointer-2];
stack[pointer-2]=ans;
pointer--;
}
void subtract()
{
if(pointer<2)
printf("Error: Nor enough numbers present in the stack to perform this operation ");
else
{
int ans=abs(stack[pointer-1]-stack[pointer-2]);
stack[pointer-2]=ans;
pointer--;
}
}
void division()
{
if(pointer<2)
printf("Error: Nor enough numbers present in the stack to perform this operation ");
else
{
if(stack[pointer-1]==0)
printf("Error: Attept to divide by 0 ");
else
{
int ans=stack[pointer-2]/stack[pointer-1];
stack[pointer-2]=ans;
pointer--;
}
}
}
void power()
{
int ans=pow(stack[pointer-2],stack[pointer-1]);
stack[pointer-2]=ans;
pointer--;
}
int main()
{
char temp;
while(1)
{
scanf("%c",&temp);
if(((int)temp>=48)&&((int)temp<=57))
{
if(pointer<100)
{
stack[pointer]=(int)temp-48;
pointer++;
}
else
printf("Error: stack overflow ");
}
else if(temp=='p')
pop();
else if(temp=='c')
clear();
else if(temp=='d')
display();
else if(temp=='=')
print();
else if(temp=='+')
add();
else if(temp=='*')
multiply();
else if(temp=='-')
subtract();
else if(temp=='/')
division();
else if(temp=='^')
power();
else if(temp=='q')
break;
}
printf("Goodbye! ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.