例题:逆波兰表达式
逆波兰表达式是一种把运算符前置的算术表达式,例如普通的
表达式2 + 3的逆波兰表示法为+ 2 3。逆波兰表达式的优点是运算
符之间不必有优先级关系,也不必用括号改变运算次序,例如(2 +
3) * 4的逆波兰表示法为* + 2 3 4。本题求解逆波兰表达式的值,
其中运算符包括+ - * /四个

代码如下:

#include <iostream>
#include<cstdlib>
using namespace std;
double Bolan();
int main()
{
    double s=Bolan();
    cout<<s<<endl;
    return 0;
}
double Bolan()
{
    char s[100];
    cin>>s;
    switch(s[0]){//取字符第一个
        case '+':return Bolan()+Bolan();
        case '-':return Bolan()-Bolan();
        case '*':return Bolan()*Bolan();
        case '/':return Bolan()/Bolan();
        default:return atof(s);
        break;
    }

}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2021-05-23
  • 2021-11-15
  • 2022-12-23
  • 2021-12-26
  • 2021-06-14
  • 2021-07-26
相关资源
相似解决方案