总时间限制: 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. Canvas 绘制矩形,圆形,不规则图形(线条),渐变等图像效果

    绘制矩形: getContext("2d") 对象是内建的 HTML5 对象,拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. fillStyle 方法将其染成红色,fill ...

  2. 屏幕置顶(WindowManager服务)

    https://www.cnblogs.com/mythou/p/3244208.html

  3. webstorm中sass编译时目录或内容包含中文字符报错

    ruby版本:ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32] sass版本:Sass 3.4.22 (Selective Steve) ...

  4. amazeui笔记-web组件

    Json.parse()

  5. 【深入JavaScript】一种JS的继承方法

    这些天读了John Resig的<Secrets of JavaScript Ninja>,其中讨论到JS中实现继承的方案,非常有趣,自己探索了一下,形成了笔记,放到这里. 这个方案在Re ...

  6. 关于键盘KeyDown事件

    if (e.KeyValue==13) //如果键盘的值等于13 这里面的13是enter键 textBox2.Focus(); //焦点就跑到textbox2上面

  7. 网络安全之——DNS欺骗实验

        ---------------发个帖证明一下存在感,希望各位大牛们,别喷我!!谢谢--------------         DNS(域名系统)的作用是把网络地址(域名,以一个字符串的形式) ...

  8. hadoop start-all.sh报错JAVA_HOME is not set and could not be found.

    原文 错误:JAVA_HOME is not set and could not be found,可能是因为JAVA_HOME环境没配置正确,还有一种情况是即使各结点都正确地配置了JAVA_HOME ...

  9. Q:关于栈的常见问题

     对于栈,一个常见的问题是:给定一个序列a0,a1,a2,a3...an依次顺序入栈,在元素顺序入栈的过程中,栈中任意一个元素可以选择是否出栈,则其共有几种出栈的可能,给定的出栈序列中,哪种是不可能的 ...

  10. UOJ#172. 【WC2016】论战捆竹竿

    传送门 首先这个题目显然就是先求出所有的 \(border\),问题转化成一个可行性背包的问题 一个方法就是同余类最短路,裸跑 \(30\) 分,加优化 \(50\) 分 首先有个性质 \(borde ...