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. Azure Managed Disk操作

    Azure Managed Disk对原有的Page Blob进行了一次封装.使得Azure VM的Disk操作变得非常简单.本文将介绍实际操作中针对Manage Disk的一些操作. 一.创建Man ...

  2. PHP7卓越性能背后的原理有哪些?

    作者:韩天峰链接:http://www.zhihu.com/question/38148900/answer/75115687来源:知乎 PHP7在运行原理上与PHP5相比并没有变化,这与hhvm不同 ...

  3. 蓝桥杯 算法训练 ALGO-147 4-3水仙花数

    算法训练 4-3水仙花数   时间限制:1.0s   内存限制:256.0MB 问题描述 打印所有100至999之间的水仙花数.所谓水仙花数是指满足其各位数字立方和为该数字本身的整数,例如 153=1 ...

  4. java代码swing编程 制作一个单选按钮的Frame

    不善于思考,结果费了时间,也没有效果 下面的框框可以做出来. package com.kk; import javax.swing.JFrame; import javax.swing.JLabel; ...

  5. linux下面的挂载点讲解

    linux.unix这类操作系统将系统中的一切都作为文件来管理.在windows中我们常见的硬件设备.磁盘分区等,在linux.unix中都被视作文件,对设备.分区的访问就是读写对应的文件.挂载点实际 ...

  6. Ubuntu16.04+TensorFlow r1.12环境搭建指南

    一.操作系统安装 OS版本:Ubuntu 16.04 (ubuntu-16.04.5-server-amd64.iso) CPU:4Core以上 内存:4GB以上 磁盘空间:80G以上 二.基础环境准 ...

  7. 开发环境入门 linux基础 (部分)while for 函数 计划任务

    while循环 while do 动作 done 需要无限循环时我们会选择while : echo -e 格式处理,将\n的意义不变. exit 指退出执行程序 break 指跳出本层循环 conti ...

  8. 第十章 深入理解Session与Cookie

    理解Cookie 理解Session Cookie安全问题 分布式Session框架 Cookie压缩 表单重复提交问题 多终端Session统一

  9. 清除SUN服务器部件的suspect状态

    对于suspect状态的部件,可以用setchs命令清除其状态.如果ScApp的版本在5.20.15之前,需要进入service模式后才能使用setchs命令.如果ScApp版本 升级到5.20.15 ...

  10. ajax跨域请求-jsonp

    1. 同源策略 ajax之所以需要“跨域”,罪魁祸首就是浏览器的同源策略.即,一个页面的ajax只能获取这个页面相同源或者相同域的数据. 如何叫“同源”或者“同域”呢?——协议.域名.端口号都必须相同 ...