Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int push(int tos, int x);
  4. int pop(int tos);
  5. int st[50];
  6. int tos=-1;
  7. int main()
  8. {
  9. char str[30];
  10. int r,k,v1,v2,i;
  11.  
  12. cin>>str;
  13. for(i=0;str[i]!='';i++)
  14. { if(str[i]!='*'&&str[i]!='-'&&str[i]!='+'&&str[i]!='/')
  15. {k=str[i]-'0';
  16. push(tos,k);
  17. }
  18. else
  19. {
  20. if(tos==-1||tos==0) cout<<"enter correct format";
  21. else {
  22. v1=pop(tos);
  23. v2=pop(tos);
  24. switch(str[i])
  25. { case '*': r=v1*v2;
  26. push(tos,r);
  27. break;
  28. case '+': r=v1+v2;
  29. push(tos,r);
  30. break;
  31. case '-': r=v1-v2;
  32. push(tos,r);
  33. break;
  34. case '/': r=v1/v2;
  35. push(tos,r);
  36. break;
  37. default: cout<<"invalid";
  38. }
  39. }
  40. } }
  41. r=pop(tos);
  42. cout<<endl<<r;
  43. return 0;
  44. }
  45. int push(int tos, int x)
  46. {
  47. if (tos==50-1)
  48. cout<<"overflow"<<endl;
  49. else {tos++;
  50. st[tos]=x;
  51. cout<<endl<<"pushed"<<tos<<st[tos];}
  52. }
  53.  
  54. int pop(int tos)
  55. { int z;
  56. if(tos==-1)
  57. cout<<"underflow";
  58. else {z=st[tos];
  59. tos-=1;}
  60. return z;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement