TABULAR METHOD 1.0
(C program)

Tabular method is a method to simplify a boolean expression. Interactive inputting with syntax checking facility are available.
 /* ##################################### # TABULAR METHOD 1.0 # # tested in Turbo C++,version 3.0 # ###############21/2/2008############# */ #include<dos.h> #include<math.h> #include<conio.h> #include<ctype.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #define FALSE 0 #define TRUE 1 #define XPRSN_LEN 70 #define PROCESSING 0 #define PROCESSED 1 #define THIRD_LIST 2 int varLen, numOfSigmaValue, i=2, j, positionOfEquality=3; int sigmaValue[40], colorCode; char varList[8], tmpStr[17]; char list[3][40][9],listIndex[4], tableList[2][20][40]; char xprsn[XPRSN_LEN+1]=/**/"f()=ä()"; //"f(w,x,y,z)=ä(0,2,3,5,6,7,8,9,10,14,15)"; //"f(w,x,y,z)=ä(0,1,2,3,4,7,8,9,10,13)"; //"f(w,x,y,z)=ä(0,1,3,4,5,6,7,8,9,10,11,13)"; //"f(w,x,y,z)=ä(0,2,3,5,6,8,11,12,13,14)"; //"f(w,x,y,z)=ä(0,2,6,8,10,11,12,13,14,15)"; //"f(w,x,y,z)=ä(0,4,5,6,8,9,10,11,14,15)"; //"f(w,x,y,z)=ä(2,3,4,5,6,8,9,10,11,12,13,14,15)"; //"f(a,b,c,d,e,f,g,h)=ä(1,4,8,16,32,64,128)"; //"f(a,b,c,d,e,f,g)=ä(20,28,38,39,52,60,102,103,127)"; //"f(w,x,y,z)=ä(7,8,10,13,0,2,4,5,6,15)"; //"f(w,x,y,z)=ä(0,2,3,5,7,8,9,10,11,13,15)"; //"f(w,x,y,z)=ä(1,4,6,7,8,9,10,11,15)"; //"f(a,b,c,d)=ä(0,1,2,8,10,11,14,15)"; //"f(a,b,c,d)=ä(0,1,2,4,5,6,8,9,12,13,14)"; //"f(w,x,y,z)=ä(1,5,6,7,11,12,13,15)"; //"f(a,b,c,d,e)=ä(0,2,3,4,5,6,11,15,16,18,19,23,27,31)"; //"f(a,b,c,d,e,f)=ä(0,1,5,7,8,15,16,17,21,23,31,32,40,47,48)"; char *msg[]={ "_Enter the expression_", "The number is invalid.", "Repeating the number.", "More than 8 variables found.", "Repeating the variable.", "Variable not found.", "Comma not found.", "The expression is condradiction.", "Number not found.", "The expression is tautology." }; char *ToVarForm(const char *str) { int k, l=0; for(k=0;k<varLen;k++) { switch(str[k]) { case'0': tmpStr[l++]=varList[k]; tmpStr[l++]='\''; continue; case'1': tmpStr[l++]=varList[k]; } } tmpStr[l]=NULL; return tmpStr; } void DisplayResult() { int k,l; gotoxy(1,23), cprintf("Result: "); for(k=0;k<listIndex[THIRD_LIST];k++) for(l=0;l<listIndex[PROCESSING];l++) if(tableList[PROCESSING][k][l]=='*') cprintf("(%s)+",ToVarForm(list[THIRD_LIST][k])); puts("\b "); }/**/ void DisplayTable() { int k,l; gotoxy(1,1), cprintf("_Redendency removing table_"); gotoxy(5,2), cprintf(xprsn); for(k=0;k<numOfSigmaValue;k++) { gotoxy(2*varLen+k*4+2,5), cprintf("%4d",sigmaValue[k]); gotoxy(2*varLen+1+k*4+1,6), cprintf("ÍÍÍÍ"); } for(k=0;k<listIndex[THIRD_LIST];k++) { gotoxy(1,7+k*2), cprintf("%*s",2*varLen,ToVarForm(list[THIRD_LIST][k])); for(l=0;l<listIndex[PROCESSING];l++) { if(tableList[PROCESSING][k][l]=='*') textcolor(tableList[1][k][l]), gotoxy(2*varLen+5+4*l,7+k*2), cprintf("%c",tableList[0][k][l]); else if(tableList[PROCESSING][k][l]=='x') if(tableList[1][k][l]=='x') textcolor(WHITE), gotoxy(2*varLen+5+4*l,7+k*2), cprintf("x"); else textcolor(tableList[1][k][l]), gotoxy(2*varLen+5+4*l,7+k*2), cprintf("x"); } textcolor(WHITE); gotoxy(2*varLen+2,7+k*2), putch('º'); gotoxy(2*varLen+2,7+k*2+1), putch('º'); } gotoxy(2*varLen+2,5), cprintf("º"); gotoxy(2*varLen,6), cprintf("ÍÍÎ"); } void AddToList(const char *str,int index) { int k; for(k=0;k<listIndex[index];k++) if(strcmp(str,list[index][k])==0) return; strcpy(list[index][listIndex[index]++],str); }/**/ void disabling(int row,int col) { int k,l; int colorValues[]={14,10,13,12,9,11,6,4,3,5}; for(k=0;k<listIndex[0];k++) { if(tableList[1][row][k]=='x') { for(l=0;l<listIndex[2];l++) { if(l==row&&k==col) { tableList[0][l][k]='*';tableList[1][l][k]=colorValues[colorCode]; } else tableList[1][l][k]=colorValues[colorCode]; } } } colorCode++; delay(1000); DisplayTable(); DisplayResult(); }/**/ int GetRow(int row,int col) { int k,l,getRow; int count=0,tmpCount; getRow=row; while(1) { tmpCount=0; while(tableList[1][row][col]!='x'&&row<listIndex[2]) row++; if(tableList[1][row][col]!='x') return getRow; for(k=0;k<listIndex[0];k++) { if(tableList[1][row][k]=='x') { for(l=0;l<listIndex[2];l++) { if(tableList[1][l][k]=='x') tmpCount++; } } } if(tmpCount>count) { count=tmpCount; getRow=row; } row++; } }/**/ void ProduceResult() { int k,l,tmp; for(k=0;k<listIndex[0];k++) { tmp=-1; for(l=0;l<listIndex[2];l++) { if(tableList[1][l][k]=='x') { if(tmp==-1) tmp=l; else break; } } if(tmp!=-1&&l==listIndex[2]) disabling(tmp,k); } for(k=0;k<listIndex[0];k++) { for(l=0;l<listIndex[2];l++) { if(tableList[1][l][k]=='x') { tmp=GetRow(l,k); break; } } if(l!=listIndex[2]) disabling(tmp,k); } }/**/ int isStr1IncludeInStr2(const char *str1,const char *str2) { int k; for(k=0;k<varLen;k++) if(str1[k]!=str2[k]&&str2[k]!='-') break; if(k==varLen) return TRUE; else return FALSE; }/**/ void MakeInitialList() { int k,l; for(k=0; k<numOfSigmaValue; k++) for(l=0; l<varLen; l++) if(((sigmaValue[k]<<(16-(varLen-l)))>>15)==0) list[0][k][l]='0'; else list[0][k][l]='1'; list[0][k][l]=NULL; listIndex[0]=numOfSigmaValue; }/**/ void RedandencyTable() { int k,l; MakeInitialList(); for(k=0;k<listIndex[THIRD_LIST];k++) for(l=0;l<listIndex[PROCESSING];l++) if(isStr1IncludeInStr2(list[PROCESSING][l],list[THIRD_LIST][k])) tableList[PROCESSING][k][l]=tableList[PROCESSED][k][l]='x'; }/**/ void ProcessedToProcessing() { int k; listIndex[PROCESSING]=0; for(k=0;k<listIndex[PROCESSED];k++) AddToList(list[PROCESSED][k],PROCESSING); }/**/ void AddToFinalList() { int k,l; for(k=0; k<listIndex[PROCESSING]; k++) { for(l=0;l<listIndex[PROCESSED];l++) if(isStr1IncludeInStr2(list[PROCESSING][k],list[PROCESSED][l])==TRUE) break; if(l==listIndex[PROCESSED]) AddToList(list[PROCESSING][k],THIRD_LIST); } }/**/ void CheckSimiliarity(const char *str1, const char *str2, char *str) { int k; char singleDifferenceOccured=FALSE; for(k=0;k<varLen;k++) { if(str1[k]!=str2[k]) { if(singleDifferenceOccured==FALSE) str[k]='-', singleDifferenceOccured=TRUE; else { str[0]=NULL; return; } } else str[k]=str1[k]; // or str[k]=str2[k]; } str[k]=NULL; }/**/ void ReduceList() { int k, l; char str[9]; listIndex[PROCESSED]=0; for(k=0;k<listIndex[PROCESSING];k++) for(l=k+1;l<listIndex[PROCESSING];l++) { CheckSimiliarity(list[PROCESSING][k],list[PROCESSING][l],str); if(strcmp(str,NULL)!=0) AddToList(str,PROCESSED); } }/**/ int StoreNum() { int k=0, num; char numStr[XPRSN_LEN]; while(isdigit(xprsn[i])) numStr[k++]=xprsn[i++]; numStr[k]=NULL; if(strlen(numStr)>3) return 1; num=atoi(numStr); if(num>=pow(2,varLen)) return 1; for(k=0; k<numOfSigmaValue; k++) if(num==sigmaValue[k]) return 2; sigmaValue[numOfSigmaValue++]=num; return 0; }/**/ int StoreVar(char ch) { int k; if(varLen==8) return 3; for(k=0; k<varLen; k++) if(ch==varList[k]) return 4; varList[varLen++]=ch; return 0; }/**/ int SyntaxChecking() { int msgNo; i=2; varLen=numOfSigmaValue=0; while(TRUE) { if(islower(xprsn[i])) { msgNo=StoreVar(xprsn[i]); if(msgNo) return msgNo; i++; } else return 5; if(xprsn[i]==')') break; if(xprsn[i]!=',') return 6; i++; } i+=4; if(xprsn[i]==')') return 7; while(TRUE) { if(isdigit(xprsn[i])) { msgNo=StoreNum(); if(msgNo) return msgNo; } else return 8; if(xprsn[i]==')') { if(numOfSigmaValue==pow(2,varLen)) return 9; else return 0; } if(xprsn[i]!=',') return 6; i++; } }/**/ void SetCurser(int NoOfCell) { i+=NoOfCell; if(i<2 || i>strlen(xprsn)-1) i-=NoOfCell; else { if(xprsn[i-1]==')') i+=3; else if(xprsn[i]=='(') i-=3; } }/**/ void AddChar(char ch) { if(strlen(xprsn)==XPRSN_LEN) return; if(i<positionOfEquality) positionOfEquality++; for(j=strlen(xprsn); j>=i; j--) xprsn[j+1]=xprsn[j]; xprsn[i]=ch; gotoxy(1,3), cprintf("%s ",xprsn); i++; }/**/ void AddDel() { if(xprsn[i]==')') return; if(i<positionOfEquality) positionOfEquality--; for(j=i; j<strlen(xprsn); j++) xprsn[j]=xprsn[j+1]; gotoxy(1,3), cprintf("%s ",xprsn); }/**/ void AddBksp() { if(xprsn[i-1]=='(') return; i--; AddDel(); }/**/ void GetXprsn(int index) { char ch; clrscr(); gotoxy(1,1), cprintf(msg[index]); gotoxy(1,3), cprintf(xprsn); while(TRUE) { gotoxy(1+i,3); switch(ch=getch()) { case 27 : exit(0); /* Escape */ case'\r': return; /* Enter */ case'\b': AddBksp(); continue; /* Backspace */ case 75 : SetCurser(-1); continue; /* left */ case 77 : SetCurser(+1); continue; /* right */ case 83 : AddDel(); continue; /* Delete */ case ',': AddChar(ch); continue; default : if(isdigit(ch)&&i>positionOfEquality) AddChar(ch); else if(islower(ch)&&i<positionOfEquality) AddChar(ch); } } }/**/ void main() { int msgNo=0; textmode(C80); textcolor(WHITE); getx: GetXprsn(msgNo); msgNo=SyntaxChecking(); if(msgNo) goto getx; MakeInitialList(); while(TRUE) { ReduceList(); AddToFinalList(); if(listIndex[PROCESSED]==0) break; ProcessedToProcessing(); } RedandencyTable(); clrscr(); DisplayTable(); DisplayResult(); ProduceResult(); gotoxy(1,25), cprintf("...Press any Key..."), getch(); } /******************************************************* * More programs: http://programs.on.googlepages.com * * Please report suggestions: programs.on@gmail.com * ********************************************307*lines**/