Advertisement
Aniket_Goku

Evo of postfix expression

Sep 3rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1.  
  2. //evolution of postfix expression
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<MATH.h>
  6. #include<string.h>
  7. char postfix[50],tp[50];
  8. int stack[50],top= (-1),ans,a,b;
  9. void alert()
  10. {
  11.     printf("\nþûþ notice :==\n \"If you Enter NUMBER then Enter the space after the number is complete\" \n LIKe : \"3 33 +\"  ");
  12. }
  13. void push(int ch)
  14. {
  15.     top++;
  16.     stack[top]=ch;
  17. }
  18. int pop()
  19. {
  20.     int ch;
  21.     ch=stack[top];
  22.     top--;
  23.     return ch;
  24. }
  25. int main()
  26. {
  27.     int i,j,ans,a,b,amt,l;
  28.     char c[20];
  29.     clrscr();
  30.     alert();
  31.     printf("\n ENter the postfix Expression: ");
  32.     gets(postfix);
  33.  
  34.     for(i=0;i<strlen(postfix);i++)
  35.     {
  36.         j=0;
  37.         while(postfix[i]>=48 && postfix[i]<=57)
  38.         {
  39.             c[j]=postfix[i];
  40.             i++;
  41.             j++;
  42.  
  43.         }
  44.         c[j]=NULL;
  45.  
  46.         amt=atoi(c);
  47.  
  48.         if(amt!=0)
  49.         {
  50.  
  51.             push(amt);
  52.         }
  53.  
  54.         switch(postfix[i])
  55.         {
  56.             case ' ':
  57.                 printf("\n");
  58.                 break;
  59.  
  60.             case '^':
  61.                 a=stack[top];
  62.                 top--;
  63.                 b=stack[top];
  64.                 ans=pow(b,a);
  65.                 stack[top]=ans;
  66.                 break;
  67.  
  68.             case '*':
  69.                 a=stack[top];
  70.                 top--;
  71.                 b=stack[top];
  72.                 ans=b*a;
  73.                 stack[top]=ans;
  74.                 break;
  75.  
  76.             case '/':
  77.                 a=stack[top];
  78.                 top--;
  79.                 b=stack[top];
  80.                 ans=b/a;
  81.                 stack[top]=ans;
  82.                 break;
  83.  
  84.             case '+':
  85.                 a=stack[top];
  86.                 top--;
  87.                 b=stack[top];
  88.                 ans=b+a;
  89.                 stack[top]=ans;
  90.                 break;
  91.  
  92.             case '-':
  93.                 a=stack[top];
  94.                 top--;
  95.                 b=stack[top];
  96.                 ans=b-a;
  97.                 stack[top]=ans;
  98.                 break;
  99.  
  100.         }
  101.  
  102.     }
  103.     printf("\nEVO of POSTFIX expression ==> %d",stack[top]);
  104.     getch();
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement