zoukankan      html  css  js  c++  java
  • 算法答疑---递归实现表达式求值

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

    一、总结

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

    1、递归的核心是什么?

    递推公式和边界条件

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

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

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

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

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

    #include<iostream>
    #include<cstring>
    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<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 int term();
     5 int expr();
     6 int factor();
     7  
     8 int expr()
     9 {
    10     cout<<"---expr---"<<endl;
    11     int result = term();
    12     cout<<"expr_first_result: "<<result<<endl;
    13     bool more = true;    
    14     while(more)
    15     {
    16         char c = cin.peek();
    17         cout<<"expr_cin.peek(): "<<c<<endl;
    18         if(c == '+' || c == '-')
    19         {
    20             cin.get();
    21             int value = term();
    22             if(c == '+')
    23                 result += value;
    24             else if(c == '-')
    25                 result -= value;
    26         }
    27         else more = false; 
    28     }
    29     cout<<"expr_return_result: "<<result<<endl;    
    30     cout<<"---expr---END"<<endl;
    31     cout<<endl;
    32     return result;
    33 }
    34  
    35 int term()
    36 {
    37     cout<<"---term---"<<endl;
    38     int result = factor();
    39     cout<<"term_first_result: "<<result<<endl;
    40     bool more = true;
    41     while(more)
    42     {
    43         char c = cin.peek();
    44         cout<<"term_cin.peek(): "<<c<<endl;
    45         if(c == '*' || c == '/')
    46         {
    47             cin.get();
    48             int value = factor();
    49             if(c == '*')
    50                 result *= value;
    51             else
    52                 result /= value;
    53         }
    54         else
    55             more = false;
    56     }
    57     cout<<"term_return_result: "<<result<<endl;
    58     cout<<"---term---END"<<endl;
    59     cout<<endl;
    60     return result;
    61 }
    62  
    63 int factor()
    64 {
    65     cout<<"---factor---"<<endl;
    66     int result = 0;
    67     char c = cin.peek();
    68     cout<<"factor_cin.peek(): "<<c<<endl;
    69     if(c == '(')
    70     {
    71         cin.get();//去掉左括号 
    72         result = expr();
    73         cin.get();//去掉右括号 
    74     }
    75     else
    76     {
    77         while(isdigit(c))
    78         {
    79             result = result*10 + c - '0';
    80             cin.get();
    81             c = cin.peek() ;
    82         }
    83     } 
    84     cout<<"factor_return_result: "<<result<<endl;    
    85     cout<<"---factor---END"<<endl;
    86     cout<<endl;
    87     return result;
    88 }
    89 int main()
    90 {
    91     freopen("in.txt","r",stdin); 
    92     freopen("out.txt","w",stdout); 
    93     cout << expr() << endl;
    94     //cout << term() << endl;
    95 //    cout << factor() << endl; 
    96     return 0;
    97 } 

    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: 3
    100 ---term---END
    101 
    102 expr_cin.peek(): 
    103 expr_return_result: 20
    104 ---expr---END
    105 
    106 20
     
     
     
     
     
     
     
     
  • 相关阅读:
    将PHP文件生成静态文件源码
    Entity Framework Code First 学习日记(6)一对多关系
    Entity Framework Code First 学习日记(5)
    Entity Framework Code First 学习日记(3)
    Entity Framework Code First 学习日记(7)多对多关系
    Entity Framework Code First学习日记(2)
    Entity Framework Code First 学习日记(8)一对一关系
    Entity Framework Code First 学习日记(9)映射继承关系
    Entity Framework Code First 学习日记(10)兼容遗留数据库
    Entity Framework Code First 学习日记(4)
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/9733728.html
Copyright © 2011-2022 走看看