C + + implementation Converts an expression to a reversed-Polish expression

Source: Internet
Author: User

Https://github.com/Lanying0/lintcode

Belongs:

Data structure--linear structure--stack

Problem:

Given an array of expression strings, returns the inverse Polish expression of the expression (that is, the parentheses are removed).

Sample Example
for [3-4 + 5] expression (the expression can be expressed as ["3", "-", "4", "+", "5"]), return [3 4-5 +] (the expression can be expressed as ["3", "4", "-", "5", "+"]).

Ideas:

Contrary to the general idea of Poland, for the input string, build two stacks, one stack to hold the operation symbol, and the other stack to save the expression results.

Scan the string from the beginning to the end: When an operand is encountered, it is pressed directly into the expression result stack, and the operator is encountered according to the operator precedence.

Operator conditions: 1. Left parenthesis: Press directly into the symbol stack.

2. Plus, minus, the lowest priority, so to the stack of subtraction number first out of the stack to the expression result stack, and then add minus sign into the stack.

3. Multiplication sign, division sign, the highest priority, so just the stack of the multiplication in the stack to the expression results stack, and then the multiplication number into the stack.

4. Closing parenthesis: Stack all the operators in the stack after the left parenthesis into the stack of expression results stack, the left parenthesis out of the stack.

After a scan, if the symbol stack is not empty, it is all stacked to the expression result stack. That is the request.

Code:

Class Solution {public:/** * @param expression:a string array * @return: The Reverse Polish notation of this Expression */vector<string> Converttorpn (vector<string> &expression) {//write your code H ere vector<string>op;//symbol stack vector<string>num;//expression result stack for (int i=0;i<expression.size (); i++)//scan again {if (expression[i]== "+" | | expression[i]== "-")//handle Plus, minus {if (op.siz                E () ==0) Op.push_back (Expression[i]); else {while (op.size ()!=0 && (op[op.size () -1]== "*" | | op[op.size () -1]== "/" | | Op[op.size () -1]== "+" | |                    Op[op.size () -1]== "-")) {string S=op.back ();                    Op.pop_back ();                                   Num.push_back (s);                } op.push_back (Expression[i]);              } if (Op[op.size () -1]== "(") {      Op.push_back (Expression[i]); }} else if (expression[i]== "*" | | expression[i]== "/")//processing multiplication sign, Division sign {if (op.si                Ze () ==0) Op.push_back (Expression[i]);                    else if (op[op.size () -1]== "*" | | op[op.size () -1]== "/") {string s=op.back ();                    Op.pop_back ();                    Num.push_back (s);                Op.push_back (Expression[i]); } else if (Op[op.size () -1]== "+" | | op[op.size () -1]== "-") {Op.push_back (E                Xpression[i]);                } else if (Op[op.size () -1]== "(") {Op.push_back (expression[i]); }} else if (expression[i]== "(")///handle opening parenthesis {op.push_back (Expressio            N[i]); } else if (expression[i]== ")")//handle closing parenthesis {while (Op.back ()! = "(")               {string S=op.back ();                    Op.pop_back ();                 Num.push_back (s);            } op.pop_back ();            The else//operand is pressed directly into the expression result stack {Num.push_back (expression[i]);            }} while (Op.size ()!=0)//symbol stack is still signed, press it into the expression result stack {string s=op.back ();            Op.pop_back ();        Num.push_back (s);    } return num; }};

  

C + + implementation Converts an expression to a reversed-Polish expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.