/************************************ * Data Structures & Algotihms * * Project 1 * * Levent Kaya * * 1800003183 * ************************************/ #include #include #include "myStack.h" #include "stackADT.h" using namespace std; class infixToPostfix{ public: void convertToPostfix(); bool precedence(char opr1, char opr2); void getInfix(string); void showInfix(); void showPostfix(); infixToPostfix(string); string getPfx(); int evaluatePostfix(char* exp); private: string infx; string postfx; }; bool infixToPostfix::precedence(char opr1, char opr2){ char c1 = opr1, c2 = opr2; int flag1 = 0, flag2 = 0; if(c1 == '(' || c1 == ')'){ flag1 = 0; } else if(c2 == '(' || c2 == ')'){ flag2 = 0; } else if(c1 == '+' || c1 == '-'){ flag1 = 1; } else if(c1 == '*' || c1 == '/'){ flag1 = 2; } else if(c2 == '+' || c2 == '-'){ flag2 = 1; } else if(c2 == '*' || c2 == '/'){ flag2 = 2; } if(flag1 >= flag2){ return true; } else return false; } string infixToPostfix::getPfx(){ return postfx; } void infixToPostfix::showPostfix(){ cout << getPfx() << endl; } void infixToPostfix::getInfix(string inputInfix){ infx = inputInfix; } void infixToPostfix::showInfix(){ cout << infx << endl; } int checkSymOperator(char symbol){ if(symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'){ return 1; } else { return 0; } } int checkSymOperand(char symbol){ if(isalpha(symbol) || isdigit(symbol)){ return 1; } else{ return 0; } } void infixToPostfix::convertToPostfix(){ stackType stack; postfx = ""; infx.append(")"); stack.push('('); for (int i = 0; i < infx.length(); i++){ if(checkSymOperator(infx[i])){ if(checkSymOperator(stack.top())){ if(precedence(infx[i], stack.top()) == 0){ postfx = postfx + stack.top(); stack.pop(); } else if(precedence(infx[i], stack.top()) == 1){ stack.push(infx[i]); } else{ postfx = postfx + stack.top(); stack.pop(); } } else{ stack.push(infx[i]); } } else{ if(infx[i] = ')'){ while (stack.top() != '('){ postfx = postfx + stack.top(); stack.pop(); } stack.pop(); } else{ postfx = postfx + infx[i]; } } } } infixToPostfix::infixToPostfix(string pref){ pref = ""; } int main(){ string input; infixToPostfix inf2post; cout << "Infix Expression: "; cin >> input; inf2post.getInfix(input); inf2post.convertToPostfix(); cout << "Postfix Expression: "; inf2post.showPostfix(); cout << endl; return 0; }