fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Node
  4. {
  5. public:
  6. char data;
  7. Node *next;
  8. };
  9. class stack
  10. { public:
  11. Node *top;
  12. stack()
  13. { top=NULL;
  14. }
  15. void push(char x);
  16. char pop();
  17. void display();
  18. };
  19. void stack::push(char x)
  20. {
  21. Node *t=new Node;
  22. t->data=x;
  23. top->next=top;
  24. top=t;
  25. }
  26. char stack::pop()
  27. { char x;
  28. Node *t=top;
  29. x=top->data;
  30. top=top->next;
  31. delete t;
  32. return x;
  33. }
  34.  
  35. int isOperand(char x)
  36. {
  37. if(x=='+'||x=='-'||x=='*'||x=='/')
  38. return 0;
  39. else return 1;
  40. }
  41. int pre(char x)
  42. {
  43. if(x=='+'||x=='-')
  44. return 1;
  45. else if(x=='*'||x=='/')
  46. return 2;
  47. else return 0;
  48. }
  49.  
  50. int main()
  51. { stack st;
  52. string infix="a+b*c+d";
  53. st.push('#');
  54. string postfix;
  55. int i=0, j=0;
  56. while(infix[i]!='\0')
  57. {
  58. if(isOperand(infix[i]))
  59. postfix[j++]=infix[i++];
  60. else
  61. {
  62. if(pre(infix[i])>pre(st.top->data))
  63. st.push(infix[i++]);
  64. else
  65. postfix[j++]=st.pop();
  66.  
  67. }
  68. }
  69. while(st.top)
  70. {
  71. postfix[j++]=st.pop();
  72. }
  73. postfix[j]='\0';
  74. cout<<postfix;
  75. }
Runtime error #stdin #stdout 0s 4596KB
stdin
Standard input is empty
stdout
Standard output is empty