this was what I did but it didn't work. Can you help me and study it to see what I'm missing?
Code:#include<stdio.h> #include<stdlib.h> #define MAXCOLS 80 main(){ char instring[MAXCOLS], postring[MAXCOLS]; int position = 0; float eval(); float result; printf("%s", "Enter postfix expression: "); while ((postring[position++] = getchar()) != '\n'); postring[--position] = '\0'; printf("%s%s", "postfix expression is", postring); getchar(); result=eval(postring); printf("%s%f\n", "value is", result); getchar(); } struct stack{ int top; float items[MAXCOLS]; }; float eval(expr) char expr[]; { int c, position; float op1, op2, value; float oper(), pop(); struct stack opndstk; opndstk.top = -1; for (position =0; (c=expr[position]) != '\0'; position++) if(c== ' '){ position++; } if(isdigit(c)){ double val, power,res; int sign; sign = (expr[position] == '-') ? -1 : 1; if (expr[position] == '+' || expr[position] == '-') position++; for (val = 0.0; isdigit(expr[position]); position++) val = 10.0 * val + (expr[position] - '0'); if (expr[position] == '.') { position++; } else { } for (power = 1.0; isdigit(expr[position]); position++) { val = 10.0 * val + (expr[position] - '0'); power *= 10; } res= sign * val / power; push(&opndstk, res); } else if ( c == '*' || c == '+' || c == '/' || c == '%' || c == '-' ) { op1 = pop ( ); op2 = Pop ( ); switch(c) { case '+' : value= op2+op1; break; case '-' : value= op2-op1; break; case '*' : value= op2*op1; break; case '/' : value= op2/op1; break; } push (&opndstk, value); } result = pop ( ); return result; }



LinkBack URL
About LinkBacks


