Summary About the suffix / infix / prefix expression

Consider what is happening now is a number 9.23

Why do I have recently lost a little good things have gods can simulate race performance so good before this year dozens of fairy thing

I'm so grateful to my physics teacher told me directly back to class in the engine room rather not come back to class tired, very grateful he was very enlightened told me that the worst is not that race did not do a good job and then come back to engage in cultural studies Well, when the teacher told you let me talk focused on the physics of slag was moved qwq I will try

Well, the above is nonsense;

This expression is evaluated is my good morning before school, but yesterday I was listening to this moment has forgotten how to write things looked in the code to think of it not as good as written down

Why prefixes and suffixes infix form will be because we evaluate the expression when in fact can be seen as a prelude to the traversal sequence after sequence order in the tree is a tree and then insert the corresponding 

So different traversal sequence expression evaluates way we will be here to talk about the calculation expression of prefixes and suffixes and how to convert infix to postfix in calculations different this is a general practice

Should all know the expression evaluates to be engaged in a stack to mention I do not believe you do not know; 

It introduces the concept infix expressions (infix notation) we most often written infix formula is a general representation of arithmetic or logical formula, the operator in the middle operand in infix form.

While the human brain is easy to understand and analyze the infix expression, but the computer is an infix expression it is very complex, and therefore the calculated value of the expression, usually need to convert to a prefix or suffix first infix expression expression,

Then the evaluated value. Computer, computing prefix or suffix value of the expression is very simple.

Prefix expression (prefix notation, Polish) operator prefix expression precedes the operand.

Prefix expression of a computer evaluation:

Expression from right to left scanning, while a numeral, the number onto the stack, the face of the operator, the pop-up stack of two numbers, using operators calculated accordingly (the top element of the op-top element thereof ), and the result stack;

Repeat the process directly to the leftmost expression, is the value obtained by the last calculation result of the expression.

E.g. prefix expression "- × + 3 4 5 6":

(1) right to left scan, 6543 onto the stack;

(2) meet the + operator, so pop 3 and 4 (note stack 34 is applied at this time the stack), the calculated value of 4 + 3, 7 too, and then the stack 7;

(3) Next is × operator, and thus the pop-up 7 5, 7 × 5 = calculated 35, the stack 35;

(4) Finally - operator, the calculated value of 35-6, i.e. 29, it follows that the final result.

Then we consider the calculation postfix expression

With a slightly different order from left to right above us encounter this expression is convenient to push digital sign will be encountered is at the top two numbers from the stack operation result of the operation carried out until the final push to get results

Postfix expression is also called reverse Polish notation this equation is characterized by the absence of parentheses so we can be a direct analog, but we are here to discuss the calculation infix expression

That is, we write the equation but most computationally we have two methods to convert it into an expression prefix or suffix transformed into the expression evaluates to say here about the way in accordance with the above expressions into a suffix

Let me talk about how the conversion here we assume that only a small parenthesis only integer input legitimate example:

Infix expression is: 1 + (2-3) * 4 + 4/2

Corresponding to the postfix expression is: 123--4 42 * + / +

How would one infix expressions into postfix notation? We need the help of the stack, use it to store operator.

First, the various operators (including brackets) priority ranking as follows (the larger the number, the higher the priority):

1: (Note that the parentheses is the lowest priority qwq

2:+ -

3:* /

4 :^

5:)

Here only take into account the power of the form 

To infix expression entered traverse from left to right:

1) If you have digital, direct to the end of the postfix expression;

2) If you experience operators +, -, *, /:

First determine whether the stack is empty. If so, then this operator directly onto the stack. If not, check the current top of the stack. If the priority of the top element is greater than or equal to this operator level, pop the top element, the top element is added to the postfix expression, and continue the determination. If the determination is not satisfied, or the stack is empty, the operator will push. It is noted that, after the above steps, the operator will eventually stack .

3)如果遇到括号:如果是左括号直接入栈。如果是右括号弹出栈中第一个左括号前所有的操作符,并将左括号弹出

4)字符串遍历结束后,如果栈不为空,则弹出栈中所有元素,将它们添加到后缀表达式的末尾,直到栈为空。

通过上述过程 我们就得到了 这个中缀表达式 对应的后缀表达式 那么 我们怎么计算后缀表达式也清楚了 但是这种发现有点 麻烦 我们还要记录这个后缀表达式是什么 

对于一些题目 不需要你输出 后缀表达式 那我们考虑直接计算 不记录这个后缀表达式 直接将两步结合起来;

我们考虑开两个栈 一个数字栈 一个符号栈 我们读入当前的中缀表达式 遇到数字 我们将其压入数字栈 遇到符号 按照刚才的方式处理

对符号栈进行处理 对应优先级的顺序 也就是说 对于一个操作符  我们将优先级大于等于当前符号的 都计算出来 方法类似于后缀表达式 一个符号 两个数字 计算结果压入栈中;

所以我们无需计算出具体的后缀表达式 直接计算即可;

鉴于写文章的作者比较懒 所以以上都没有代码 不过luogu上倒是有后缀表达式 以及简单的中缀表达式计算的例题 如1449 但为啥都是普及-啊

所以 只有计算中缀的一个题目 涉及 负数 多位数字的处理 以及加 减 乘 整除 乘方 括号 多种运算

给出一个表达式,其中运算符仅包含+,-,*,/,^(加 减 乘 整除 乘方)要求求出表达式的最终值。

数据可能会出现括号情况,还有可能出现多余括号情况。

数据保证不会出现大于或等于231的答案。

数据可能会出现负数情况。

输入格式

输入仅一行,即为表达式。

输出格式

输出仅一行,既为表达式算出的结果。

输入样例:

(2+2)^(1+1)

输出样例:

16

我们按照上面的思路来一遍就好了 注意好好处理数字 

#include<bits/stdc++.h>
using namespace std;
stack<int> num;
stack<char> ops;

inline int mul(int a,int k) {//慢速幂???
    int res=1;
    while(k--) res*=a;
    return res;
}

void cal() {//计算表达式的值 按照后缀表达式的方式
    int num1,num2,num3;
    num1=num.top(); num.pop();
    num2=num.top(); num.pop();
    char op=ops.top(); ops.pop(); 
    if(op=='+') num3=num2+num1;
    else if (op=='-') num3=num2-num1;
    else if (op=='*') num3=num2*num1;
    else if (op=='/') num3=num2/num1;
    else num3=mul(num2,num1);
    num.push(num3);//计算结果压入栈中
}

int main()
{
    string str;
    cin >> str;
    string left;
    for(int i=0;i<str.size();i++) left+='(';
    str=left+str+')';//处理多余括号的情况 可以多左括号 但是多右括号就麻烦了 
    for(int i=0;i<str.size();i++) {
        if(str[i]>='0'&&str[i]<='9') {
            int j=i,t=0;
            while(str[j]>='0'&&str[j]<='9') {
                t=t*10+str[j]-'0';
                j++;
            }
            num.push(t);//处理多位数字的情况 题目并没有保证是10以内的数字
            i=j-1;
        }
        else {
            char c=str[i];
            if(c=='(') ops.push(c);//左括号 直接入栈 
            else if(c=='+'||c=='-') {
                if(c=='-'&&i&&!(str[i-1]>='0'&&str[i-1]<='9')&&str[i-1]!=')') {//处理负数的情况 将减去一个数 改成+负数 
                    int j=i+1,t=0;
                    while(str[j]>='0'&&str[j]<='9') {
                        t=t*10+str[j]-'0';
                        j++;
                    }
                    num.push(-t);
                    i=j-1;
                }
                else {
                    while(ops.top()!='(') cal();
                    ops.push(c);
                }
            }
            else if(c=='*'||c=='/') {
                while(ops.top()=='*'||ops.top()=='/'||ops.top()=='^') cal();//优先级大于等于加减的计算完
                ops.push(c);
            }
            else if(c=='^') {
                while(ops.top()=='^') cal();//乘方的优先级最高
                ops.push(c);
            }
            else if(c==')') {
                while(ops.top()!='(') cal();//遇到右括号
                ops.pop();
            }
            else 
                cout<<"sbsbsbsb"<<endl;//无解?????
        }
    }
    cout<<num.top()<<endl;
    return 0;
}

 

这啥鬼畜代码 我自己都看不懂了 中午好好想想

 

Guess you like

Origin www.cnblogs.com/Tyouchie/p/11570638.html