L2-033 简单计算器 (25 分)

水题~。

const int N=1010;
int a[N];
stack<int> num;
stack<char> opt;
int n;

int main()
{
    cin>>n;

    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        num.push(x);
    }

    for(int i=0;i<n-1;i++)
    {
        char op;
        cin>>op;
        opt.push(op);
    }

    for(int i=0;i<n-1;i++)
    {
        int a=num.top();
        num.pop();
        int b=num.top();
        num.pop();
        char op=opt.top();
        opt.pop();

        if(op == '+')
            num.push(a+b);
        else if(op == '-')
            num.push(b-a);
        else if(op == '*')
            num.push(a*b);
        else
        {
            if(a == 0)
            {
                printf("ERROR: %d/0\n",b);
                return 0;
            }
            num.push(b/a);
        }
    }
    cout<<num.top()<<endl;

    //system("pause");
    return 0;
}
posted @ 2021-04-20 22:16  Dazzling!  阅读(139)  评论(0编辑  收藏  举报