P1449 後綴表達式code
棧。題目說的不是很清楚,沒說包含什麼操做。除法用整數除法就行。
先string讀入字符串,而後從前日後看若是是個數字就入棧,若是是運算符就從棧裏彈出兩個數計算再入棧。ci
#include<iostream> #include<cstring> #include<cstdio> #include<string> #include<stack> #include<algorithm> std::string sss; std::stack<int> s1; inline void read(int &T) { int x=0;bool f=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=!f;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} T=f?-x:x; } int main() { std::cin>>sss; int len=sss.length(); int sum=0; for(int i=0;i<len-1;++i) { if(sss[i]>='0'&&sss[i]<='9') { sum=sum*10+sss[i]-'0'; }else { if(sss[i]=='.') { s1.push(sum); sum=0; } else { int x1=s1.top(); s1.pop(); int x2=s1.top(); s1.pop(); if(sss[i]=='+') s1.push(x1+x2); if(sss[i]=='-') s1.push(x2-x1); if(sss[i]=='*') s1.push(x2*x1); if(sss[i]=='/') s1.push(x2/x1); } } } std::cout<<s1.top(); return 0; }