Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<iostream>
- //#include<stack>
- //#include<locale> //for function isalnum()
- //using namespace std;
- //
- //short privilege(char ch) {
- // if (ch == '+' || ch == '-') {
- // return 1;
- // }
- // if (ch == '*' || ch == '/') {
- // return 2;
- // }
- // if (ch == '^') {
- // return 3;
- // }
- // return 0;
- //}
- //
- //string inToPost(string infix) {
- // stack<char> stk;
- // //add some extra character to avoid underflow
- // string postfix = ""; //initially the postfix string is empty
- // string::iterator it;
- //
- // for (it = infix.begin(); it != infix.end(); it++) {
- // if (isalnum(char(*it)))
- // postfix += *it; //add to postfix when character is letter or number
- // else if (*it == '(')
- // stk.push('(');
- // else if (*it == '^')
- // stk.push('^');
- // else if (*it == ')') {
- // while (!stk.empty() && stk.top() != '(') {
- // postfix += stk.top(); //store and pop until ( has found
- // stk.pop();
- // }
- // stk.pop(); //remove the '(' from stack
- // }
- // else {
- // if (stk.empty()||privilege(*it) > privilege(stk.top()))
- // stk.push(*it); //push if precedence is high
- // else {
- // while (!stk.empty() && privilege(*it) <= privilege(stk.top())) {
- // postfix += stk.top(); //store and pop until higher precedence is found
- // stk.pop();
- // }
- // stk.push(*it);
- // }
- // }
- // }
- //
- // while (!stk.empty()) {
- // postfix += stk.top(); //store and pop until stack is not empty.
- // stk.pop();
- // }
- //
- // return postfix;
- //}
- //
- //int main() {
- // string infix = "A+B/C*(D-A)^F^H";
- // cout << "Postfix Form Is: " << inToPost(infix) << endl;
- //}
- #include<iostream>
- #include<stack>
- #include<locale> //for function isalnum()
- using namespace std;
- int preced(char ch) {
- if (ch == '+' || ch == '-') {
- return 1; //Precedence of + or - is 1
- }
- else if (ch == '*' || ch == '/') {
- return 2; //Precedence of * or / is 2
- }
- else if (ch == '^') {
- return 3; //Precedence of ^ is 3
- }
- else {
- return 0;
- }
- }
- string inToPost(string infix) {
- stack<char> stk;
- stk.push('#'); //add some extra character to avoid underflow
- string postfix = ""; //initially the postfix string is empty
- string::iterator it;
- for (it = infix.begin(); it != infix.end(); it++) {
- if (isalnum(char(*it)))
- postfix += *it; //add to postfix when character is letter or number
- else if (*it == '(')
- stk.push('(');
- else if (*it == '^')
- stk.push('^');
- else if (*it == ')') {
- while (stk.top() != '#' && stk.top() != '(') {
- postfix += stk.top(); //store and pop until ( has found
- stk.pop();
- }
- stk.pop(); //remove the '(' from stack
- }
- else {
- if (preced(*it) > preced(stk.top()))
- stk.push(*it); //push if precedence is high
- else {
- while (stk.top() != '#' && preced(*it) <= preced(stk.top())) {
- postfix += stk.top(); //store and pop until higher precedence is found
- stk.pop();
- }
- stk.push(*it);
- }
- }
- }
- while (stk.top() != '#') {
- postfix += stk.top(); //store and pop until stack is not empty.
- stk.pop();
- }
- return postfix;
- }
- int main() {
- string infix = "A+B/C*(D-A)^F^H";
- cout << "Postfix Form Is: " << inToPost(infix) << endl;
- }
- from pythonds.basic.stack import Stack
- def infixToPostfix(infixexpr) :
- prec = {}
- prec["^"] = 4
- prec["*"] = 3
- prec["/"] = 3
- prec["+"] = 2
- prec["-"] = 2
- prec["("] = 1
- opStack = Stack()
- postfixList = []
- tokenList = infixexpr.split()
- for token in tokenList :
- if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789" :
- postfixList.append(token)
- elif token == '(' :
- opStack.push(token)
- elif token == ')' :
- topToken = opStack.pop()
- while topToken != '(' :
- postfixList.append(topToken)
- topToken = opStack.pop()
- else:
- while (not opStack.isEmpty()) and \
- (prec[opStack.peek()] >= prec[token]) :
- postfixList.append(opStack.pop())
- opStack.push(token)
- while not opStack.isEmpty() :
- postfixList.append(opStack.pop())
- return " ".join(postfixList)
- print(infixToPostfix("A + B / C * ( D - A ) ^ F ^ H "))
- print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement