解题思路:
逆波兰表达式的第一个字符一定是运算符,判断输入的字符串第一个字符是否为运算符,是则返回两个子逆波兰表达式相运算的结果,如不是则返回数值
注意事项:
不能使用gets读入字符串
atof是将字符串转换为浮点数的函数 ,在头文件stdlib.h中
本题还可利用栈的思想
参考代码:
#include #include double getResult() { char a[20]; scanf("%s", a); switch(a[0]) { case '*': return getResult() * getResult(); case '/': return getResult() / getResult(); case '+': return getResult() + getResult(); case '-': return getResult() - getResult(); default: return atof(a); } } int main() { printf("%lf", getResult()); return 0; }
0.0分
3 人评分
【计算两点间的距离】 (C语言代码)浏览:1482 |
C语言程序设计教程(第三版)课后习题11.8 (C语言代码)浏览:683 |
C二级辅导-进制转换 (C语言代码)浏览:660 |
Hello, world! (C语言代码)浏览:824 |
C语言程序设计教程(第三版)课后习题8.9 (C语言代码)浏览:498 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:1220 |
剔除相关数 (C语言代码)浏览:1091 |
C语言程序设计教程(第三版)课后习题10.7 指针(C语言代码)浏览:544 |
P1044 (C++代码)浏览:493 |
采药 (C语言代码)浏览:921 |