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.
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 ;}