POJ 2106 Boolean Expressions

Source: Internet
Author: User


Boolean Expressions
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3665 Accepted: 1104

Description

The objective of the program, going to produce was to evaluate Boolean expressions as the one shown next:
Expression: (V | V) & F & (F | V
Where V is for True, and F are for False. The expressions may include the following operators:! For no, & for and, | For or, the use of parenthesis for operations grouping is also allowed.

To perform the evaluation of an expression, it'll be considered the "the" the operators, the not have the highes T, and the or the lowest. The program must yield V or F, as the result of each expression in the input file.

Input

The expressions is of a variable length, although'll never exceed. Symbols May is separated by any number of spaces or no spaces at all, therefore, the total length of a expression, as a n Umber of characters, is unknown.

The number of expressions in the input file is variable and would never be greater than 20. Each expression was 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 Corres ponding test expression. Separate the output for consecutive test expressions with a new line.

Use the same format as, shown in the sample output shown below.

Sample Input

(V | V) & F & (f| V)! X 2 V & V &! F & (F | V) & (! F | F |! V & V) (f&f| v|! v&! f&! (f| F&V))

Sample Output

Expression 1:fexpression 2:vexpression 3:v

Source

México and central America 2004ACcode:

#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int sum[110],poss;int op[        110],poso;void Insert (int t) {while (poso&&op[poso-1]==3) {t=!t;    --poso; } sum[poss++]=t;}    void Calc () {int B=sum[--poss];    int A=sum[--poss];    int O=op[--poso];    int c= (A&AMP;B);    if (o==1) c= (a|b); Insert (c);}    int main () {int loop=1;    char c;        while ((C=getchar ())!=eof) {poss=poso=0;            do{if (c== ' (') {op[poso++]=0;                    }else if (c== ') ') {while (poso&&op[poso-1]!=0) calc ();                --poso;            Insert (Sum[--poss]); }else if (c== '! ')            {op[poso++]=3;                }else if (c== ' & ') {while (poso&&op[poso-1]>=2) calc ();            op[poso++]=2; }else if (c== ' | ')   {while (poso&&op[poso-1]>=1) calc ();             Op[poso++]=1; } else if (c== ' V ' | |            c== ' F ') {insert (c== ' V '? 1:0);        }}while ((C=getchar ())! = ' \ n ' &&c!=eof);        while (Poso) calc (); printf ("Expression%d:%c\n", loop++, (sum[0]? ')    V ': ' F ')); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2106 Boolean Expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.