ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HW12-递归1

2020-04-19 18:53:06  阅读:271  来源: 互联网

标签:HW12 递归 expression shown value expressions include Expression


上次的Hw11我忽略了ddl,结果少交了一道题,哭orz

人果然不能颓,一旦一颓就容易一颓到底。

A:Boolean Expressions

描述

The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next:

Expression: ( V | V ) & F & ( F | V )


where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed.

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file.
输入

The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown.

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below.
输出

For each test expression, print "Expression " followed by its sequence number, ": ", and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line.

Use the same format as that shown in the sample output shown below.
样例输入

( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))

样例输出

Expression 1: F
Expression 2: V
Expression 3: V

来源México and Central America 2004

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 using namespace std;
 5 bool factor_value();
 6 bool term_value();
 7 bool expression_value();
 8 int main(){
 9     int temp = 0;
10     while(cin.peek()!=EOF){
11         if(cin.peek()=='\n'){
12             cin.get();
13             continue;
14         }
15         cout<<"Expression "<<++temp<<": ";
16         bool ans = expression_value();
17         if(ans) cout<<"V"<<endl;
18         else cout<<"F"<<endl;
19     }
20     return 0;
21 }
22 bool expression_value(){
23     bool result = term_value();
24     while(1){
25         while(cin.peek()==' ') cin.get();
26         if(cin.peek()=='|'){//最低优先级
27             cin.get();
28             result |= term_value(); 
29         }
30         else break;
31     }
32     return result;
33 }
34 bool term_value(){
35     bool result = factor_value();
36     while(1){
37         while(cin.peek()==' ') cin.get();
38         if(cin.peek()=='&'){
39             cin.get();
40             result &= factor_value();
41         }
42         else break;
43     }
44     return result;
45 }
46 bool factor_value(){//!(……)类型的因子 
47     bool result = 0;
48     bool reverse = false; //有没有取反 
49     while(cin.peek()==' ') cin.get();
50     while(cin.peek()=='!'){//叹号和叹号之间没有空格应该… 
51         reverse = !reverse;
52         cin.get();
53     }
54     while(cin.peek()==' ') cin.get(); //这步操作完就不会有空格了吧 
55     if(cin.peek()=='('){
56         cin.get();
57         result = expression_value();
58         cin.get();//后括号 
59     } 
60     else{
61         if(cin.peek()=='V') result = true;
62         else result = false;
63         cin.get();
64     }
65     if(reverse) result=!result;
66     return result;
67 }

备注:嗷……这就是上课老师讲的四则表达式那道题嘛。上课没认真想,就觉得还挺有意思,现在想想其实用递归就是相当于实现了不同层次的栈,层次是由运算优先级决定的。expression_value就是最外层的,所以是优先级最差的 | ,其次是term_value也就是 & ,最后是factor_value,就是单个字母或者(……)或者!(……)这样的项。还有就是要注意时刻取空格。

标签:HW12,递归,expression,shown,value,expressions,include,Expression
来源: https://www.cnblogs.com/fangziyuan/p/12732860.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有