计蒜客:表达式求值

 

这道题,想了一早上。只可意会不可言传。

需要注意的点:1.输入的时候ch==10为回车。

while(scanf("%c",&ch)&&ch!=10)
{

}

2.  

#include <iostream>
#include <cstdio>
using namespace std;
const int mod = 1e4;
int main()
{
    char ch;
    int tmp=0,tmpx=1,ans=0;
    while(scanf("%c",&ch)&&ch!=10)
    {
        if(ch>='0'&&ch<='9')
        {
            tmp=tmp*10+ch-'0';
        }else if(ch=='+'){
            if(tmpx!=1)
            {
                tmpx=(tmpx*tmp)%mod;
                ans=(ans+tmpx)%mod;
                tmpx=1;
                tmp=0;
            }else{
                ans=(ans+tmp)%mod;
                tmp=0;
            }
        }else if(ch=='*'){
            tmpx=(tmpx*tmp)%mod;
            tmp=0;
        }
    }
    if(tmpx!=1){
        tmpx=(tmpx*tmp)%mod;
        ans=(ans+tmpx)%mod;
    }else{
        ans=(ans+tmp)%mod;
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/qie-wei/p/12094114.html