总时间限制: 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. 何为cookie?

    何为cookie HTTP Cookie(也叫Web Cookie或浏览器Cookie)是服务器发送到用户浏览器并保存在本地的一小块数据,它会在浏览器下次向同一服务器再发起请求时被携带并发送到服务器上 ...

  2. EF4.4增删改查实例

    第一.先创建一个名为Store数据库,将下面脚本代码执行创建表: USE [Store] GO /****** Object: Table [dbo].[Category] Script Date: ...

  3. cnblog博客停用

    本博客从今日起停止更新,后续的文章将会发布在新的博客mrbackkom.github.io

  4. Perl入门

    Perl 是一门开源的脚本语言,由 Larry Wall 所创造,该语言以实用,快速开发为主要目标,与当前流行的面向对象结构化编程有些格格不入,但这并不妨碍 Perl 被广泛流传和使用,世界范围内围绕 ...

  5. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  6. Java泛型的协变

    在上篇<Java泛型的基本使用>这篇文章中遗留以下问题,即将子类型也能添加到父类型的泛型中,要实现这种功能必须借助于协变. 实验准备 现在在上篇文章展示的Decorator类型的基础上,增 ...

  7. 阿里巴巴Java开发规约插件使用

    10月14日上午9:00 阿里巴巴于在杭州云栖大会<研发效能峰会>上,正式发布<阿里巴巴Java开发手册>扫描插件,该插件在扫描代码后,将不符合<手册>的代码按Bl ...

  8. [javaSE] GUI(对话框Dialog)

    对话框不能单独存在,依赖于窗体,有显示标题,有模式 获取Dialog对象,new出来,构造参数:Frame对象,String的标题,模式 窗体内部的内容,Label对象,Button对象,调用Dial ...

  9. LeetCode刷题第一天

    1 . 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用 ...

  10. BZOJ1812: [Ioi2005]riv(树形dp)

    题意 题目链接 Sol 首先一个很显然的思路是直接用\(f[i][j] / g[i][j]\)表示\(i\)的子树中选了\(j\)个节点,该节点是否选的最小权值.但是直接这样然后按照树形背包的套路转移 ...