#include "cstdio"
#include "iostream"
#include "stack"
#include "string"
#include "sstream"
using namespace std;
stack<float> n;
stack<string> f;
int main(){
string s,buf;
float a,b;
float temp;
while(getline(cin,s)){
stringstream ss(s);
while(ss >> buf){
// cout << buf;
if(buf[0] == '+'){
b = n.top();
// cout << b <<endl;
n.pop();
a = n.top();
// cout << a <<endl;
n.pop();
n.push(a+b);
// cout << n.top()<<endl;
}
else if(buf[0] == '-'){
b = n.top();
n.pop();
a = n.top();
n.pop();
n.push(a-b);
}
else if(buf[0] == '*'){
b = n.top();
n.pop();
a = n.top();
n.pop();
n.push(a*b);
}
else {
stringstream toFloat;
toFloat << buf;
toFloat >> temp;
// cout << temp <<endl;
n.push(temp);
}
}
cout <<n.top() << endl;
}
}
逆波兰算法 C++——String及Stack实现
最新推荐文章于 2022-10-22 20:07:12 发布