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 --------------------- ...
随机推荐
- 《Think Python》第6章学习笔记
目录 6.1 返回函数值(Return values) 6.2 增量式开发(Incremental development) 6.3 组合(Composition) 6.4 布尔函数(Boolean ...
- 跟着Nisy一起学习C语言
编辑器是使用环境turboc的IDE,使用dos窗口中的edit作为编辑器,有点类似于vim:使用的是xp-sp3的虚拟机上的系统. Nisy说要有两种语言,脚本语言以及一个底层语言,比如现在我的py ...
- 【译】Steve Yegge的文章《Practicing Programming》
与你所相信的恰恰相反,单纯地每天埋头于工作并不能算是真正意义上的锻炼——参加会议并不能锻炼你的人际交往能力:回复邮件并不能提高你的打字水平.你必须定期留出时间,集中锻炼,这样才能把事情做得更好.我认识 ...
- 一文看懂大数据的技术生态圈,Hadoop,hive,spark都有了
一文看懂大数据的技术生态圈,Hadoop,hive,spark都有了 转载: 大数据本身是个很宽泛的概念,Hadoop生态圈(或者泛生态圈)基本上都是为了处理超过单机尺度的数据处理而诞生的.你可以把它 ...
- 浅谈arguments与arguments的妙用
1.每个函数都有一个arguments属性,表示函数的实参集合,这里的实参是重点,就是执行函数时实际传入的参数的集合. 2.arguments不是数组而是一个对象,但它和数组很相似,所以通常称为类数组 ...
- [SCOI2009]粉刷匠
线性DP预处理+分组背包 首先设dp[i][j][0/1]表示该木板前i个格刷了j次且最后一次颜色为0/1的最大正确数 做下0/1的前缀和然后转移状态 dp[i][j][k]=max(dp[l][j] ...
- 创建一个Dribbble的作品展示
Most designers on dribbble have a personal portfolio website that usually consists of a name and a b ...
- HTML 的特殊字符转换转义符,的两种方法。
HTML 的特殊字符转换转义符,的两种方法. 方法一: function htmlEscape(str) { return String(str) .replace(/&/g, '&' ...
- java多线程中的死锁、活锁、饥饿、无锁都是什么鬼?
死锁.活锁.饥饿是关于多线程是否活跃出现的运行阻塞障碍问题,如果线程出现了这三种情况,即线程不再活跃,不能再正常地执行下去了. 死锁 死锁是多线程中最差的一种情况,多个线程相互占用对方的资源的锁,而又 ...
- 微信小程序-movable-view
<view class="page-body"> <view class="page-section"> <view class= ...