1331:【例1-2】后缀表达式的值
1 #include<iostream> 2 #include<cstring> 3 #include<stack> 4 using namespace std; 5 typedef long long ll; 6 ll t; 7 stack<ll> sta; 8 ll cal(ll t1,ll t2,char c){ 9 ll ans; 10 if(c=='+')ans=t1+t2; 11 else if(c=='-')ans=t2-t1; 12 else if(c=='*')ans=t1*t2; 13 else ans=t2/t1; 14 return ans; 15 } 16 int main(){ 17 string s; 18 while(cin>>s){ 19 if(s[0]>='0'&&s[0]<='9'){ 20 t=0; 21 for(int i=0;i<s.length();i++) 22 t=t*10+s[i]-'0'; 23 sta.push(t); 24 }else{ 25 for(int i=0;i<s.length()-1;i++){ 26 ll t1=sta.top(),t2; 27 sta.pop(); 28 t2=sta.top(); 29 sta.pop(); 30 sta.push(cal(t1,t2,s[i])); 31 } 32 break; 33 } 34 } 35 cout<<sta.top(); 36 return 0; 37 }