計蒜客:表達式求值

 

 

這道題,想了一早上。只可意會不可言傳。

需要注意的點: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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章