Guest User

Untitled

a guest
Jan 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 100
  5. int num[MAX],tos=-1;
  6.  
  7. push(int x)
  8. {
  9. if(tos==MAX)
  10. {
  11. printf("the stack is full");
  12. }
  13. else
  14. {
  15. printf(" l");
  16. tos++;
  17. num[tos]=x;
  18. }
  19. }
  20. int pop()
  21. {
  22. if(tos<0)
  23. {
  24. printf("stack underflow");
  25. }
  26. else
  27. return num[tos--];
  28. }
  29. int main()
  30. {
  31. char postfix[MAX],exp[MAX],ch,val;
  32. int a,b;
  33. printf("enter the postfix expression");
  34. fgets(postfix,MAX,stdin);
  35. strcpy(exp,postfix);
  36. for(int i=0;i<strlen(postfix);i++)
  37. {
  38. printf(" xox ");
  39. ch=postfix[i];
  40. if(isdigit(ch))
  41. {
  42. push(ch - '0');
  43. printf(" %d ",atoi(ch));
  44. }
  45. else
  46. {
  47. printf("%d",tos);
  48. a=pop();
  49. b=pop();
  50. switch(ch)
  51. {
  52. case '+':
  53. val=a+b;
  54. break;
  55. case '-':
  56. val=a-b;
  57. break;
  58. case '*':
  59. val=a*b;
  60. break;
  61. case '/':
  62. val=a/b;
  63. break;
  64. }
  65. printf("%d",val);
  66. push(val);
  67. }
  68. }
  69. printf("the result of the expression %s = %d",exp,num[0]);
  70. return 0;
  71. }
Add Comment
Please, Sign In to add comment