INFIX TO POSTFIX

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include<ctype.h> using namespace std; #define Max 20 int priority(char ch); class InfixToPostfix { }; class Stack{ char stack[Max]; public: int top = 0; void push(char value) { if(top == Max-1) { cout<<"Stack Overflow"<<"\n"; return; } top++; stack[top] = value; } char pop() { if(top == -1) { cout<<"Stack Underflow"<<"\n"; return -1; } char value = stack[top]; top--; return value; } char peek() { return stack[top]; } }; int priority(char ch) { if(ch=='^') return 3; if(ch == '*' || ch =='%' || ch == '/' ) return 2; if(ch == '+' || ch == '-') return 1; return 0; } int main() { char infix[] = "A+B+C-D*E/F"; // for(int i=0;i<10;i++) // cout<<infix[i]; char postfix[20]; int p_index = 0; Stack src; for(int i=0;i<20;i++) { if(infix[i]=='(') { src.push(infix[i]); continue; } if(infix[i]==')') { char ch = src.pop(); while(ch != '(' && src.top!=-1) { postfix[p_index]= ch; p_index++; ch = src.pop(); } continue; } if(infix[i]=='+'||infix[i]=='-'||infix[i]=='*'||infix[i]=='/'||infix[i]=='%'||infix[i]=='^') { if(src.top!=-1) { char op = src.pop(); while(priority(op)>=priority(infix[i]) && src.top!=-1) { postfix[p_index]= op; p_index++; op = src.pop(); } src.push(op); src.push(infix[i]); } else { src.push(infix[i]); } continue; } if(isdigit(infix[i]) || isalpha(infix[i])){ postfix[p_index]= infix[i]; p_index++; continue; } } while(src.top!=-1) { char ch = src.pop(); postfix[p_index] = ch; p_index++; } for(int i=0;i<p_index;i++) cout<<postfix[i]; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines