POJ2106 LA3094 ZOJ2483 Boolean Expressions文本处理

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2106 LA3094 ZOJ2483 Boolean Expressions文本处理相关的知识,希望对你有一定的参考价值。

Boolean Expressions
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7023 Accepted: 2446

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

Source

México and Central America 2004

问题链接POJ2106 LA3094 ZOJ2483 Boolean Expressions
问题简述:(略)
问题分析:布尔表达式求值计算问题,可以用递归子程序法和堆栈法来解决。这里用递归子程序法来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ2106 LA3094 ZOJ2483 Boolean Expressions */

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 100 + 1;
char s[N];
int k;

int expression();
int item();

int factor()     // 处理括号与值

    int result = 0;
    char op = s[k];       // 看一个字符
    if ( op == '(') 
        k++;      // 读左括号
        result = expression();	// 括号内表达式
        k++;    // 读右括号
     else if (op == 'V') 
        k++;
        result = 1;
     else if (op == 'F') 
        k++;
        result = 0;
     else if (op == '!')
        result = item();

    return result;


int item()	// 非

    char op = s[k];       // 看一个字符
    if ( op == '!') 
        k++;
        return factor() ? 0 : 1;		// 处理括号
     else
        return factor();


int expression()

    int result = item();     // 处理非
    for(;;) 
        char op = s[k];       // 看一个字符
        if (op == '\\0') break;
        else if ( op == '&' || op == '|') 
            k++;
            int value = item();
            if ( op == '&')
                result = result & value;
            else if ( op == '|')
                result = result | value;
         else
            break;
    
    return result;


int main()

    int caseno = 0;
    while(fgets(s, N, stdin) && s[0] != '\\n') 
        int j = 0;
        for (int i = 0; s[i] != '\\n'; i++)
            if (s[i] != ' ') s[j++] = s[i];
        s[j] = '\\0';
        k = 0;
        printf("Expression %d: %c\\n", ++caseno, expression() ? 'V' : 'F');
    

    return 0;

以上是关于POJ2106 LA3094 ZOJ2483 Boolean Expressions文本处理的主要内容,如果未能解决你的问题,请参考以下文章

POJ2033 LA3078 HDU1508 ZOJ2202 AlphacodeDFS+DP

POJ1323 LA2521 HDU1338 ZOJ1362 Game Prediction贪心

POJ2092 LA3157 HDU1347 ZOJ2250 Grandpa is Famous排序

UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制

ACM/ICPC 之 Floyd练习六道(ZOJ2027-POJ2253-POJ2472-POJ1125-POJ1603-POJ2607)

ZOJ1139 POJ1468 Rectangles水题