上次的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:
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,就是单个字母或者(……)或者!(……)这样的项。还有就是要注意时刻取空格。