- 表达式求值
- @ 2016-07-09 12:51:30
//运算后的结果会溢出int
#include <cstdio>
#include <stack>
using namespace std;
const int MOD=10000;
int main()
{
    stack<int> it;
    stack<char> op;
    int e;
    char ch;
    while(scanf("%d%c",&e,&ch)&&ch!='\n')
    {
        e%=MOD;
        it.push(e);
        if(op.empty())
        {
            op.push(ch);
        }
        else
        {
            char tp=op.top();
            if(ch=='*'&&tp=='+')
            {
                op.push(ch);
            }
            else
            {
                op.pop();
                int y=it.top();it.pop();
                int x=it.top();it.pop();
                int z;
                if(tp=='+') z=x+y;
                else z=x*y;
                z%=MOD;
                it.push(z);
                op.push(ch);
            }
        }
    }
    it.push(e);
    while(!op.empty())
    {
        char ch=op.top();op.pop();
        int y=it.top();it.pop();
        int x=it.top();it.pop();
        int z;
        if(ch=='+') z=x+y;
        else z=x*y;
        z%=MOD;
        it.push(z);
    }
    int res=it.top();
    res%=MOD;
    printf("%d\n",res);
    return 0;
}
0 条评论
信息
- ID
- 1849
- 难度
- 7
- 分类
- (无)
- 标签
- 递交数
- 3595
- 已通过
- 770
- 通过率
- 21%
- 被复制
- 10
- 上传者