Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

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

题目链接:http://poj.org/problem?id=2106

题意:

  求逻辑表达式的值,真或假,可能会含有若干的空格。

思路:

  优先级:! > && > ||  逻辑表达式也同样符合表达式的定义,表达式是由若干的项“||”操作组成, 项是由若干个因子通过“&&”操作组成, 因子由单个F或V组成,也可由带括号的表达式组成。"!"操作同样作为因子的一部分。最后按照定义递归进行即可。

代码:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std; char ep[];
int k; int factor_value()
{
int expression_value(void);
int ret = ;
int flag = ;
while (ep[k] == '!') {
k++;
flag = ~flag;
}
if (ep[k] == '(') {
k++;
ret = expression_value();
k++;
}
else {
if (ep[k] == 'F') {
k++;
ret = ;
}
else if (ep[k] == 'V'){
ret = ;
k++;
}
}
if (flag) return !ret;
else return ret;
} int term_value()
{
int ret = factor_value();
while () {
if (ep[k] == '&') {
k++;
ret = factor_value()&&ret; //同下解释
}
else break;
}
return ret;
} int expression_value()
{
int ret = term_value();
while () {
if (ep[k] == '|') {
k++;
ret = term_value() || ret;
} //term_vaue()一定要写到前面,否则ret为真,term_value就不进行了
else break;
}
return ret;
} int main()
{
//freopen("1.txt", "r", stdin);
char ori[];
int t = ;
while (cin.getline(ori, )) {
int j = ;
for (int i = ; ori[i]; i++) {
if (ori[i] != ' ')
ep[j++] = ori[i];
}
ep[j] = '\0';
//cout << ep << endl;
k = ;
printf("Expression %d: ", ++t);
if (expression_value())
printf("V\n");
else
printf("F\n");
} return ;
}

  

[poj 2106] Boolean Expressions 递归的更多相关文章

  1. POJ 2106 Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述 The objective of the program you are going to produce is to evaluate ...

  2. (栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)

    /* * POJ_2106.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  3. POJ 2106 Boolean Expressions (布尔表达式求值)

    题意:关于!,&,| 的运算,表达式中V代表true,F代表false. 思路:见代码吧,很详细了. 要注意 !!!F,!(...) 的情况. #include <iostream> ...

  4. poj 2106 Boolean Expressions 课本代码

    #include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(in ...

  5. Boolean Expressions POJ - 2106 (表达式求值)

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

  6. POJ | Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述The objective of the program you are going to produce is to evaluate ...

  7. Boolean Expressions

    Boolean Expressions Time Limit: 1000MS   Memory Limit: 30000K       Description The objective of the ...

  8. poj 3295 Tautology 伪递归

    题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...

  9. shorthand trick with boolean expressions

    https://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean --------------------- ...

随机推荐

  1. 蓝桥杯 算法训练 ALGO-129 特殊的数字四十

    算法训练 特殊的数字四十   时间限制:1.0s   内存限制:256.0MB 特殊的数字四十 问题描述 1234是一个非常特殊的四位数,因为它的各位数之和为10,编程求所有这样的四位十进制数. 输出 ...

  2. HDU1272(并查集判图连通)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. 【转】 Pro Android学习笔记(八七):了解Handler(1):组件和线程

    目录(?)[-] 主线程消息队列和线程池 查看线程信息 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingf ...

  4. 【转】link_to中delete无效的问题解决

    问题一 1.问题描述:点击[删除]链接之后,跳到了show页面,完全没有弹出框提示 <%= link_to "删除", product_path(product.id), : ...

  5. docker 安装mysql 使用navicat访问 解决

    1. 下载Mysql的Docker镜像: 2. 运行镜像,设置root账号初始密码(123456),映射本地宿主机端口3306到Docker端口3306.测试过程没有挂载本地数据盘: 3. 查看已运行 ...

  6. pa15-三省吾身

    序号 项 1 凡事提前10分钟    凡事提前10分钟,会让你有充裕的时间应对可能的突发事件,更加从容.    试着把起床闹钟提前10分钟,你就会发现你出门不必急匆匆,早饭也可慢慢享用,一整天的状态也 ...

  7. ghld data format

    %CTF: 1.00%FileType: PROF strp "VelocityProfile"%PROFSpec: 1.00 2006 00 00%Manufacturer: C ...

  8. 问题:oracle decode;结果:oracle中的decode的使用

    oracle中的decode的使用 Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件 ...

  9. C# 正规表达式

    在C#中怎么用正则表达式限制文本框内不能输入数字?只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数字:" ...

  10. Python的IDE:Eclipse+PyDev配置

    最近准备学习python的开发了,当然主要先尝试web方面的开发,个人所学的主要就是javaweb方面,出去了一趟,感觉到了自己的狠多不足,当然也想对自己重新定位一下,不想以后出去只是码畜级别的.想学 ...