Advertisement
ruhul0

Postfix to Evaluation

Aug 25th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <math.h>
  4. int stack[100];
  5. int top=-1;
  6.  
  7. void push(int element)
  8. {                      
  9.     stack[++top]=element;
  10. }
  11.  
  12. int pop()
  13. {                    
  14.     return(stack[top--]);
  15. }
  16.  
  17. int main()
  18. {                        
  19.     char postfix[100],ch,j;
  20.     int i=0,a,b;
  21.     printf("Expression:\n");
  22.     scanf("%s",postfix);
  23.     while( (ch=postfix[i++]) != '\0')
  24.     {
  25.         if(isdigit(ch))
  26.             push(ch-'0');
  27.         else
  28.         {      
  29.             b=pop();
  30.             a=pop();
  31.             if(ch=='+')
  32.                 push(a+b);
  33.             if(ch=='-')
  34.                 push(a-b);
  35.             if(ch=='*')
  36.                 push(a*b);
  37.             if(ch=='/')
  38.                 push(a/b);
  39.             if(ch=='^')
  40.                 push(pow((float)a,b));
  41.  
  42.         }
  43.     }
  44.     printf("\nPostfix: %s\n",postfix);
  45.     printf("\nEvaluation: %d\n",stack[top]);
  46.     getchar();
  47.     getchar();
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement