Calculator: expression evaluation

 

 

I thought about this question all morning. Can only be understood and unspeakable.

Points to note: 1. When entering, ch==10 means carriage return.

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;
}

 

Guess you like

Origin blog.csdn.net/qie_wei/article/details/88993123