数据结构算法——1022. 波兰表达式

题目

数据结构算法——1022. 波兰表达式

思路

中缀转成逆波兰表达式再按栈的特性操作

如何转换?也是利用栈特性:
①数字放在数字的栈里面
②操作栈若为空 随便放
③(*/都是随便放的
④当遇见‘)’的时候,把操作栈逐个排出(每次排除拿数字栈的两个元素进行操作再塞回去结果),直到遇见‘(’,此时把‘(’弹出
⑤塞入+和-的时候,若前一个操作为‘*’或者‘/’,我们就弹出元素知道遇见左括号或者栈空

代码

#include<iostream>
#include<string.h>
using namespace std;

int cal(int a, int b, char c)
{
    //cout << "a " << a <<" b " << b <<" c "<<c << endl;
    switch (c)
    {
    case '+':
        return a + b;
    break;
    case '-':
        return b - a;
    break;
    case '*':
        return a * b;
    break;
    case '/':
        return b / a;
    break;
    default:
        break;
    }
}
void print(int* a, int n)
{
    for(int i = 0 ; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
    cout << n << endl;
}

int main()
{
    char ope[1000];
    int num[1000];
    char NUM[100];

    int on,nn,NN;
    on = nn = NN = 0;

    string str;
    cin >> str;
    int l = str.length();

    for(int i = 0; i < l ;i++)
    {
        if(str[i] == '+')
        {
            if(on == 0)
                ope[on++] = '+';
            else if(ope[on - 1] == '*' || ope[i - 1] == '/')
            {
                while(on && ope[on - 1] != '(')
                {
                    int a = num[--nn];
                    int b = num[--nn];
                    int c = cal(a,b,ope[--on]);
                    num[nn++] = c;
                }
                ope[on++] = '+';
            }    
            else
            {
                ope[on++] = '+';
            }      

        }
        else if(str[i] == '-')
        {
            if(i == 0 || (str[i - 1] > '9' || str[i - 1] < '0'))
            {
                NUM[NN++] = '-';
            }
            else if(on == 0)
                ope[on++] = '-';
            else if(ope[on - 1] == '*' || ope[i - 1] == '/')
            {
                while(on && ope[on - 1] != '(')
                {
                    int a = num[--nn];
                    int b = num[--nn];
                    int c = cal(a,b,ope[--on]);
                    num[nn++] = c;
                }
                ope[on++] = '-';
            }      
            else
                ope[on++] = '-';
        }
        else if(str[i] == '*')
        {
            ope[on++] = '*';
        }
        else if(str[i] == '/')
        {
            ope[on++] = '/';
        }
        else if(str[i] == '(')
        {
            ope[on++] = '(';
        }
        else if(str[i] == ')')
        {
            while(on && ope[on - 1] != '(')
            {
                int a = num[--nn];
                int b = num[--nn];
                int c = cal(a,b,ope[--on]);
                num[nn++] = c;
            }
            on--;
        }
        else if(str[i] == ' ')
        {
            continue;
        }
        else//
        {
            while((str[i] >= '0' && str[i] <= '9') && i < l)
            {
                NUM[NN++] = str[i++];
            }
            NUM[NN] = 0;
            num[nn++] = atoi(NUM);
            NN = 0;
            i--;//别忘记减
        }

        // cout << "i " << i << endl;
        // cout << " operat"<<endl;
        // for(int i = 0; i < on; i++)
        // {
        //     cout << ope[i] << " ";
        // }cout << endl;
        // cout << " num"<<endl;
        // for(int i = 0; i < nn; i++)
        // {
        //     cout << num[i] << " ";
        // }cout << endl<<endl;
    
    }
    //print(num,nn);
    while(on > 0) 
    {
        int a = num[--nn];
        int b = num[--nn];
        int c = cal(a,b,ope[--on]);
        num[nn++] = c;
        //print(num,nn);
        //cout << " on " << on << " nn " << nn << endl;
    }

    cout << num[0] << endl;
}
上一篇:[LeetCode] 1022. Sum of Root To Leaf Binary Numbers


下一篇:1022 D进制的A+B (20 分)