目錄- [TOC]
- 題目
- 思路
- $Code$
- 題目
- 思路
- $Code$
題目
P1449 后綴運算式
思路
堆疊,題目說的不是很清楚,沒說包含什么操作,除法用整數除法就行,
先string讀入字串,然后從前往后看如果是個數字就入堆疊,如果是運算子就從堆疊里彈出兩個數計算再入堆疊,
\(Code\)
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98157.html
標籤:C++
