/* Program to interpretively evaluate a Boolean expression */ /* Whitespace can be inserted anywhere, and is ignored. Each expression is terminated by a period. */ /* Variables A, B, C, and D are initialized to hold FF00, F0F0, CCCC, and AAAA respectively. That produces row i of a truth table in bit position i, for variables in the order ABCD. The result is 4 hexadecimal digits, the truth table column of the expression. */ #include int trm,val, num, s; int vallet[4]={0xFF00, 0xF0F0, 0xCCCC, 0xAAAA }; char tr[256]; void init() { int i; for (i=0; i<256; i++) tr[i]=0; for (i='A'; i<='D'; i++ ) tr[i]=1; for (i=0; i<6; i++) tr["01~+*."[i]] = i+2; tr['\n']= tr['\t']= tr[' ']=8; trm = 0xffff; num = 0; s=val = 0; } void term() { if (s) num = ~num; trm &= num; s=num = 0; } void exp() { term(); val |= trm; trm = 0xffff; } void main() { char c; init(); while ((c=getchar())!='@') { switch ( tr[c] ) { case 1: num = vallet[c-'A']; break; case 2: num = 0; break; case 3: num = 0xffff; break; case 4: s = 1-s; break; case 5: exp(); break; case 6: term(); case 8: break; case 7: exp(); printf("%04X\n",val); val = 0; break; default: printf("illegal char\n"); } } }