#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{ int b[200][200],c[200][200],n,*m,*ones,*primes,i,j,k,e,f,t;
int a1,b1,index,min = 0,order=0,cnt=0;
char cr;
/* comment : allocating memory space for variables */
printf(" Ain Shams University Faculty of Engineering \n");
printf(" Year : 2nd Year of Computer and Systems Eng. \n");
printf(" Student Name : Mahmoud Reda Hussien Seireg \n");
printf(" Section : 4 \n");
printf(" Bench No. : 23777 \n");
printf("\n enter # of variables in boolean expr. : ");
scanf("%d",&n);
if (n<=0) { printf(" error : not valid no. of variables \n"); goto end; }
m=(int *)calloc(n,sizeof(int));
ones=(int *)calloc(n,sizeof(int));
primes=(int *)calloc(n,sizeof(int));
/* comment : receiving minterms */
printf("\n enter minterms seperated by space");
printf("\n note : enter last minterm -1");
printf("\n for example F(x1,x2,x3)=(0,1,2,7) is entered as ");
printf("\n 0 1 2 7 -1 ");
printf("\n enter data : ");
for (i=0;i<pow(2,n);++i)
{ scanf("%d",&m[i]);
if (m[i]==-1)
break;
if ((m[i]>=pow(2,n)) || (m[i]<0))
{
printf("\n error : %d not a valid minterm.\n",m[i]);
printf(" please re-run the program \n ");
goto end;
}
/* comment : obtaining binary representation of minterm */
for (j=n-1;j>=0;--j)
{ b[i][j]=((m[i]%2)==1); m[i]/=2; }
}
/* comment : printing binary value of each minterm of the function */
printf("\n\n Displaying minterm's binary values of entered function\n");
for (k=0;k<i;++k)
{ printf(" | ");
for (j=0;j<n;++j)
printf("%d",b[k][j]);
}
printf("\n press enter to continue ");
cr=getchar();
cr=getchar();
/* comment : diplaying the function before simplification */
printf("\n\n Displaying the function before simplification : ");
printf(" \n F(");
for (k=1;k<=n;++k)
printf("x%d%s",k,(k!=n)?",":")");
printf("=");
for (k=0;k<i;++k)
{ for (j=0;j<n;++j)
if (b[k][j]==0)
printf("x%d'",j+1);
else if (b[k][j]==1)
printf("x%d",j+1);
printf("%s",(k==i-1)?".\n":" + ");
}
printf("\n press enter to continue ");
cr=getchar();
/* comment : counting ones in each minterm */
for (k=0;k<i;++k)
primes[k]=0;
for (f=0;f<i;++f)
{
for (k=0;k<i;++k)
{ ones[k]=0; primes[k]=0;}
for (k=0;k<i;++k)
for (j=0;j<n;++j)
ones[k]+=(b[k][j]==1);
/* comment : sorting and simplyfing min no. of ones with those
of next level of ones. */
min=ones[0];
for (k=0;k<i;++k)
min=(ones[k]<=min)?ones[k]:min;
for (e=0;e<i;++e)
for (k=0;k<i;++k)
if (ones[k]==min+e)
for (j=0;j<i;++j)
{ if (j==k) continue;
if (ones[j]==min+e+1)
{ for (a1=0;a1<n;a1++)
if (b[k][a1]==b[j][a1])
++cnt;
else
index=a1;
if (cnt==n-1)
{ primes[k]=1;
primes[j]=1;
for (b1=0;b1<n;b1++)
if (b1==index)
c[order][b1]='-';
else
c[order][b1]=b[k][b1];
++order;
}
}
cnt=0;
}
for (k=0;k<order;k++)
for (j=0;j<n;j++)
b[k][j]=c[k][j];
}
t=0;
for (k=0;k<order;++k)
if (primes[k]==0)
{ for (j=0;j<n;++j)
c[t][j]=b[k][j];
++t;
}
/* comment : printing binary value of simplified minterms */
printf("\n Displaying the binary value of the functions prime implicants \n");
for (k=0;k<t;++k)
{ printf(" \n");
for (j=0;j<n;++j)
if (c[k][j]==45)
printf("%c",c[k][j]);
else
printf("%d",c[k][j]);
}
printf("\n press enter to continue ");
cr=getchar();
/* comment : diplaying the function after simplification */
printf("\n Displaying the function after simplification : ");
printf(" \n F(");
for (k=1;k<=n;++k)
printf("x%d%s",k,(k!=n)?",":")");
printf("=");
for (k=0;k<=t;++k)
{ for (j=0;j<n;++j)
if (c[k][j]==0)
printf("x%d'",j+1);
else if (c[k][j]==1)
printf("x%d",j+1);
printf("%s",(k==t)?".\n":" + ");
}
putchar('\n');
end:
printf("\n press enter to exit the program ");
cr=getchar();
return 0;
}