不用栈会很方便
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <time.h>
#include <iomanip>
#include <cctype>
#include <string>
using namespace std; //https://ac.nowcoder.com/acm/problem/16539
typedef long long ll;
const ll Mod = 10000;
stack<char> s1; //符号栈
stack<ll> s2; //数字栈
//遍历表达式,若是数字则入数字栈
//若是符号,比较当前符号与栈顶符号的优先级,若当前优先级不小于栈顶,则入符号栈
//若当前优先级小,则**将符号栈顶元素弹出,将数字栈顶两个元素弹出并进行对应计算后将结果重新压入数字栈**
//重复该操作直至栈顶元素优先级不大于当前符号,再将当前符号入栈(*保证符号栈的优先级递减性*)
//数字全部入栈后将两栈元素进行上述操作(** **)计算清空,最后数字栈中的元素即为答案
int trans(string x){ //转数字
int ans=0;
for(int j=0;j<x.length();j++){
ans*=10;
ans+=(int)(x[j]-'0');
}
return ans;
}
int cmp(char x){ //设置优先级
if(x=='+'||x=='-') return 1;
else if(x=='*'||x=='/') return 2;
else return 0;
}
ll cal(char x,ll x1,ll x2){
if(x=='+') return x1+x2;
if(x=='-') return (x1-x2)%Mod;
if(x=='*') return ((x1%Mod)*(x2%Mod))%Mod;
if(x=='/') return x1/x2;
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin>>s;
s+=".";
string t="";
for(int i=0;i<s.length();i++){
if(isdigit(s[i]))
t+=s[i];
else if(s[i]=='.')
s2.push(trans(t));
else{
s2.push(trans(t));
t="";
if(s1.empty()){
s1.push(s[i]);
}
else{
if(cmp(s[i])>=cmp(s1.top())){
s1.push(s[i]);
}
else{
while(cmp(s1.top())>cmp(s[i])&&!s1.empty()){
char tmp=s1.top();
s1.pop();
ll x1=s2.top();
s2.pop();
ll x2=s2.top();
s2.pop();
s2.push(cal(tmp,x2,x1));
if(s1.empty()) break;
}
s1.push(s[i]);
}
}
}
}
while(!s1.empty()){
char tmp=s1.top();
s1.pop();
ll x1=s2.top();
s2.pop();
ll x2=s2.top();
s2.pop();
s2.push(cal(tmp,x2,x1));
}
//cout<<s2.top()<<endl;
cout<<s2.top()%Mod<<endl;
return 0;
}