逆波兰表达式是一种把运算符前置的算数表达式,又被称为后缀表达式。
题目:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
double notation()
{
char str[10];
cin>>str;
printf("str=%s\n",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;
}
atof(),是C 语言标准库中的一个字符串处理函数,功能是把字符串转换成浮点数,所使用的头文件为<stdlib.h>。
该函数名是 “ascii to floating point numbers” 的缩写。
语法格式为:double atof(const char *nptr)。