POJ | Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
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.
样例输入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
样例输出
Expression 1: F
Expression 2: V
Expression 3: V
解题思路
此题目可以通过递归的方式来做,下面是根据题意分析的Grammar解析过程,据此我们可以写实现代码。
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> char str[] = {'\0'};
int idx = ;
bool expression(); bool primary()
{
char op = str[idx];
bool result;
if (op == '(')
{
idx++; //(
result = expression();
idx++; //)
}
else if (op == 'V')
{
result = true;
idx++;
}
else if (op == 'F')
{
result = false;
idx++;
}
else if (op == '!')
{
idx++;
result = !primary();
}
return result; } bool expression()
{
bool result = primary();
while (true)
{
char op = str[idx];
if (op == '&' || op == '|')
{
idx++;
bool value = primary();
if (op == '&')
{
result &= value;
}
else
{
result |= value;
}
}
else
{
break;
}
}
return result;
} bool getline_ns(char *str, int max)
{
bool ret = false;
char *s = (char *)malloc(sizeof(char) * max * );
if (fgets(s, max * , stdin))
{
int i = , j = ;
for (; s[i] != '\0'; i++)
{
if (s[i] != ' ')
str[j++] = s[i];
}
str[j++] = '\0';
ret = true;
}
free(s);
return ret;
}
int main()
{
int i = ;
while (getline_ns(str, ))
{
idx = ;
printf("Expression %d: %c\n", ++i, expression() ? 'V' : 'F');
}
return ;
}
POJ | Boolean Expressions的更多相关文章
- [poj 2106] Boolean Expressions 递归
Description The objective of the program you are going to produce is to evaluate boolean expressions ...
- Boolean Expressions POJ - 2106 (表达式求值)
The objective of the program you are going to produce is to evaluate boolean expressions as the one ...
- POJ 2106 Boolean Expressions
总时间限制: 1000ms 内存限制: 65536kB 描述 The objective of the program you are going to produce is to evaluate ...
- POJ 2106-Boolean Expressions,双栈运用类似表达式求值!
Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...
- Boolean Expressions
Boolean Expressions Time Limit: 1000MS Memory Limit: 30000K Description The objective of the ...
- (栈的应用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 ...
- shorthand trick with boolean expressions
https://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean --------------------- ...
随机推荐
- 剑指offer(41-45)编程题
41.入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. class Solution { public: vector&l ...
- 14 线程间协作的两种方式:wait、notify、notifyAll和Condition
原文链接:http://www.cnblogs.com/dolphin0520/p/3920385.html 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者- ...
- 【LeetCode题解】2_两数相加
目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...
- 为什么在大多数OS中都引入”打开“这一文件系统调用?打开的含义是什么?
当用户要求对一个文件实施多次读/写或者其他操作时,每次都要从检索目录开始.为了避免多次重复检索目录,在大多数OS中都引入了”打开“这一文件系统调用,当用户第一次请求对某文件进行操作时,须先利用open ...
- [PY3]——IO——文件读写
文件打开和关闭 # 使用open 打开文件,返回时值是一个 File-like对象 f.open('/test/file') # 使用read读取文件 f.read( ) # 使用close关闭文件 ...
- Android studio--几个生成文件的区别:Make Project、Make Module、Build apk、Signed apk
参考资料: https://stackoverflow.com/questions/35334319/difference-between-make-project-make-module-app-b ...
- 解决angular-deckgrid高度不均衡和重加载的问题
在项目中使用angular-deckgrid+ng-infinite-scroll实现瀑布流的无限加载.但是实际测试中发现deckgrid有2个比较严重影响体验的BUG: 每次添加新的card,整个d ...
- SearchRequestBuilder常用方法说明
SearchRequestBuilder常用方法说明 (1) setIndices(String... indices):上文中描述过,参数可为一个或多个字符串,表示要进行检索的index: (2) ...
- 自定义ContentTypeHandler在struts2-rest-2.3.16.2不生效
需要使用自定义的ContentHandler格式化json中的时间类型为指定模式. 在struts.xml中增加了自定义的ContentHandler,但不会生效. http://blog.csdn. ...
- 可变参数的lambda表达式
delegate int mydelegate(params int[] a); class Program { static void Main(string[] args) { //接收可变参数的 ...