[poj 2106] Boolean Expressions 递归
Description
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 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
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 递归的更多相关文章
- POJ 2106 Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB 描述 The objective of the program you are going to produce is to evaluate ...
- (栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)
/* * POJ_2106.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...
- POJ 2106 Boolean Expressions (布尔表达式求值)
题意:关于!,&,| 的运算,表达式中V代表true,F代表false. 思路:见代码吧,很详细了. 要注意 !!!F,!(...) 的情况. #include <iostream> ...
- poj 2106 Boolean Expressions 课本代码
#include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(in ...
- Boolean Expressions POJ - 2106 (表达式求值)
The objective of the program you are going to produce is to evaluate boolean expressions as the one ...
- POJ | Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB 描述The objective of the program you are going to produce is to evaluate ...
- Boolean Expressions
Boolean Expressions Time Limit: 1000MS Memory Limit: 30000K Description The objective of the ...
- poj 3295 Tautology 伪递归
题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...
- shorthand trick with boolean expressions
https://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean --------------------- ...
随机推荐
- Fiddler 使用技巧
1.Host重定向,将192.10.11.12:8091的地址重新定向到127.0.0.1:8080 if (oSession.host=="192.10.11.12:8091") ...
- VisualGDB系列8:使用VS创建CMake Linux项目
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用VS来创建.构建.调试一 ...
- Angular常犯的错误
ng-app="name名称" name名称 == 一定要写对 其次 angular.min导入一定要正确,一定要导入正确的angular.min的库 再次js中要写自调用 (f ...
- Python多进程-进程间数据的共享
不同的进程不能同时修改一份数据,但是不同的进程能对一份数据进行修改 可通过Manager来实现进程间的数据共享 # -*- coding:utf-8 -*- __author__ = "Mu ...
- 如何解决SSH登录Solaris主机速度慢的问题
SSH登录速度慢可能有多种原因. 1. 与DNS有关 缺省情况下,当客户端用SSH登录solaris服务器时,服务器会试图反向解析客户端的IP 地址(即把IP地址解析成机器名).如果Solaris系统 ...
- Android 4学习(4):概述 - Using Resources
参考:<Professional Android 4 Application Development> Andorid中的资源包括用户自定义资源和系统自带资源,这两种资源既可以在代码中使用 ...
- Git分支和指针
Git中的分支本质上是个指向commit对象的指针. 在当前commit点创建一个新的分支test git branch test 创建了一个新的可变指针指向f30ab commit Git 是如何 ...
- 转载-你应该知道的 RPC 原理
在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...
- nginx负载均衡, 配置地址带端口
nginx.conf 配置如下: upstream wlcf.dev.api { server 127.0.0.1:8833; server 127.0.0.2:8833; } server { l ...
- 高性能MySQL笔记-第4章Optimizing Schema and Data Types
1.Good schema design is pretty universal, but of course MySQL has special implementation details to ...