总时间限制: 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. PHP之string之str_repeat()函数使用

    str_repeat (PHP 4, PHP 5, PHP 7) str_repeat - Repeat a string str_repeat - 重复一个字符串 Description strin ...

  2. 学习并发包常用的接口----java.util.concurrent

    1.常用的相关的接口 Callable.(Runnable).Futrue.RunnableFuture.RunnableSheduledFuture.ScheduledFuture.Executor ...

  3. OC weak strong __weak __strong copy retain assign nonatomic atomic等关键字的总结

    weak和strong的区别: weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放 ,即使还有weak型指针指向它. 一旦最后一个strong型指针离去 ...

  4. 类库里面添加日志记录 log4net

    第一步: 新建一个公共类库common,添加CustomLog4jLogger.cs 并引用log4net.dll /// <summary> /// 日志记录 /// </summ ...

  5. android inflate压力泵,将视图发生整合的过程

    转自:https://blog.csdn.net/u012702547/article/details/52628453?utm_source=copy inflate方法从大范围来看,分两种,三个参 ...

  6. NSURLSession和NSURLConnection

    iOS9.0之后NSURLConnection被注销,采用NSURLSession,先介绍NSURLSession,然后介绍NSURLConnection 1.NSURLSession: post请求 ...

  7. [Telegram X]旧版分享 突破被锁群组

    Telegram X的锁群是由于 App Store 审核时发现Telegram官方并不限制18+.社会舆论等的讨论:在 版本 5.0.2 (版本号825487096)时就已经封禁该类群组 注:可能由 ...

  8. [SCOI2009]粉刷匠

    线性DP预处理+分组背包 首先设dp[i][j][0/1]表示该木板前i个格刷了j次且最后一次颜色为0/1的最大正确数 做下0/1的前缀和然后转移状态 dp[i][j][k]=max(dp[l][j] ...

  9. vue-cli新建一个项目

    零.我想把项目安装在C:\www\Arup.DAH.ABCD\SourceCode\FrontEnd这个目录下,所以在我想安装的位置,Shift+右键-->powershell窗口,打开下图位置 ...

  10. Mongodb安装详解及mongochef视图工具安装。

    按照国际惯例我们先来介绍一下MongoDB. MongoDB是一个基于分布式文件存储的数据库,由c++语言编写,为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB属于非关系数据库,也不能说 ...