Advertisement
Aniket_Goku

evo_po

Nov 28th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. //evolution if postfix
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. #include<math.h>
  6. #define size 100
  7. int top=-1,ans;
  8. char postfix[size];
  9. char stack[size];
  10. void push(int ch)
  11. {
  12. top++;
  13. stack[top]=ch;
  14. }
  15. int pop()
  16. {
  17. int ch;
  18. ch=(int)stack[top];
  19. top--;
  20. return ch;
  21. }
  22. int main()
  23. {
  24. int i,j=0,a=0,b=0;
  25. printf("\n Enter the postfix expression: ");
  26. scanf("%s",postfix);
  27. for(i=0;i<strlen(postfix);i++)
  28. {
  29. switch(postfix[i])
  30. {
  31. case '^':
  32. a=pop();
  33. b=pop();
  34. ans=pow(b,a);
  35. push(ans);
  36. break;
  37. case '*':
  38. a=pop();
  39. b=pop();
  40. ans=b*a;
  41. push(ans);
  42. break;
  43. case '/':
  44. a=pop();
  45. b=pop();
  46. ans=b/a;
  47. push(ans);
  48. break;
  49. case '+':
  50. a=pop();
  51. b=pop();
  52. ans=b+a;
  53. push(ans);
  54. break;
  55. case '-':
  56. a=pop();
  57. b=pop();
  58. ans=b-a;
  59. push(ans);
  60. break;
  61. default:
  62. push(postfix[i]-'0');
  63. break;
  64. }
  65.  
  66. printf("\n %c \t %s \t %d \t %d \t %d",postfix[i],stack,a,b,ans);
  67. }
  68. ans=(int)pop();
  69. printf("\n ANs=> %d",ans);
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement