表达式求值 (容器 数组模拟)

表达式求值

给定一个表达式,其中运算符仅包含 +,-,*,/(加 减 乘 整除),可能包含括号,请你求出表达式的最终值。
注意:

  • 数据保证给定的表达式合法。
  • 题目保证符号 - 只作为减号出现,不会作为负号出现,例如,-1+2,(2+2)*(-(1+1)+2) 之类表达式均不会出现。
  • 题目保证表达式中所有数字均为正整数。
  • 题目保证表达式在中间计算过程以及结果中,均不超过 2^31−1。
  • 题目中的整除是指向 0 取整,也就是说对于大于 0 的结果向下取整,例如 5/3=1,对于小于 0 的结果向上取整,例如 5/(1−4)=−1。
  • C++和Java中的整除默认是向零取整;Python中的整除//默认向下取整,因此Python的eval()函数中的整除也是向下取整,在本题中不能直接使用。

输入格式
共一行,为给定表达式。
输出格式
共一行,为表达式的结果。
数据范围
表达式的长度不超过 105。
输入样例:

(2+2)*(1+1)

输出样例:

8

STL对比数组模拟(前者STL,后者数组模拟)
131ms 27ms
STL AC代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>

using namespace std;
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
stack<int> num;
stack<char> op;

void eval()
{
    auto b = num.top(); num.pop();
    auto a = num.top(); num.pop();
    auto c = op.top(); op.pop();
    int x;
    if (c == '+') x = a + b;
    else if (c == '-') x = a - b;
    else if (c == '*') x = a * b;
    else x = a / b;
    num.push(x);
}

int main()
{
	IOS;
    unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
    string str;
    cin >> str;
    for (int i = 0; i < str.size(); i ++ )
    {
        auto c = str[i];
        if (isdigit(c))
        {
            int x = 0, j = i;
            while (j < str.size() && isdigit(str[j]))
                x = x * 10 + str[j ++ ] - '0';
            i = j - 1;
            num.push(x);
        }
        else if (c == '(') op.push(c);
        else if (c == ')')
        {
            while (op.top() != '(') eval();
            op.pop();
        }
        else
        {
            while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval();
            op.push(c);
        }
    }
    while (op.size()) eval();
    cout << num.top() << endl;
    return 0;
}

数组模拟AC代码

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
int a[2000];//存存出现的数字
char s[100005];//输入数组
char b[2000];//存符号
char c[300];//符号优先级
int n=0,i=0,m=0;// n为数字栈栈顶,m为符号栈栈顶,i代表输入字符串的下标
void calculate(){
    switch(b[m-1]){
        case '+':a[n-1]+=a[n];break;
        case '-':a[n-1]-=a[n];break;
        case '*':a[n-1]*=a[n];break;
        case '/':a[n-1]/=a[n];break;
    }
    b[m-1]=b[m];
    m--;
    n--;
}
int judge(){
    if(c[b[m-1]]==3||c[b[m]]==3)//遇到括号不计算,有特别的计算下面
        return 0;
    if(c[b[m-1]]>=c[b[m]])//如果上一个运算符优先级高,则计算
        return 1;
    return 0;//c[b[m]]的优先级比 c[b[m-1]]的高不计算
}
int getint(){//该函数 找到数字为止
    int sum=0;
    if(s[i+1]=='('){//当前为左括号
        b[++m]=s[++i];//入符号栈
        return getint();
    }
    while('0'<=s[++i]&&s[i]<='9')//若当前为数字
    sum=sum*10+s[i]-'0';//转换为实际数字
    return sum;
}
int main(){
    IOS;
    //下标对应值初始化
    c['+']=c['-']=1;
    c['*']=c['/']=2;
    c['(']=c[')']=3;
    scanf("%s",s+1);
    a[++n]=getint();
    while(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'||s[i]==')'){
        b[++m]=s[i];//入栈
        if(s[i]==')'){//到),则从b中逐一弹出操作符直到遇到(,
        //每弹出一个操作符,就通过count函数计算一遍该操作符计算出的值,再将该值压入栈
            while(b[m-1]!='(')
                calculate();
            m-=2;
            i++;//把这一对括号出栈并继续读下一个字符
            continue;
        }
        while(m>1&&judge())//保证有一个符号位并且后面的符号位满足四则运算
            calculate();
        a[++n]=getint();//数字入栈
    }
    m++;//留一个空间,因为每一次计算的是m-1
    while(m>1)
        calculate();
    cout<<a[1]<<endl;
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值