题目描述
逆波兰表达式是一种把运算符前置的算术表达式
如2 + 3的逆波兰表达式为+ 2 3
如(2 + 3) * 4的逆波兰表达式为 * + 2 3 4
编写一个程序求解仅含 + - * /四个运算符的逆波兰表达式
输入:* + 11.0 12.0 + 24.0 35.0
输出:1357.0
#include<iostream>
using namespace std;
double notation() { //逆波兰表达式
char str[10];
cin >> str;
switch (str[0])
{
case '+': return notation() + notation();
case '-': return notation() - notation();
case '*': return notation() * notation();
case '/': return notation() / notation();
default: return atof(str); //字符串转化为浮点数
}
}
int main() {
cout << notation();
return 0;
}