Page 76 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 76
Unit 3: Stacks
double eval (char exp[]) Notes
{
int c, position;
double opnd1, opnd2, value;
struct stack opndstk;
opndstk.top = -1;
for (position = 0; (c = expr[position) != ‘\0’; posit1on+ +)
if (isdigit(c))
push(&opndstk, (double) (c –‘0’));
else {
opnd2 = pop(&opndstk);
opnd1 = pop(&opndstk);
value = oper(c, opnd1, opnd2);
push(&opndstk, value);
}
return(pop(&pndstk));
}
int isdigit(char symb)
{
return (symb >= ‘0’ && symb <= ‘9’);
}
double oper(int symb, double op1, double op2)
{
switch(symb)
{
case ‘+’ : return (opl + op2);
case ‘-’ : return (opl + op2);
case ‘*’ : return (opl + op2);
case ‘/’ : return (opl + op2);
case ‘^’ : return (opl + op2);
default : printf(“%s”, “illegal operation”);
}
}
double pop(struct stack *s)
{
int x;
if(s.top == -1)
return(0);
else
{
LOVELY PROFESSIONAL UNIVERSITY 71