前缀表达式求值

版权声明:请勿商业化使用 https://blog.csdn.net/qq_40991687/article/details/90115769
前缀表达式求值(逆波兰表达式求值)
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
double exp() {
	char a[10];
	cin>>a;//去当前输出的字符串
	switch(a[0]) {//看第一个字符是否是运算符
		case '+':
			return exp()+exp();
		case '-':
			return exp()-exp();
		case '*':
			return exp()*exp();
		case '/':
			return exp()/(exp()==0?1e9:exp());//分母判零情况
		default :
			return atof(a);//atof()将数字字符串转换浮点数类型
	}
}
int main() {
	printf("%lf\n",exp());
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40991687/article/details/90115769