2021-04-23

 逆波兰求值

 

#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
using namespace std;
int priority(int x, char ch, int y)//判断字符为什么运算符,两个数字计算 并返回
{
    switch (ch)
    {
    case '+':
        return x + y;
        break;
    case '-':
        return x - y;
        break;
    case '*':
        return x * y;
        break;
    case '/':
        return x / y;
        break;
    }
}
int first(char top, char ch)//比较优先级
{

    if ((top == '+' || top == '-') && (ch == '+' || ch == '-')) return 1;//优先级相等
    else if ((top == '*' || top == '/') && (ch == '*' || ch == '/')) return 1;//优先级相等
    else if ((top == '*' || top == '/') && (ch == '+' || ch == '-')) return 1;//优先级低
    else  if (ch == ')') return 0;
    else return 2;//优先级高的情况或者(

}
int main()
{
    string s;
    cin >> s;
    stack<int> a;
    stack<char>b;


    int num=0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] >= '0' && s[i] <= '9')//判断是否为数字
        {
            if(num==0)
            {
                a.push(s[i]-'0');
                num=1;
            }
            else
            {
                int Number=a.top();
                a.pop();
                Number= Number*10+(s[i]-'0');
                a.push(Number);
            }



        }
        //以下为字符情况
        else
        {
            num=0;


            if (!b.empty())
            {

                int temp = first(b.top(), s[i]);//判断优先级进而返回相应的值
                if (temp == 1)//如果当前字符优先级小于等于stack.top(),执行出栈
                {
                    int y = a.top();//数字栈top
                    a.pop();
                    int x = a.top();//数字栈top
                    a.pop();
                    int result = priority(x, b.top(), y);//计算数字栈出来的两个数字的值

                    b.pop();
                    b.push(s[i]);
                    a.push(result);//将计算后的值入到数字栈



                }
                else if (temp == 2)//优先级高的情况和(的情况,直接入字符栈
                {
                    b.push(s[i]);

                }

                else if (temp == 0) //遇到右括号的情况
                {


                    while (b.top() != '(')
                    {
                        //直到遇到左括号之前进行出栈,只要字符栈有字符出栈,就有数字栈出来两个数字进行计算



                        b.top();
                        int y = a.top();//数字栈top
                        a.pop();
                        int x = a.top();//数字栈top
                        a.pop();
                        int result = priority(x, b.top(), y);//计算数字栈出来的两个数字的值
                        b.pop();
                        a.push(result);//将计算后的值入到数字栈


                    }

                    b.pop();

                }
            }
            else//字符栈为空就直接入栈
            {
                b.push(s[i]);

            }
        }

    }
    while (!b.empty())
    {

        char C = b.top();
        b.pop();
        int y = a.top();
        a.pop();
        int x = a.top();
        a.pop();
        int result = priority(x, C, y);
        a.push(result);
    }
    cout << a.top() << endl;

    return 0;
}

                                                                                三连走你

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值