C++ reverse Polish expression evaluation-convert postfix expressions into infix expressions for calculation

Inverse Polish expression, also called postfix expression. As shown in the figure:
Insert picture description here

The operation expressions we usually see are infix expressions, that is, the sequence of "operand ① operator ② operand ③", and the operator is in the middle of the two operands.
But the postfix expression is the sequence of "operand ① operand ③ operator ②", and the operator is after the two operands.

There is no essential difference between various expressions. They are actually the same syntax tree, but different formulas obtained by different traversal methods; they are a whole multi-faceted thing, just viewed from different angles.

The infix expression is the middle-order traversal
of its corresponding syntax tree ; the postfix expression is the post-order traversal
of its corresponding syntax tree ; the prefix expression is the pre-order traversal of its corresponding syntax tree; the
left side of the figure below is the infix Expression, the middle is its corresponding syntax tree, and the right is the suffix expression converted from the syntax tree.
Insert picture description here
The process of evaluating inverse Polish expressions is:

If you encounter a number, push it to the stack;
if you encounter an operator, pop two numbers from the top of the stack, namely shuzi1 (top of the stack) and shuzi2 (the second element in the stack); calculate shuzi2 and operate shuzi1.
Inverse Polish expression Yes, the code implementation is very convenient, and it can be solved with a stack.

#include <iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
class Solution {
    
    
public:
    int evalRPN(vector<string>& tokens) {
    
    
        stack<int> digit;
        for (int i = 0; i < tokens.size(); i++) {
    
    
            string& token = tokens[i];
            if (isNumber(token)) {
    
    //isNumber函数在C++中并没有定义,所以我们需要单独定义一下
                digit.push(stoi(token));//将string转换为int类型进行运算,还有一种方法就是讲string先用c_str转换为char类型后用atoi在转换为int类型
                continue;
            }
            else {
    
    
                int shuzi1 = digit.top();
                digit.pop();
                int shuzi2 = digit.top();
                digit.pop();
                int nums;
                const char* r = tokens[i].c_str();//+,-,*,/均是char类型,而tokens中是string类型所以用C_str进行转换
                switch (*r) {
    
    
                case '+': nums = shuzi1 + shuzi2; break;
                case '-': nums = shuzi2 - shuzi1; break;
                case '*': nums = shuzi1 * shuzi2; break;
                case '/': nums = shuzi2 / shuzi1; break;
                }
                digit.push(nums);
            }
        }
        return digit.top();
    }
    bool isNumber(string& token) {
    
    
        return !(token == "+" || token == "-" || token == "*" || token == "/");
    }
};
int main()
{
    
    
    vector<string> tken = {
    
     "2","1","+","3","*" };
    Solution* x = new Solution;
    cout << x->evalRPN(tken);  
}

Guess you like

Origin blog.csdn.net/qq_41884662/article/details/115028171