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. IE版本的标准定义

    解决方案 首页加代码把IE浏览器的标准改了,无论客户用的什么IE,都是按照IE7的标准来的. <meta http-equiv="X-UA-Compatible" conte ...

  2. java代码输出质因数

    package com.badu; import java.util.Scanner; //分解质因数问题: //从键盘输一个数, //首先最小质因数为2 //n不能被2整除时, //n能被2整除时, ...

  3. 2015 浙江省赛 H - May Day Holiday

    H - May Day Holiday As a university advocating self-learning and work-rest balance, Marjar Universit ...

  4. canvas图像以及剪切

    图像篇: 代码: 1 /** 2 * Created by Administrator on 2016/1/28. 3 */ 4 function draw (id){ 5 var canvas = ...

  5. 侯捷STL学习(三)--分配器测试

    第七节:分配器测试 标准的分配器Allocator,#include<ext/...>都是拓展的 可以用不同的分配器测试同一容器 分配器allocate() & deallocat ...

  6. 环境搭建:Vue环境搭建和项目初始化(windows)

    1.    安装node.js 官网下载安装:https://nodejs.org/en/ 版本查看:node -v 注意:node版本最好新一点好,推荐6.0以上. 2.    npm安装webpa ...

  7. 调试json

    console.log("======================") // 转对象 //var obj = eval('(' + data + ')'); // 转对象 // ...

  8. react过渡动画效果的实现,react-transition-group

    本文介绍react相关的过渡动画效果的实现 有点类似vue的transition组件,主要用于组件mount和unmount之前切换时应用动画效果 安装 cnpm install react-tran ...

  9. java连接字符串操作,可用来向sql传值

    private static String concat(String tag,String Time) { // TODO Auto-generated method stub // return ...

  10. 关于mybatis和spring复合pom的异常

    java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...