表达式求值(含括号的复杂运算)

具体解析看注释

#include<bits/stdc++.h>
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;
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王也枉不了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值