Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. /* This C program is designed to evaluate a postfix expression*/
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <ctype.h>
  7.  
  8. #define SIZE 15 //initializing a common size for all arrays
  9.  
  10. int stack[SIZE];
  11. char user[SIZE];
  12. int top=0;
  13.  
  14. int pop()
  15. {
  16.     return stack[--top];//This sentence returns a value back to where the function is called
  17. }
  18.  
  19. void push(int n)
  20. {
  21.     stack[top++]=n;//Enters an element into the stack
  22. }
  23.  
  24. int main()
  25. {
  26.     start:int i=0,cn=0,pop1=0,pop2=0;
  27.     char input;
  28.     top=0;
  29.     printf("Please enter a valid postfix expression, each token separated by a space:-\n");
  30.     gets(user);//getting the string from the user
  31.    
  32.     while(user[i]!='\0')//Reading the string token wise
  33.     {
  34.         int result=0,l=0;
  35.         input=user[i];
  36.         if((input>=48)&&(input<=57))//checking for an number (ASCII 48 to 57)
  37.         {
  38.             for(int k=0;user[i+k]!=' ';k++)//calculating decimal value of number
  39.             {
  40.                 result=(result*10)+user[i+k]-48;
  41.                 l++;
  42.             }
  43.             i+=l;
  44.             push(result);
  45.         }
  46.        
  47.         else if ((top!=0)&&((top%2==0)||((top-cn)%2==0))&&((input=='+')||(input=='-')||(input=='*')||(input=='/')||(input=='$')||(input=='^')))
  48.         {
  49.             pop1=pop();
  50.             pop2=pop();
  51.             switch(input)
  52.             {
  53.             case '+'://Adding
  54.                 result=pop2+pop1;
  55.                 break;
  56.             case '-'://subtracting
  57.                 result=pop2-pop1;
  58.                 break;
  59.             case '*'://multiplying
  60.                 result=pop2*pop1;
  61.                 break;
  62.             case '/':
  63.                 if(pop1!=0)//dividing
  64.                 result=pop2/pop1;
  65.                 else
  66.                 printf("Can't divide by zero");
  67.                 break;
  68.                
  69.             }
  70.            
  71.            push(result);
  72.            cn++;//calculating position of computed result in stack
  73.     }
  74.     else if(input==' ');
  75.     else
  76.     {
  77.     printf("Invalid expression\n");
  78.     goto start;
  79.     }
  80.             i=i+1;//incrementing i to traverse the input array
  81. }
  82.     if(top==1)//print result only if a single result exists
  83.     printf("Result=%d",pop());
  84.     else
  85.     {
  86.         printf("Invalid expression\n");
  87.         goto start;
  88.     }
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement