#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int array[30];
int front = -1;
void add(int val)
{
array[++front] = val;
}
int remove()
{
return array[front--];
}
int main()
{
char expression[30];
char *h;
int k1,k2,k3,number;
printf("Enter a string to evaluate: ");
scanf("%s",expression);
h= expression;
while(*h != '\0')
{
if(isdigit(*h))
{
number = *h - 48;
add(number);
}
else
{
k1 = remove();
k2 = remove();
switch(*h)
{
case '+':
{
k3 = k1 + k2;
break;
}
case '-':
{
k3 = k2 - k1;
break;
}
case '*':
{
k3 = k1 * k2;
break;
}
case '/':
{
k3 = k2 / k1;
break;
}
}
add(k3);
}
h++;
}
printf("\nAnswer is %s = %d\n\n",expression,remove());
return 0;
}
Comments
Leave a comment