[# 4] algorithm expression evaluation

The most annoying problem that made the calculator ......

This calculator was recently made several pit title, or a universal - the title. There are a lot of expression, but we are in the face of infix expression dog feces are there any principles set?

Have. Before one of which is the operator in the operator stack, the operator of operator precedence top of the stack must have strictly lower than the priority of the operator, the operator and the number of pop-up or stack operand stack until the stack is empty calculates or the current priority is strictly greater than the stack operator precedence. Of course, the premise is that you use this evaluation method, other things temporarily not be considered.

First, we encounter an operand, operand read and added to the operand stack operands, when we read the operator, the operator stack pop the top element until the top element priority strictly less than the current operator precedence or stack empty.

After completion of reading expression. Note that if the end of the operand is sure to include the operand stack for the stack pop the top element and the operator until the stack is empty is calculated. Thus there is only one element and the value of the expression in the operand stack.

There are other when faced with a left parenthesis, added directly to the operator stack, when faced with a right parenthesis continuous pop-up operator until it encounters a left parenthesis or stack empty (fatal error).

Board posted here, is not particularly full, only supports the + and * operators can only enter a positive integer. In fact, the popularity - the title of the code, change it a little magic on it.

print(input())

(fog)

The following is the board.

#include<bits/stdc++.h>
using namespace std;
long long stao[100005],top1,stan[100005],top2,temp;
string inp;
bool mk;
int main(){
    getline(cin,inp);
    for(int i=0;i<inp.length();i++){
        if(inp[i]<='9'&&inp[i]>='0'){
            temp=temp*10+inp[i]-'0';
            mk=1;
        }
        else {
            if(mk){
                stan[++top2]=temp;
                temp=0;
                mk=0;
            }
            if(inp[i]=='+'){
                while(top1){
                    if(stao[top1]==-1){
                        top1--;
                        top2--;
                        stan[top2]+=stan[top2+1];
                        stan[top2]%=10000;
                    }
                    else{
                        top1--;
                        top2--;
                        stan[top2]*=stan[top2+1];
                        stan[top2]%=10000;
                    }
                }
                stao[++top1]=-1;
            }
            else{
                while(stao[top1]==-2&&top1){
                    top1--;
                    top2--;
                    stan[top2]*=stan[top2+1];
                    stan[top2]%=10000;
                }
                stao[++top1]=-2;
            }
        }
    }
    if(mk)
        stan[++top2]=temp;
    while(top1){
        if(stao[top1]==-1){
            top1--;
            top2--;
            stan[top2]+=stan[top2+1];
            stan[top2]%=10000;
        }
        else{
            top1--;
            top2--;
            stan[top2]*=stan[top2+1];
            stan[top2]%=10000;
        }
    }
    cout<<stan[1]%10000;
}

Guess you like

Origin www.cnblogs.com/Schwarzkopf-Henkal/p/11830216.html