波兰表达式是一种把运算符前置的算术表达式,例如普通的表达式2 + 3的波兰表示法为+ 2 3。波兰表达式的优点是运算符之间不必有优先级关系,也不必用括号改变运算次序,例如(2 + 3) * 4的波兰表示法为* + 2 3 4。本题求解波兰表达式的值,其中运算符包括+ - * /四个。
Input
输入为一行,其中运算符和运算数之间都用空格分隔,运算数是浮点数。
Output
输出为一行,表达式的值。
可直接用printf(“%f\n”, v)输出表达式的值v。
Sample Input
* + 11.0 12.0 + 24.0 35.0
Sample Output
1357.000000
Hint
可使用atof(str)把字符串转换为一个double类型的浮点数。atof定义在math.h中。
此题可使用函数递归调用的方法求解。
ac代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <string.h>
#include <set>
using namespace std;
typedef unsigned long long ULL;
char a[100];
double expr()
{
scanf("%s",a);
if(a[0]=='+')
return expr()+expr();
else if(a[0]=='-')
return expr()-expr();
else if(a[0]=='*')
return expr()*expr();
else if(a[0]=='/')
return expr()/expr();
else
return atof(a);
}
int main()
{
printf("%lf",expr());
return 0;
}