1836 verification of Yongzhen Formula

Source: Internet
Author: User
Description

We are studying discrete mathematics this semester. discrete mathematics can be called computer mathematics. It is a basic course for many computer professional courses in the future.

Now we try to use computer programming to solve the problem of discrete mathematics. We can both review discrete mathematics and carry out programming practices, which is the best of both worlds.

Task

Compile the program to determine whether a proposition formula is a permanent formula (also known as the statement ).

The perpetual truth formula is defined as: Given a proposition formula, the true value is always true regardless of the assignment of components.

Here, we limit that there are only four types of proposition concatenation words, namely, negation (expressed by not), Union (expressed by and), and extraction (expressed by or) conditional concatenation (also known as containing concatenation, expressed by then). Each atomic proposition is represented by an uppercase English letter.

Input

One input may contain multiple groups of data. Each group of data occupies one row. End when input.

Note that there are spaces before and after each combination. If not, there can be no spaces before a line. There can be no space between an atomic proposition and parentheses.

The priority of the word combination has been fully reflected by parentheses in the input. You only need to judge the parentheses during programming.

The length of the string entered in each test case cannot exceed 1000.

Output

For each input group, if the proposition formula is a permanent formula, 1 is output; otherwise, 0 is output.

Sample Input
( not P) then (P then Q)
((P or S) and R) or ( not ((P or S) and R))
(P and Q) then M
end
Sample output
1
1
0
Simulation questions, case-based discussion
# Include "stdio. H "# include" string. H "# include" math. H "# define and 1 # define or 2 # define then 3 # define not 4 # define true 1 # define false 0 # define max_size 1100 void transform (INT start_pos, int end_pos, char formular []) {int I, j, variable [2];/* variable records the value of the proposition variable in the formula */INT flag; /* flag indicates whether the formula contains the logical operation word */INT op;/* op records the logical operation word */INT res; /* the value in the brackets */J = 0; flag = 0; for (I = start_pos; I <= end_pos; I ++) {If (formular [I] = '0' | formular [I] = '1') {Variable [J ++] = formular [I]-48 ;} if (formular [I]> = 'A' & formular [I] <= 'Z')/* encounters a logical computing term */{flag = 1; /* record the logical operation term */switch (formular [I]) {Case 'A': op = and; I + = 2; break; Case 'O ': OP = or; I + = 1; break; Case 'T': op = then; I + = 3; break; Case 'N': op = not; I + = 2; break ;}}if (FLAG)/* contains the logical operator */{Switch (OP) {Case and: res = variable [0] & Variable [1]; break; case or: res = variable [0] | variable [1]; break; CAS E then: res = (variable [0] = 1 & Variable [1] = 0 )? 0: 1); break; case not: res = (variable [0] = 1 )? 0: 1); break ;}} else {res = variable [0];} formular [start_pos] = res + 48; for (I = start_pos + 1; I <= end_pos; I ++) {formular [I] = '';} return;} int judge (char formular [])/* determines whether the formula is a permanent truth formula, true */{int search (char formular [], char f []); int get_res (char formular []); char variable [30]; /* store the true values of each variable */Char f_var [30];/* corresponding relationship */Char temp [max_size]; /* use the true value to replace the variable strings */int n, J;/* n is the number of variable propositions of the proposition formula */INT flag; long I, m Ax, temp_ I; n = search (formular, f_var); max = POW (2, n)-1; /* enumerate all cases to determine whether it is a permanent formula */for (I = 0; I <= max; I ++) {temp_ I = I; /* assign true values to each proposition in turn */For (j = N; j> = 1; j --) {Variable [f_var [J]-64] = temp_ I % 2 + 48; temp_ I = temp_ I/2;}/* replace the proposition variable */For (j = 0; formular [J]! = '\ 0'; j ++) {If (formular [J]> = 'A' & formular [J] <= 'Z')/* Subject variable, replace with the true value */{temp [J] = variable [formular [J]-64];} else {temp [J] = formular [J];} temp [J] = '\ 0';/* calculate the value of this proposition formula under the true value */flag = get_res (temp); If (flag = false) return false;} return true;} int search (char formular [], char f [])/* This function calculates the number of variable propositions of a formula, this article also describes how to save proposition variables to the ing relationships of F [] in sequence */{int I, j, Count, flag; Count = 0; for (I = 0; formular [I]! = '\ 0'; I ++) {If (formular [I]> = 'A' & formular [I] <= 'Z') {flag = 0; for (j = 1; j <= count; j ++) {If (F [J] = formular [I]) {flag = 1; /* indicates that this proposition variable has occurred and does not record */} If (! Flag) {f [++ count] = formular [I] ;}} return count ;}int get_res (char formular []) /* calculate the true value of a proposition formula that has assigned true values to each proposition variable */{void transform (INT start_pos, int end_pos, char formular []); int I, j, flag;/* flag records whether parentheses exist in the proposition formula */INT left and right;/* records the positions of left and right brackets */flag = false; left = 0; Right = 0; for (I = 0; formular [I]! = '\ 0'; I ++) {If (formular [I] =' (') {left = I;} If (formular [I] = ') ') {flag = true; Right = I; break;} while (FLAG) {transform (left + 1, right-1, formular);/* judgment, remove brackets */formular [left] = ''; formular [right] ='';/* continue to judge whether parentheses exist */flag = false; for (I = 0; formular [I]! = '\ 0'; I ++) {If (formular [I] =' (') {left = I;} If (formular [I] = ') ') {flag = true; Right = I; break ;}} transform (0, strlen (formular)-1, formular); for (I = 0; formular [I]! = '\ 0'; I ++) {If (formular [I] = '1') {flag = true; break ;} else if (formular [I] = '0') {flag = false; break;} return flag;} int input (char formular []) /* processing Input Function */{gets (formular); If (formular [0] = 'E') return true; return false;} int main (void) {char formular [max_size]; int end; end = input (formular); While (END = false) {printf ("% d \ n ", judge (formular); end = input (formular);} return 0 ;}

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.