#include
#define stack_size_normal 100 # Define Bianliang_max 20 # define str_max 60
INT zuhe [bianliang_max]; // Value combined array definition; int N; // variable number;
/ / According to the node definition of the binary tree established in the expression; typef struct btdnode {char data; struct btdnode * lchild; struct bitdnode * rchild;} * bitree;
// Identify the stack definition used by the expression, which stores the structure of the tree; typef struct lnode_optr {struct btdnode ** base; / / The elements in the stack are the node structure of the tree; struct btdnode ** Top; int STACKSIZE;} Sqstack;
// Various value combinations for generating variables;
Void Creatzuhe (int N) {INT I, NUM = 0, J = 0, E; int Temp [BianLiang_max]; for (i = 0; I } J = j-1; Num = n-num; while (j> = 0) {E = Temp [J -]; zuhe [Num ] = E;}} / / The molecular tree function is established according to the operator priority from the operator; when the logical expression is read, the root zigen is a complete binary tree INT K = 0; // Building a tree sign, k = 1 means For the first time, the molecular tree is established. It is necessary to handle the pointer domain to the left and right children. Void Create (Bitree & Zigen, Bitree L, Bitree R) {zigen-> lchild = L; zigen-> rchild = r; // Tree link IF ( L && r) {if (int (l-> data)> = 65 && int (l-> data) <= 90) {l-> lchild = null; l-> rchild = null;} if (INT (INT (R-> Data) > = 65 && int (r-> data) <= 90) {r-> lchild = null; r-> rChild = null;} }} // The priority discrimination of logical operators; char lie, char hang {Int i, j; char bijiao [7] [7] = {',' | ',' & ',' ~ ',' (',') ',' # ',' | ','> ',' <',' <',' <','> ','> ',' & ','> ', '>', '<', '<', '>', '>', ','> ',' <','> ','> ',' ',' <',' <',' <',' <',' = ',' ',') ','> ','> ','> ',' ','> ','> ',' # ',' <',' <',' <',' <',' ',' = '}; For (i = 0; i <7; i ) IF (Bijiao [0] [i ] == LIE) Break; for (j = 0; j <7; j ) IF (bijiao [j] == hang) Break; return bijiao [j] [i];} // Targeter And variable stack operation; Void Creatstack (SQSTACK & ST) {st.base = (Bitree * ) malloc (stack_size_normal * sizeof (btdnode)); if (! st.base) exit (0); st.top = st.base; st.stacksize = stack_size_normal;} void Push (Sqstack & ST, Bitree E) {IF (st.top-st.base // Remarkary identification function; Void Creattree (char s [】, Bitree & Tree) {SQSTACK VARIABLE; / / Variable Stack; Sqstack Logic; // Logic Operator Stack; Creatstack (Variable); Bitstack Logic_Di, Variables, Logics, E, A, B, THTA, KUOHAO; / / The elements in the stack; // t Thet is the root of the last binary tree; logic_di = (Bitree) Malloc (sizeof (btdnode); if (! Logic_di) EXIT (0); logic_di-> data = '#'; push (logic, logic_di); while (* s! = null) {if (int (* s)> = 65 && int (* s) <= 90) {Variables = (bitree) malloc (btdnode)); if (! Variables) exit (0); variables-> data = * s; push (variable, variables);} else if (int (* s)> 90 || int (* s) <65) {Gettop (Logic, E); // Upset Operator Stack Stack Top Element Performs Priority Comparison Switch (Youxianji (* S, E-> Data) {CASE '<' : / / The operator priority in the top of the stack is low, the logical operator is in the stack logics = (bitree); if (! Logics) EXIT (0); logics-> data = * s; push ( Logic, Logics; Break; Case '=': // Removed parent and accept the next character; POP (Logic, Kuohao); Break; Case '>': POP (Logic, THETA); // Pop-up logic operator POP (variable, a); / / Pop-up variable B = NULL; if (Theta-> Data! = ') POP (variable, b); // Building a function call k = k 1; Create (Theta, B, A); Push (variable , theta); // Press the temporary root as a new variable into the variable stack; if (* s! = '#' && * s! = ')') {logics = (Bitree) Malloc (Sizeof (btdnode) ); If (! Logics) exit (0); logics-> data = * S; push (logic, logics);} else s = s-1; break;}} s ;} Tree = Theta;} // Value of variables and uses logical expressions to evaluate int value_tree (Bitree Tree) {if (! Tree) Return 0; // encounter empty nodes; Else IF (Tree-> Data! = '|' && Tree-> Data! = '&' && Tree-> Data! = '~') // Find the variable; return zuhe [int (Tree-> Data) -65]; Else IF (INT (Tree- > data) <65 || int (Tree-> DATA)> 90) // Found is the operator; switch (Tree-> DATA) {CASE '|': return (value_tree (Tree-> Lchild) || value_tree (Tree-> rchild); Case '&': Return (Value_TR EE (Tree-> Lchild) && value_tree (Tree-> rchild); Case '~': return (! value_tree (tree-> rchild);} } // User sets a value of a variable; void user () {INT i; cout << "Please enter your change in the value" << Endl; for (i = 65; i <65 N) ; i ) {cout << CHAR (i) << "="; cin >> zuhe [i-65];}} void main () {char str [str_max], string [str_max], * pstr; int loop = 20, Choice, I = 0, Choose, SUM; Bitree Tree; While (LOOP) {PSTR = Str; I = 0; int sum = 0, L; // Logical expression for each combination for accumulating variables The result of the logical expression category, according to the number of variables of the logical expression, "<< endl; cin >> N; SUM = INT (POW (2, N)); // Total number of variables; cout << "Please enter a logical expression expression (or use '|', with '&' and non-use '~')" << endl; cin >> str; // Resident Correct reading; for (; * pstr! = Null; pstr ) if (* pstr! = '') String [i ] = * pstr; string [i] = '#'; string [i 1] = '/ 0'; cout << "************ Please select the action you want **********" << endl; cout << "**** ******** 1 Logical expression (not shown) ********** "<< endl; cout <<" ************** ****** 2 logical expression (and display the result of various value combinations) ********** "<< endl; cout <<" ******** **** 3 logical expression evaluation (according to user value) ********** "<< endl; cout <<" Please select the action you want: "; CIN >> Choose Switch (choose) {case 1: // Different combinations of variables call the evaluation function of the auxiliary binary tree in turn; ; For (loop = 0; Loop <"Logical expression:" << String << "is neither a relief, nor a contradictory" << Endl; Break; Case 2: Creattree (String, Tree); // Establish a two-fork tree; COUT << "Logical expression variable combination budget results" << endl; cout <<<-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------- "<< Endl; Printf (" | "); for (L = 65; L <65 N; L ) Printf ("% - 4c ", L ); Printf ("Logical expression value"); Printf ("| / n"); cout << "------------------------ --------------------- << endl; for (loop = 0; loop