#include <iostream>
#include <cctype>
#include <stack>
using std::cin, std::cout, std::cerr, std::endl;
using std::stack;
#define NUM 256
struct token_t {
int symbol;
double value;
};
token_t get_token() {
int ch = cin.get();
switch(ch) {
case ' ':
case '\t':
case '\v':
case '\f': return get_token();
}
if(isdigit(ch)) {
cin.unget();
double value; cin >> value;
return {.symbol = NUM, .value = value};
}
return {.symbol = ch};
}
int main() {
cout.setf(cout.fixed);
token_t token;
stack<double> s;
while(true) {
token = get_token();
if(token.symbol == '\n') {
cout << s.top() << endl;
s.pop();
break;
}
if(token.symbol == NUM) {
s.push(token.value);
continue;
}
switch(token.symbol) {
case '+': {
double b = s.top(); s.pop();
double a = s.top(); s.pop();
s.push(a + b);
break;
}
case '-': {
double b = s.top(); s.pop();
double a = s.top(); s.pop();
s.push(a - b);
break;
}
case '*': {
double b = s.top(); s.pop();
double a = s.top(); s.pop();
s.push(a * b);
break;
}
case '/': {
double b = s.top(); s.pop();
double a = s.top(); s.pop();
s.push(a / b);
break;
}
default:
cerr << "无法识别运算符:'" << (char)token.symbol << "'" << endl;
}
}
return 0;
}
|