fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int precedence(char);
  5. struct node{
  6. char i;
  7. node *next;
  8. }*ptr,*newptr,*a,*top;
  9. class stack{
  10. node *create_node(char data){
  11. ptr=new node;
  12. ptr->i=data;
  13. ptr->next=NULL;
  14. return ptr;
  15. }
  16. public:
  17. void push(char d);
  18. void pop();
  19. bool isempty();
  20. void display(node *np){
  21. while(np!=NULL){
  22. cout<<np->i;
  23. np=np->next;
  24. }
  25. cout<<"\n";
  26. }
  27. };
  28. void stack::push(char d){
  29. newptr=create_node(d);
  30. if(isempty()){top=newptr;}
  31. else{
  32. newptr->next=top;
  33. top=newptr;
  34. }
  35.  
  36. }
  37. void stack::pop(){
  38. if(isempty()){cout<<"underflow";}
  39. else{
  40. a=top;
  41. top=top->next;
  42. delete a;
  43. }
  44. }
  45. bool stack::isempty(){
  46. if(top==NULL){return true;}
  47. else return false;
  48. }
  49. int main(){
  50. int size,em=0;
  51. stack s1;
  52. cin>>size;
  53. char vop,infix[size],postfix[size];
  54. for(int E=0;E<size;E++){
  55. cin>>vop;
  56. infix[E]=vop;
  57. for(int e=0;e<size;e++){
  58. if((infix[e]>='a'&&infix[e]<='z')||(infix[e]>='A'&&infix[e]<='Z')){
  59. postfix[em]=infix[e];
  60. em++;
  61. }
  62. else if(infix[e]=='('){
  63. s1.push(infix[e]);
  64. }
  65. else if(infix[e]==')'){
  66. while(!s1.isempty()&&top->i!='('){
  67. postfix[em]=top->i;
  68. em++;
  69. s1.pop();
  70. }
  71. s1.pop();
  72. }
  73. else
  74. {
  75. while(!s1.isempty()&&top->i!='('&&precedence(top->i)>=precedence(infix[e]))
  76. {
  77. postfix[em]=top->i;
  78. em++;
  79. s1.pop();
  80. }
  81. s1.push(infix[e]);
  82. }
  83. }
  84. while(!s1.isempty()){
  85. postfix[em]=top->i;
  86. s1.pop();
  87. em++;
  88. }
  89. for(int ele=0;ele<size;ele++){
  90. cout<<postfix[ele];
  91. }
  92. return 0;
  93. }
  94. int precedence(char c){
  95. switch(c){
  96.  
  97. case '^': return 5;
  98. break;
  99. case '/':return 4;
  100. break;
  101. case '*':return 4;
  102. break;
  103.  
  104. case '-':return 3;
  105. break;
  106. case '+':return 3;
  107. break;
  108.  
  109. }
  110. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:94:27: error: a function-definition is not allowed here before ‘{’ token
     int precedence(char c){
                           ^
prog.cpp:110:1: error: expected ‘}’ at end of input
 }
 ^
stdout
Standard output is empty