I have another problem with stack. I want to evaluate a postfix expression and display the result. Below is my code but contain some errors. Anyone can help me?
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
#include "StackInterface.h"
char *PostfixString;
void InterpretPostfix (void)
{
float LeftOperand, RightOperand, Result;
int i;
char c;
char *s="x";
Stack EvalStack;
InitializeStack(&EvalStack);
printf("Enter Postfix expression-> ");
scanf("%s", PostfixString);
for(i=0; i<strlen(PostfixString); ++i)
{
s[0]=c=PostfixString[i];
if(isdigit(c))
Push((float)atof(s), &EvalStack);
else if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^')
{
Pop(&EvalStack, &RightOperand);
Pop(&EvalStack, &LeftOperand);
switch (c)
{
case '+':
Push(LeftOperand + RightOperand, &EvalStack);
break;
case '-':
Push(LeftOperand - RightOperand, &EvalStack);
break;
case '*':
Push(LeftOperand * RightOperand, &EvalStack);
break;
case '/':
Push(LeftOperand / RightOperand, &EvalStack);
break;
case '^':
Push(exp(log(LeftOperand)*RightOperand), &EvalStack);
break;
default:
break;
}
}
}
Pop(&EvalStack, &Result);
printf("Value of postfix expression = %f\n", Result);
}