1331:【例1-2】字尾表示式的值
//洛谷ac,一本通10分,一本通應該是覺得這個題目不需要用stl吧,棧的應用用stl可能結構清晰點
#include<iostream>
#include<stack>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
stack<int> s1;
stack<char> s2;
int n;
void calculate(stack<int> &s1,stack<char> &s2)
{
int x,y;
char z;
y=s1.top(); s1.pop();
x=s1.top(); s1.pop();
z=s2.top(); s2.pop();
if(z=='+') s1.push(x+y);
if(z=='-') s1.push(x-y);
if(z=='*') s1.push(x*y);
if(z=='/') s1.push(x/y);
}
int main()
{
int i;
int len,on=0;
int temp=0;
char str[250];
gets(str);//這裡用了scanf輸入字元,搞了好久沒有搞明白,想來想去沒有想明白問題在這裡,浪費了大量時間
len=strlen(str);
for (i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9') {temp=temp*10+(str[i]-'0');on=1;}
else{
if(on==1) { s1.push(temp); temp=0;on=0;}
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/') s2.push(str[i]);
while(s2.empty()==false) calculate(s1,s2);
if(str[i]=='@') break;
}
}
cout<<s1.top()<<endl;
return 0;
}