博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法答疑---递归实现表达式求值
阅读量:4563 次
发布时间:2019-06-08

本文共 3485 字,大约阅读时间需要 11 分钟。

算法答疑---递归实现表达式求值

一、总结

一句话总结:表达式求值除了日常的栈做法,也可以用递归。

 

1、递归的核心是什么?

递推公式和边界条件

递推公式就是递归的逻辑,有时候不一定方便写出来

 

2、c++中int factor();形式的代码是做什么用的

函数声明,c++中的函数声明是有带函数返回值的,也就是最前面那个int。

1 #include
2 #include
3 using namespace std; 4 int term(); 5 int expr(); 6 int factor();

 

 

 

二、3.1 表达式求值(递归实现)

#include
#include
using namespace std;int term();int expr();int factor();int expr(){ int result = term(); bool more = true; while(more) { char c = cin.peek(); if(c == '+' || c == '-') { cin.get(); int value = term(); if(c == '+') result += value; else if(c == '-') result -= value; } else more = false; } return result;}int term(){ int result = factor(); bool more = true; while(more) { char c = cin.peek(); if(c == '*' || c == '/') { cin.get(); int value = factor(); if(c == '*') result *= value; else result /= value; } else more = false; } return result;}int factor(){ int result = 0; char c = cin.peek(); if(c == '(') { cin.get();//去掉左括号 result = expr(); cin.get();//去掉右括号 } else { while(isdigit(c)) { result = result*10 + c - '0'; cin.get(); c = cin.peek() ; } } return result;}int main(){ cout << expr() << endl; //cout << term() << endl;// cout << factor() << endl; return 0;}

还有一种方法是通过栈来实现,后面更新~

 
 
 

三、代码测试(代码的中间结果)

测试代码:

1 #include
2 #include
3 using namespace std; 4 int term(); 5 int expr(); 6 int factor(); 7 8 int expr() 9 {10 cout<<"---expr---"<

 

in.txt

(2+3)+(5+7)+9/3

 

 

out.txt

1 ---expr---  2 ---term---  3 ---factor---  4 factor_cin.peek(): (  5 ---expr---  6 ---term---  7 ---factor---  8 factor_cin.peek(): 2  9 factor_return_result: 2 10 ---factor---END 11  12 term_first_result: 2 13 term_cin.peek(): + 14 term_return_result: 2 15 ---term---END 16  17 expr_first_result: 2 18 expr_cin.peek(): + 19 ---term--- 20 ---factor--- 21 factor_cin.peek(): 3 22 factor_return_result: 3 23 ---factor---END 24  25 term_first_result: 3 26 term_cin.peek(): ) 27 term_return_result: 3 28 ---term---END 29  30 expr_cin.peek(): ) 31 expr_return_result: 5 32 ---expr---END 33  34 factor_return_result: 5 35 ---factor---END 36  37 term_first_result: 5 38 term_cin.peek(): + 39 term_return_result: 5 40 ---term---END 41  42 expr_first_result: 5 43 expr_cin.peek(): + 44 ---term--- 45 ---factor--- 46 factor_cin.peek(): ( 47 ---expr--- 48 ---term--- 49 ---factor--- 50 factor_cin.peek(): 5 51 factor_return_result: 5 52 ---factor---END 53  54 term_first_result: 5 55 term_cin.peek(): + 56 term_return_result: 5 57 ---term---END 58  59 expr_first_result: 5 60 expr_cin.peek(): + 61 ---term--- 62 ---factor--- 63 factor_cin.peek(): 7 64 factor_return_result: 7 65 ---factor---END 66  67 term_first_result: 7 68 term_cin.peek(): ) 69 term_return_result: 7 70 ---term---END 71  72 expr_cin.peek(): ) 73 expr_return_result: 12 74 ---expr---END 75  76 factor_return_result: 12 77 ---factor---END 78  79 term_first_result: 12 80 term_cin.peek(): + 81 term_return_result: 12 82 ---term---END 83  84 expr_cin.peek(): + 85 ---term--- 86 ---factor--- 87 factor_cin.peek(): 9 88 factor_return_result: 9 89 ---factor---END 90  91 term_first_result: 9 92 term_cin.peek(): / 93 ---factor--- 94 factor_cin.peek(): 3 95 factor_return_result: 3 96 ---factor---END 97  98 term_cin.peek():  99 term_return_result: 3100 ---term---END101 102 expr_cin.peek(): 103 expr_return_result: 20104 ---expr---END105 106 20

 

 
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/Renyi-Fan/p/9733728.html

你可能感兴趣的文章
pyqt 自定义信号
查看>>
多任务--进程 及 进程间通信
查看>>
多线程/多进程+QProgressBar实现进度条
查看>>
多任务(进程)案例----- 拷贝文件夹
查看>>
Kotlin的快速入门
查看>>
python 虚拟环境的 安装与 使用 和修改为豆瓣源
查看>>
js 快速入门
查看>>
Python 中的GIL
查看>>
如何解决ASCII 字符显示不出来的情况
查看>>
制表符 \t 的用法
查看>>
断点模式
查看>>
Ubuntu 侧边栏和顶栏设置
查看>>
底层原理
查看>>
21. Merge Two Sorted Lists
查看>>
shiro设置加密算法源码解析
查看>>
第二次冲刺
查看>>
实验四
查看>>
win8.1镜像制作
查看>>
Windows 服务开发框架介绍 - Topshelf
查看>>
php,字符串(二)
查看>>