1. 程式人生 > >NOIP2013 表示式求值

NOIP2013 表示式求值

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
const int Tmax=105,MOD=10000;
stack<int> Snum,Sopt;
void clear(int num)
{
    int tmp1,tmp2,opt;
    while(!Sopt.empty())
    {
        if(Sopt.top()<=num)
        {
            opt=Sopt.top();
            Sopt.pop();
            tmp1=Snum.top();
            Snum.pop();
            tmp2=Snum.top();
            Snum.pop();
            if
(opt==2) Snum.push((tmp1+tmp2)%MOD); else Snum.push((tmp1*tmp2)%MOD); } else break; } return; } int main() { int num=0; char ch; while(scanf("%c",&ch)==1&&ch!='\n') { if(ch=='+'){ Snum.push(num); clear(2); Sopt.push(2
); num=0; } else if(ch=='*'){ Snum.push(num); clear(1); Sopt.push(1); num=0; } else num=(num*10+ch-'0')%MOD; } Snum.push(num); clear(3); printf("%d",Snum.top()); return 0; }