总时间限制: 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解析过程,据此我们可以写实现代码。

Grammar:
  expression:
    primary
    primary "&" primary
    primary "|" primary
  primary:
          primary
    "!" primary
    "V"
    "F"
    "(" expression ")"
 
实现代码
 #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 ;
}
测试数据
输入
  F &!F | !V|!V & !F& !( F | F& (V | !F| !V| V& (V|F)))
  F& !F|!V|!F& (V|F| !V) & !F&!(F|F& (V | !F|!V| V &(V| F) ))
  F&!F | !V| !F& !!!( V|F| !V) & !F&!(F|F& (V|!F|!V|V&(V|F)))
  !V|!F&! (V|(F | V )| !V)& !F& !!(F| F& (V|!F|!V|V&( V| F )))
  (F|(F&V)|!V)&!F&! (F|(F&( V| !F|!V |V& (V |F)) ))
  V
  (((V)))
  ((!(F)))
  (!(!( !(F ))))
  !!F|!F&!(V|(F|V)|!V)&!F&!!(F|F&(V|!F|!V|V&(V|F)))|((!(F)))
输出
  Expression 1: F
  Expression 2: V
  Expression 3: F
  Expression 4: F
  Expression 5: F
  Expression 6: V
  Expression 7: V
  Expression 8: V
  Expression 9: V
  Expression 10: V

POJ | Boolean Expressions的更多相关文章

  1. [poj 2106] Boolean Expressions 递归

    Description The objective of the program you are going to produce is to evaluate boolean expressions ...

  2. Boolean Expressions POJ - 2106 (表达式求值)

    The objective of the program you are going to produce is to evaluate boolean expressions as the one ...

  3. POJ 2106 Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述 The objective of the program you are going to produce is to evaluate ...

  4. POJ 2106-Boolean Expressions,双栈运用类似表达式求值!

    Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...

  5. Boolean Expressions

    Boolean Expressions Time Limit: 1000MS   Memory Limit: 30000K       Description The objective of the ...

  6. (栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)

    /* * POJ_2106.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  7. POJ 2106 Boolean Expressions (布尔表达式求值)

    题意:关于!,&,| 的运算,表达式中V代表true,F代表false. 思路:见代码吧,很详细了. 要注意 !!!F,!(...) 的情况. #include <iostream> ...

  8. poj 2106 Boolean Expressions 课本代码

    #include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(in ...

  9. shorthand trick with boolean expressions

    https://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean --------------------- ...

随机推荐

  1. 嵌套div的margin-top不生效

    之前遇到过嵌套div设置margin-top不生效的情况,一直没去研究,今天又遇到了,查阅资料得知原因,并不是margin-top没生效,只是当外层容器跟内层容器之间没有别的元素的时候,内层容器的ma ...

  2. 原生JS实现旋转轮播图+文字内容切换

    废话不多说,直接上图看效果: 需求:点击左右按钮实现切换用户图片与信息: 原理:点击右侧左侧按钮,把3号的样式给2号,2号的给1号,1号的给5号,5号的给4号,4号的样式给3号,然后根据现在是第几张图 ...

  3. IOS8添加启动图

    在IOS8之后,可以用pdf矢量图添加启动图,昨天下班时没来得及弄,今天早上来试了下. 1.Images.xcassets中添加New Launch Image,并命名为Launch Screen,之 ...

  4. RabbitMQ---9、消息确认机制(事务+Confirm)

    转载至:https://blog.csdn.net/u013256816/article/details/55515234 参考资料:https://www.cnblogs.com/520playbo ...

  5. MongoDB的基本使用及java对MongoDB的基本增删改查

    MongoDB的特点 MongoDB 是文档存储数据库,存储结构灵活 MongoDB 支持复杂查询操作.支持序列 MongoDB 采用C++开发,可以做分布式扩展 MongoDB 采用BSON格式存储 ...

  6. 【转】redis windows环境搭建

    一.下载redis windows压缩包 地址参考: https://github.com/ServiceStack/redis-windows/tree/master/downloads https ...

  7. HDFS学习

    HDFS体系结构 HDFS采用了主从(Master/Slave)结构模型,一个HDFS集群包括一个名称节点(NameNode)和若干个数据节点(DataNode)(如图所示).名称节点作为中心服务器, ...

  8. 线性规划费用流解法(Bzoj1061: [Noi2008]志愿者招募)

    题面 传送门 Sol 线性规划费用流解法用与求解未知数为非负数的问题 这道题可以列出一堆形如 \(x[i]+x[j]+x[k]+...>=a[p]\) 的不等式 我们强行给每个式子减去一个东西, ...

  9. Tomcat的学习和使用(一)

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  10. drupal7 自定义登录&找回密码页面,注意事项

    1.登录页面的 $form['form_id'] 和 $form['form_build_id'],是这样输出的: <?php print drupal_render($form['form_i ...