[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 --------------------- ...
随机推荐
- 技术总监Sycx的故事
其实我在各种演讲里,线下吹牛里面无数次提及过他,讲过他的故事,但是总还是没有任何一次认认真真的详细讲过,所以,今天就讲讲他的故事吧. Sycx是福建漳州人,我经常开玩笑说,你生于一个著名的骗子之乡,为 ...
- DB字段顺序与类的属性顺序一致:{Oracle.DataAccess.Client.OracleException ORA-00932: 数据类型不一致: 应为 TIMESTAMP, 但却获得 NUMBER
{Oracle.DataAccess.Client.OracleException ORA-00932: 数据类型不一致: 应为 TIMESTAMP, 但却获得 NUMBER 应用程序中类型T ...
- mybatis 学习二 MyBatis简介与配置MyBatis+Spring+MySql
1.2.2建立MySql数据库 在C:\Program Files\MySQL\MySQL Server 5.7\bin下面: 首先连接MySQL: mysql -u root -p ...
- C Primer Plus学习笔记(五)- C控制语句:循环
伪代码的概念: 伪代码是一种用简单的句子表示程序思路的方法,它与计算机语言的形式相对应.伪代码有助于设计程序的逻辑.确定程序的逻辑无误之后,再把伪代码翻译成实际的编程代码.使用伪代码的好处之一是,可以 ...
- 类型:.net;问题:C#lambda表达式;结果:Lambda表达式详解
Lambda表达式详解 前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1 ...
- ClientDataSet + DataSetProvider + FDQuery 的bug
ClientDataSet + DataSetProvider +FDQuery 有 bug ClientDataSet + DataSetProvider +ADOQuery正常. Client ...
- Delphi BLE 控件
TBluetoothLEDevice LDevice.Address;//"00:11:22:DD:EE:FF". LDevice.DeviceName//Mi LDevice.I ...
- sql server导入excel等数据
1.首先打开并登陆sql server数据库 2.选择要将表导入的数据库,右击选择任务-->导入数据 3.在弹出的窗口中选择下一步 4.在弹出的窗口中选择数据源,也就是从哪种文件导入,sql s ...
- Maven核心概念(转)
转自 https://www.cnblogs.com/xdp-gacl/p/4051819.html 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1 ...
- PHP自定义函数获取汉字首字母的方法
使用场景:城市列表等根据首字母排序的场景 function getFirstCharter($str) { if (empty($str)) { return ''; } $fchar = ord($ ...