[poj 2106] Boolean Expressions 递归

Description

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.

Input

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.

Output

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.

Sample Input

( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))

Sample Output

Expression 1: F
Expression 2: V
Expression 3: V

题目链接:http://poj.org/problem?id=2106

题意:

  求逻辑表达式的值,真或假,可能会含有若干的空格。

思路:

  优先级:! > && > ||  逻辑表达式也同样符合表达式的定义,表达式是由若干的项“||”操作组成, 项是由若干个因子通过“&&”操作组成, 因子由单个F或V组成,也可由带括号的表达式组成。"!"操作同样作为因子的一部分。最后按照定义递归进行即可。

代码:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;

char ep[110];
int k;

int factor_value()
{
    int expression_value(void);
    int ret = 0;
    int flag = 0;
    while (ep[k] == ‘!‘) {
        k++;
        flag = ~flag;
    }
    if (ep[k] == ‘(‘) {
        k++;
        ret = expression_value();
        k++;
    }
    else {
        if (ep[k] == ‘F‘) {
            k++;
            ret = 0;
        }
        else if (ep[k] == ‘V‘){
            ret = 1;
            k++;
        }
    }
    if (flag) return !ret;
    else return ret;
}

int term_value()
{
    int ret = factor_value();
    while (1) {
        if (ep[k] == ‘&‘) {
            k++;
            ret = factor_value()&&ret;  //同下解释
        }
        else break;
    }
    return ret;
}

int expression_value()
{
    int ret = term_value();
    while (1) {
        if (ep[k] == ‘|‘) {
            k++;
            ret =  term_value() || ret;
        }    //term_vaue()一定要写到前面,否则ret为真,term_value就不进行了
        else break;
    }
    return ret;
}

int main()
{
    //freopen("1.txt", "r", stdin);
    char ori[100001];
    int t = 0;
    while (cin.getline(ori, 100000)) {
        int j = 0;
        for (int i = 0; ori[i]; i++) {
            if (ori[i] != ‘ ‘)
                ep[j++] = ori[i];
        }
        ep[j] = ‘\0‘;
        //cout << ep << endl;
        k = 0;
        printf("Expression %d: ", ++t);
        if (expression_value())
            printf("V\n");
        else
            printf("F\n");
    }

    return 0;
}

  

时间: 2024-08-29 21:30:38

[poj 2106] Boolean Expressions 递归的相关文章

poj 2106 Boolean Expressions 课本代码

#include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(int b) { while(otop &&op[otop-1]==3) { b=!b; --otop; } val[vtop++]=b; } void calc(void) { int b=val[--vtop]; int a=val[--vtop]; int opr=op[--otop]; i

POJ 2106 Boolean Expressions

Boolean Expressions Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3665   Accepted: 1104 Description The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next: Expression: ( V | V ) &

POJ 2106 Boolean Expression 表达式求值

题意:给出布尔表达式求值? 插入数字时,若有!则更新.遇到右括号弹出知道左括号,左括号前有'!'则更新, 其余和中缀表达式一样,遇到下一个运算符时 若操作栈中运算符优先级大,则先算. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <s

005:Boolean Expressions

描述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: !

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 includ

poj 3295 Tautology 伪递归

题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, E 分别表示且,或,非,真蕴含,等值.问表达式是不是永真的,如果是输出“tautology”,否则输出“not”. 解题思路: 这里借用到了递归的本质,也就是对栈的模拟,用递归进行压栈,求表达式的值,再加上对变量状态压缩进行枚举. 1 #include <cstdio>//本代码用G++交就ac,

poj 2106

原题链接:http://poj.org/problem?id=2106 题意:或.与. 非的多元表达式的求值: 思路:中缀表达式变为后缀表达式: 代码: 1 #include<cstdio> 2 #include<iostream> 3 #include<stack> 4 #include<string> 5 6 using namespace std; 7 8 struct Pri{ 9 char op; 10 int pri; 11 }lpri[5]={

POJ 1780 【手工递归】【欧拉回路】

题意: 1.提供密码的位数. 2.密码的输入可以一直保持,取后n位作为密码.如果密码正确则开锁. 3.设计一种方法使得在输入最少的情况下破译.(即保证每个密码只输入一次) 4.输出输入的数字的序列. 思路: 去密码的前n-1位作为状态节点,将n位数密码作为边.建造有向图. 显然,每个点的入度和出度都为10,则一定存在欧拉回路. 利用简单DFS寻找欧拉回路. (这题好像是要求输出字典序最小的序列) DFS应该不难写,但是这题如果直接递归会爆栈.所以需要手工用栈模拟递归的过程... 屌丝看了大神的关

poj 1664 放苹果 递归

题目链接: http://poj.org/problem?id=1664 题目描述: 有n个苹果,m个盒子,盒子和苹果都没有顺序,盒子可以为空,问:有多少种放置方式? 解题思路: 当前有n个苹果,m个盒子. (1):假设当前最少的盒子放置一个苹果,则给m个盒子分别放一个苹果,剩下n-m个苹果. (2):假设当前最少的盒子不放苹果,则剩m-1个box,n个苹果. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <c