将运算符前置的一种表达式。很简单,没什么要具体说明的。逐个拆就行。
#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);//将字符串转换为float型
}
}
int main(){
cout << notation() << endl;
system("pause");
return;
}
前缀表达式解析
本文介绍了一种使用C++实现的前缀表达式解析方法。通过递归地调用自身来处理加减乘除等运算符,并利用atof函数将数字字符串转换为浮点数进行计算。此程序能够读取简单的前缀表达式并输出其计算结果。
757

被折叠的 条评论
为什么被折叠?



