#include<iostream> #include<stack> #include<string> #include<vector> using namespace std; int Priority(char symbol) { // to found priority of the symboles if (symbol == '(') return 3; else if (symbol == '/' || symbol == '*') return 2; else if (symbol == '+' || symbol == '-') return 1; } bool IsOperation(char symbol) // return true if symbole ,else return false {like numbers} { return (symbol == '(' || symbol == '/' || symbol == '*' || symbol == '+' || symbol == '-'); } bool IsNumber(char symble) // return true if numbers , else return false {like operation + - ...} { return (symble == '0' || symble == '1' || symble == '2' || symble == '3' || symble == '4' || symble == '5' || symble == '6' || symble == '7' || symble == '8' || symble == '9'); } void main() // start Function main { stack<char>postfix; stack<char>OPERATION; string x; cout << "Enter your process without spaces to convert infix to postfix : "; cin >> x; // enter x for (int i = 0; i < x.size(); i++) // To Check on every index in x string { if (x[i] == ')') // This's case if the symbole { ) } it means the end of the brackets { (..) } { // and this will insert all element in operation stack while (OPERATION.top() != '('&& !OPERATION.empty()) //to postfix stack and remove all element into { ( } in operation stack { // then remove the symbole{ ( } postfix.push(OPERATION.top()); OPERATION.pop(); } OPERATION.pop(); } else if (IsNumber(x[i])) // if symoble is Number enter to stack " postfix " { if (IsOperation(x[i+1])) // To check if i need to put space or not { postfix.push(x[i]); postfix.push(' '); } else { postfix.push(x[i]); } } else // else if symoble is operation if (IsOperation(x[i])) { if (OPERATION.empty()) { OPERATION.push(x[i]); } else if (Priority(x[i]) > Priority(OPERATION.top()) || OPERATION.top() == '(') // insert in stack operation if element of index is operation or // OPERATION.top()=='(' { OPERATION.push(x[i]); } else { while ((!OPERATION.empty())&& Priority(OPERATION.top()) >= Priority(x[i]) ) // Repetition the body of while into // stack operation become empty and Priority OPERATION.top() { if (OPERATION.top() == '(') { break; } // and Priority(OPERATION.top()) >= Priority(x[i]) postfix.push(OPERATION.top()); // then insert to operation stack x[i] OPERATION.pop(); } OPERATION.push(x[i]); } } else // This's case if the symbole is not operation and number // in fact the enter is error { cout << "Go out" << endl; break; } } // finish for loop while (!OPERATION.empty()) // insert all element in operation stack to postfix stack and remove all element in operation stack { // because we have finished converting postfix.push(OPERATION.top()); OPERATION.pop(); } vector<char> q; while (!postfix.empty()) { q.push_back(postfix.top()); postfix.pop(); } cout << endl; cout << "Before the conversion process : " << x << endl; cout << "After the conversion process : "; for (int i = q.size() - 1; i >= 0; --i) { cout << q[i] ; } cout << endl; system("pause"); }
Priority
( / * + -
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)