题目:
给定一个包含加减乘除法的算术表达式,请你编程计算表达式的值
输入:
一行,为需要你计算的表达式,表达式中包含数字、加法运算符 “+” 、减法运算符 “-”、乘法运算符 “×”、除法运算符 “/”,且没有括号,所有参与运算的数字均为 0 到 2
31
−1 之间的整数。表达式中加法运算符和乘法运算符的总数 ≤100000。
输入数据保证这一行只有 0−9、+、-、×、/ 这 14种字符,保证除法能够整除。
输出:
一个整数,表示这个表达式的值。
注意:当答案长度多于 4 位时,请只输出最后 4 位,前导 0 不输出。
样例输入:
1+1*3-4/2
样例输出:
2
我的代码:
#include<iostream>
#include<stack>
using namespace std;
stack<int> x;
stack<char> y;
const int mod=10000;
int main(){
int a,b;
char c;
cin>>a;
a=a%mod;
x.push(a);
y.push('+');
while(cin>>c>>b){
if(c=='*'){
a=x.top();
x.pop();
x.push(a*b%mod);
}else if(c=='/'){
a=x.top();
x.pop();
x.push(a/b);
}else{
x.push(b);
y.push(c);
}
}
a=0;
while(y.size()){
if(y.top()=='+')a+=x.top();
else a-=x.top();
a%=mod;
x.pop();
y.pop();
}
cout<<a%mod<<endl;
return 0;
}
只有82分,求助