fork download
  1. #include<iostream>
  2. using namespace std;
  3. struct Stack
  4. {
  5. char data;
  6. Stack *next;
  7. };
  8. Stack *top=NULL;
  9. void push(int item)
  10. {
  11. Stack *New=new Stack;
  12. if(New==NULL)
  13. {
  14. cout<<"Overflow"<<endl;
  15. exit(1);
  16. }
  17. New->data=item;
  18. New->next=NULL;
  19. New->next=top;
  20. top=New;
  21. }
  22. void pop()
  23. {
  24. if(top==NULL)
  25. {
  26. cout<<"Underflow"<<endl;
  27. exit(1);
  28. }
  29. top=top->next;
  30. }
  31. int main()
  32. {
  33. string infix;
  34. getline(cin,infix);
  35. infix="(" +infix+")";
  36. string exp;
  37. for(int i=0;i<infix.size();i++)
  38. {
  39. if(infix[i]>='A' && infix[i]<='Z')
  40. {
  41. exp.push_back(infix[i]);
  42. }
  43. else if(infix[i]=='(')
  44. {
  45. push(infix[i]);
  46. }
  47. else if(infix[i]==')')
  48. {
  49. while(top->data != '(')
  50. {
  51. exp.push_back(top->data);
  52. pop();
  53. }
  54. pop();
  55. }
  56. else
  57. {
  58. while(true)
  59. {
  60. if((infix[i]=='^' || infix[i]=='*' || infix[i]=='/') &&(top->data=='+' || top->data=='-'))
  61. {
  62. push(infix[i]);
  63. break;
  64. }
  65. else if(infix[i]=='^' && (top->data=='*' || top->data=='/'))
  66. {
  67. push(infix[i]);
  68. break;
  69. }
  70. else if(top->data=='(')
  71. {
  72. push(infix[i]);
  73. break;
  74. }
  75. else
  76. {
  77. exp.push_back(top->data);
  78. pop();
  79. }
  80. }
  81. }
  82. }
  83. cout<<exp<<endl;
  84. return 0;
  85. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout