表达式求值
来源:互联网 发布:百卓优采软件 编辑:程序博客网 时间:2024/06/01 19:21
表达式求值(间接递归)
1.表达式是由项通过+ - 组成
2.项是由因子通过* /组成
3.因子可以为一个整数,因子也可以由左右括号和表达式组成(因子的> 单个整数就相当于递归的终止条件)
PS:看程序如果有困难的话,建议进入debug中!
#include <iostream>#include <cstring>#include <cstdlib>using namespace std;int factor_value();int term_value();int expression_value();int main(){ cout << expression_value() << endl; return 0;}int expression_value() // 求一个表达式的值{ int result = term_value(); // 求第一项的值 bool more = true; while( more) { char op = cin.peek(); // 看一个字符, 不取走 if( op == '+' || op == '-' ) { cin.get(); // 从输入中取走一个字符 int value = term_value(); if( op == '+' ) result += value; else result -= value; } else more = false; } return result;}int term_value() // 求一个项的值{ int result = factor_value(); // 求第一个因子的值 while(true) { char op = cin.peek(); if( op == '*' || op == '/') { cin.get(); int value = factor_value(); if( op == '*') result *= value; else result /= value; } else break; } return result;}int factor_value() // 求一个因子的值{ int result = 0; char c = cin.peek(); if( c == '(') { cin.get(); result = expression_value(); cin.get(); } else { while(isdigit(c)) { result = 10 * result + c - '0'; cin.get(); c = cin.peek(); } } return result;}
0 0
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 表达式求值
- 走进Spring Framework
- table学习笔记
- 建造者模式
- sort()
- 中国剩余定理
- 表达式求值
- Httpclient发送http请求
- Angular开发者指南(一)入门介绍
- NSIS总脚本代码参考
- 《Head First JAVA》学习笔记(一)
- hdu5950
- JAVA设计模式之工厂模式
- 输出错误信息与调试信息
- iOS10 不自动弹出询问是否允许使用蜂窝数据的解决方法