TRUTH TABLE
(C program)

This program creates truth table for the given Boolean expression. Checking of logical equivalence of two expressions and Interactive inputting with syntax checking facility are available.
/* ##################################### # TRUTH TABLE 1.0 # # tested in Turbo C++,version 3.0 # ##############18/2/2008############## */ #include<math.h> #include<conio.h> #include<ctype.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #define XPRSN_LEN 79 int i, j, curXY, TO; /* Total Operands */ char xprsn[2*XPRSN_LEN], xprsnCopy[2*XPRSN_LEN], var[26]; char *msg[]={ "enter an expression", "operand not found", "operator not found", "extra openning bracket", "extra closing bracket", "more than 18 variables" }; /******************* gets xprsn *******************/ void SetCurser(int NoOfCell) { curXY+=NoOfCell; if(curXY<0 || curXY>strlen(xprsn)) curXY-=NoOfCell; } void PutXprsn() { int i=0; gotoxy(1,3); do { switch(xprsn[i]) { case '(': case ')': textcolor(RED); break; case '~': textcolor(GREEN); break; case '+': case '.': case '>': case '=': textcolor(BROWN); break; default : textcolor(WHITE); } cprintf("%c",xprsn[i]); }while(xprsn[i++]!=NULL); } void AddChar(char ch) { int i; if(strlen(xprsn)==XPRSN_LEN) return; for(i=strlen(xprsn); i>=curXY; i--) xprsn[i+1]=xprsn[i]; xprsn[curXY]=ch; curXY++; PutXprsn(); } void AddDel() { int i; if(curXY==strlen(xprsn)) return; for(i=curXY; i<strlen(xprsn); i++) xprsn[i]=xprsn[i+1]; PutXprsn(); } void AddBksp() { if(curXY==0) return; curXY--; AddDel(); } void GetXprsn() { char ch; while(1) { gotoxy(1+curXY,3); ch=getch(); switch(ch) { case 27 : exit(0); /* Escape */ case'\r': return; /* Enter */ case'\b': AddBksp(); continue; /* Backspace */ case 71 : SetCurser(-curXY); continue; /* Home */ case 75 : SetCurser(-1); continue; /* left */ case 77 : SetCurser( 1); continue; /* right */ case 79 : SetCurser(strlen(xprsn)-curXY); continue; /* right */ case 83 : AddDel(); continue; /* End */ case '~': case '+': case '.': case '>': case '=': case '(': case ')': AddChar(ch); continue; default : if(islower(ch)) AddChar(ch); } } } /******************* syntax checking *******************/ void RecognizeVar() { int i; char ch; TO=0; for(ch='a'; ch<='z'; ch++) { for(i=0; xprsn[i]!=NULL; i++) { if(xprsn[i]==ch) { var[TO++]=ch; break; } } } } int SyntexChecking() { int bracketCheck, openningBracket=0, closingBracket=0; curXY=0; while(1) { do { while(xprsn[curXY]=='(') openningBracket++, curXY++; bracketCheck=0; while(xprsn[curXY]=='~') bracketCheck=1, curXY++; }while(bracketCheck==1); if(islower(xprsn[curXY])) curXY++; else return 1; while(xprsn[curXY]==')') { if(++closingBracket>openningBracket) return 4; else curXY++; } switch(xprsn[curXY]) { case'\0': if(openningBracket>closingBracket) return 3; else { RecognizeVar(); if(TO>18) return 5; else return 0; } case '+': case '.': case '>': case '=': curXY++; continue; default : return 2; } } } /******************* postfix notation *******************/ void Unary2Binary() { int i=0, j=0; strcpy(xprsnCopy,xprsn); do { if(xprsnCopy[i]=='~') xprsn[j++]='@'; xprsn[j++]=xprsnCopy[i]; }while(xprsnCopy[i++]!=NULL); } void Binary2Unary() { int i=0, j=0; for(i=0; i<=strlen(xprsn); i++) { if(xprsn[i]=='@') continue; else xprsn[j++]=xprsn[i]; } } void postfix(); int Priority(char char1, char char2, char char3) { if(char2=='(') { i+=2; postfix(); return Priority(char1,'?',xprsn[i]); } if(char2!='?') i+=2, xprsn[j++]=char2; if(char3==NULL||char3==')') return 1; if(char3=='~') return 0; if(char1=='~') return 1; if(char1=='.') return 1; if(char3=='.') return 0; if(char1=='+') return 1; if(char3=='+') return 0; if(char1=='>') return 1; if(char3=='>') return 0; return 1; } void postfix() { int index=0; char operators[XPRSN_LEN]; while(1) { switch(xprsn[i]) { case '(': i++, postfix(); continue; case ')': i++; return; case'\0': xprsn[j]=NULL; return; default : { if(islower(xprsn[i])||xprsn[i]=='@') xprsn[j++]=xprsn[i++]; else { operators[index]=xprsn[i]; if(Priority(xprsn[i],xprsn[i+1],xprsn[i+2])) do xprsn[j++]=operators[index--]; while(index>=0&&Priority(operators[index],'?',xprsn[i])); index++; } } } } } /******************** boolean function ********************/ char negation(char x) { return (x=='F') ? 'T' : 'F'; } char conjunction(char x, char y) { return (x=='T'&&y=='T') ? 'T' : 'F'; } char implies(char x,char y) { return (x=='T'&&y=='F') ? 'F' : 'T'; } char bicondition(char x,char y) { return (x==y) ? 'T' : 'F'; } char disjunction(char x,char y) { return (x=='F'&&y=='F') ? 'F' : 'T'; } /******************** ends ********************/ char evaluate() { switch(xprsnCopy[--curXY]) { case '~': return negation (evaluate()); case '.': return conjunction(evaluate(),evaluate()); case '+': return disjunction(evaluate(),evaluate()); case '>': return implies (evaluate(),evaluate()); case '=': return bicondition(evaluate(),evaluate()); default : return xprsnCopy[curXY]; } } void SetValue(char var,char value) { int i; for(i=0;xprsn[i]!=NULL;i++) { if(xprsn[i]==var) xprsnCopy[i]=value; } } void DrawTable() { int i,j; gotoxy(2,5); textcolor(BROWN); putch('É'); for(i=0; i<TO; i++) cprintf("ÍÍÍË"); cprintf("ÍÍÍ»"); for(i=0; i<=8&&TO>3 || TO<4&&i<=pow(2,TO); i++) for(j=0;j <=TO+1; j++) gotoxy(4*j+2,2*i+6), putch('º'); for(i=0; i<8&&TO>3 || TO<4&&i<pow(2,TO); i++) { gotoxy(2,2*i+7), putch('Ì'); for(j=1; j<=TO; j++) cprintf("ÍÍÍÎ"); cprintf("ÍÍ͹"); } if(TO<4) gotoxy(2,2*pow(2,TO)+7); else gotoxy(2,23); putch('È'); for(i=0;i<TO;i++) cprintf("ÍÍÍÊ"); cprintf("ÍÍͼ"); } void TruthTable() { int i; unsigned long l; char ch, isThereF='F', isThereT='F'; strcpy(xprsnCopy,xprsn); textcolor(WHITE); gotoxy(1,1 ), cprintf("_ Bolean expression and its truth table_"); gotoxy(1,25), cprintf("...Press any key..."); textcolor(LIGHTMAGENTA), gotoxy(2,6); for(i=0; i<TO; i++) cprintf(" %c ",var[i]); cprintf(" -Y-\n\n"); DrawTable(); for(l=0x0000; l<pow(2,TO); l++) { if(l%8==0 && l!=0) getch(); for(i=0; i<TO; i++) { gotoxy(4+4*i,8+2*(l%8)); if(((l<<(32-(TO-i)))>>31)==0) textcolor(RED), putch('F'), SetValue(var[i],'F'); else textcolor(GREEN), putch('T'), SetValue(var[i],'T'); } curXY=strlen(xprsn), ch=evaluate(); textcolor(WHITE), gotoxy(4+4*i,8+2*(l%8)), putch(ch); (ch=='F') ?(isThereF='T'):(isThereT='T'); } gotoxy(1,25); if(isThereT=='F') cprintf(" The expression is contradiction."); else if(isThereF=='F') cprintf(" The expression is tautology."); else cprintf(" The expression is neither contradiction nor tautology."); } void Instructions() { clrscr(); textcolor(WHITE); cputs("__INSTRUCTIONS__\n"); cputs("\r\n_OPERATORS_"); cputs("\r\n# ~ for negation."); cputs("\r\n# . for conjunction."); cputs("\r\n# + for disjunction."); cputs("\r\n# > for implies."); cputs("\r\n# = for bicondition."); cputs("\r\n\n_OPERANDS_"); cputs("\r\n# should be lower case."); cputs("\r\n# should be single letter."); cputs("\r\n# dont try to use more than 18 variables."); cputs("\r\n\n_LOGICAL EQUALENCE_"); cputs("\r\n# if you have to check the logical equalence"); cputs("\r\n of expression1 and expression2."); cputs("\r\n enter the expression: (expressio1)=(expression2)"); cputs("\r\n if and only if its output is a tautology,"); cputs("\r\n then the two expressions are logically equalent."); cputs("\r\n\n_EXIT_"); cputs("\r\n# Esc-key will help you."); cputs("\r\n\n...press any key..."); getch(), clrscr(); } void main() { int msgNo=0; char xprsnDup[XPRSN_LEN+1]; textmode(C80); Instructions(); while(1) { textcolor(WHITE), gotoxy(1,1), cprintf("%-23s",msg[msgNo]); GetXprsn(); msgNo=SyntexChecking(); if(msgNo) continue; strcpy(xprsnDup,xprsn); Unary2Binary(), i=j=0, postfix(), Binary2Unary(); TruthTable(); getch(); strcpy(xprsn,xprsnDup), curXY=0, clrscr(), PutXprsn(); } } /******************************************************* * More programs: http://programs.on.googlepages.com * * Please report suggestions: programs.on@gmail.com * ********************************************461*lines**/