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

2023-05-16

具体解析看注释

#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;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

表达式求值(含括号的复杂运算) 的相关文章

随机推荐