Infix suffix turn (reverse Polish evaluation)

Between (1) 0-2 ^ 32, consider the case where a number greater than 10.

(2) C / C ++ to achieve

In (3) suffix prefix turn the call stack stack 

(4) evaluation suffix, using their own definition of the evaluation stack

Ideas:

Initialization two stacks: stack operators s1 and s2 stack store intermediate results;
    infix scan from left to right;
    encounters an operand, it is pressed s2;
    encountered operator, compares it to the stack s1 operator precedence:
    If s1 is empty, or top of the stack to the left of the operator bracket "(", this push operator directly;
    otherwise, if the top of the stack is higher than the priority of the operator, the operator will press the s1 (Note that to convert prefix expression is higher or the same priority and the same situation are not included here);
    otherwise, the pop-up operator stack s1 and s2 pressed into, go again (4 -1) s1 is compared with the new top of stack operator;
    encountered brackets:
    for a left bracket "(", directly pressed s1;
    if the right parenthesis ")", then in turn the pop stack operation s1 Fu, and pressed into s2, until it left parenthesis is encountered, then discards the pair of brackets;
    repeat steps 2-5 until the rightmost expression;
    s1 to the remaining operators and pressed sequentially eject S2;
    s2 elements successively ejected and the output is the reverse infix expressions corresponding postfix expression results ( Do not reverse the conversion as a prefix expression)

 

achieve:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct sstack{
    int top;
    int *data;
    int MaxSize;
    void InitStack(int sz){
        MaxSize = sz;
        data = new int[sizeof(int)*MaxSize];
        top = -1;
    }
    void FreeStack(){
        free(data);
    }
    void MakeEmpty(){
        top = -1;
    }
    bool IsEmpty(){
        return top ==-1;
    }
    bool IsFull(){
        return top == MaxSize-1;
    }
    int Push(int item){
        if(!IsFull()){
            data[++(top)] = item;
            return 0;
        }
        return -1;
    }
    int Pop(){
        if(!IsEmpty()){
            return data[(top) --];
        }
        return -1;
    }
    int GetTop(){
        if(!IsEmpty()){
            return data[top];
        }
        return -1;
    }
}Stack;

string zh_str =  "(2*(3+4)+5)*6-6/3";
string af_str ="";
int fir[128];
void init()
{
    fir['+']=fir['-'] = 1;
    fir['*']=fir['/'] = 2;
}
void Work1()
{
    stack<char>S1;

    int len = zh_str.length();
    int cot = 0 ;
    for(int i = 0 ; i < len ; i++)
    {
        if(zh_str[i]==' ')
            continue;
        if(zh_str[i]>='0' && zh_str[i]<='9')
        {
            int tempnum = 0 ;
            while(zh_str[i]>='0' && zh_str[i]<='9'){
                tempnum = tempnum*10 + (zh_str[i++]-'0');
            }
            i--;
            af_str += tempnum +'0';
            af_str +=' ' ;
        }
        else if(zh_str[i]=='(')  // 左括号直接进栈
        {
            S1.push(zh_str[i]);
        }
        else if(zh_str[i]==')') // 遇到右括号  依次出栈 直到碰到 (
        {
            while(S1.top()!='(' && !S1.empty())
            {
                af_str += S1.top();
                af_str +=' ' ;
                S1.pop();
            }
            S1.pop();// "(" 出栈
        }
        else // 处理  符号优先级的问题
        {
            if(S1.empty())
            {
                S1.push(zh_str[i]);
            }
            else{
               while(!S1.empty() && S1.top()!='(' && fir[S1.top()] >= fir[zh_str[i]] )
                {
                    af_str += S1.top();
                    af_str +=' ' ;
                    S1.pop();
                }
                S1.push(zh_str[i]);
            }
        }
    }
    while(!S1.empty()){  // 栈中所有元素出栈
        af_str += S1.top();
        af_str +=' ' ;
        S1.pop();
    }
    cout<<"after polish : "<<endl;
    cout<<af_str<<endl;
}
void Work2()
{
    Stack SK;// 自定义栈;
    SK.InitStack(100);
    int len = af_str.length();
    for(int i = 0 ; i < len ; i++)
    {
        if(af_str[i] == ' ')
            continue;
        if(af_str[i]>='0' && af_str[i]<='9')
        {
            int tempnum = 0 ;
            while(af_str[i]>='0' && af_str[i]<='9'){
                tempnum = tempnum*10 + (af_str[i++]-'0');
            }
            i--;
            SK.Push(tempnum);
        }
        else{
            int k ;
            switch(af_str[i]){
                case '+':
                    k = SK.Pop() + SK.Pop();
                    SK.Push(k);
                    break;
                case '-':
                    k = SK.Pop();
                    k = SK.Pop() - k;
                    SK.Push(k);
                    break;
                case '*':
                    k = SK.Pop() * SK.Pop();
                    SK.Push(k);
                    break;
                case '/':
                    k = SK.Pop();
                    k = SK.Pop() / k;
                    SK.Push(k);
                    break;
            }
        }

    }
    cout<<"the ans is : "<<SK.Pop()<<endl;
}
int main()
{
    init();
    Work1();
    Work2();
    return 0;
}

 

Published 372 original articles · won praise 89 · Views 230,000 +

Guess you like

Origin blog.csdn.net/sizaif/article/details/100561062