-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
639 lines (619 loc) · 18 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data[257],bin[26],noofones,isimplicant,minarr[1000];
char term[26];
struct node* right;
};
struct node *root,*head,*improot,*save,*fin;
int var,min,number=1,columns=2,check=1,limit,imptable[100][100],counter=0,essential[1000],t=0,no=0,minterms[1000];
char a[26],b[26]; //variable names are stored as alphabets, can be modified to work for more variables
void group1(); //the minterms are grouped according to the number of ones
void arrange(); //the minterms are arranged according t their magnitude
void swap(struct node*,struct node*); //data of two nodes is swapped
void disp(); //various column with pairings are displayed
void further_groupings(); //the minterms are paired
void end_loop(struct node*); //the extra node in a list is deleted
void display_implicants(); //the implicants are displayed
void implicants(struct node*); //initializes each term as an implicant
void collect(); //converts the term from binary notation to variables
void variables(); //the variables for the function are stored
void convert(); //reduces the prime implicants which occur more than once to one
void implicants_table(); //the prime implicants table is formed and essential implicants are found
void func(); //the minimized function is displayed
void other_implicants(); //the prime implicants other than the essential ones are collected
void final_terms(); //the final terms in the minimized function are noted
void store_minterms(); //minterms are stored in an array
int main()
{
int i,j,k,x;
struct node* temp;
printf("\nEnter the number of variables : "); //no. of variables and minterms are recorded
scanf("%d",&var);
printf("\nEnter the number of minterms : ");
scanf("%d",&min);
i=min-1;
root=temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the minterms one by one\n\n");
scanf("%d",&temp->data[0]); //first minterm is stored
j=temp->data[0];
temp->noofones=0;
x=var;
k=0;
while(x--) //converts minterm to binary notation
{
if(j%2==1)
{
temp->bin[k]=1;
temp->noofones++;
}
else
{
temp->bin[k]=0;
}
j=j/2;
k++;
}
while(i--) //rest of the minterms are stored
{
temp=temp->right=(struct node*)malloc(sizeof(struct node));
scanf("%d",&temp->data[0]);
j=temp->data[0];
temp->noofones=0;
x=var;
k=0;
while(x--)
{
if(j%2==1) //converts the minterms to binary notation
{
temp->bin[k]=1;
temp->noofones++; //the number of ones in binary notation
}
else
{
temp->bin[k]=0;
}
j=j/2;
k++;
}
}
temp->right=NULL;
arrange(); //various functions are called according to their needs
store_minterms();
group1();
disp();
end_loop(root);
head=(struct node*)malloc(sizeof(struct node));
while(check>0)
{
further_groupings();
}
save->right=NULL; //storing null value in link field of list storing prime implicants
printf("No pairs formed hence no further calculation required\n\n");
end_loop(improot);
collect();
display_implicants();
variables();
implicants_table();
other_implicants();
final_terms();
end_loop(fin);
convert();
func();
return 0;
}
void arrange() //arranging the minterms in increasing order of magnitude
{
struct node *temp1,*temp2;
temp1=temp2=root;
while(temp1!=NULL)
{
temp2=root;
while(temp2!=NULL)
{
if(temp1->data[0]<temp2->data[0]) //if not in order their values are exchanged with swap function
{
swap(temp1,temp2);
}
temp2=temp2->right;
}
if(temp1->right==NULL)
{
limit=temp1->data[0]; //the magnitude of the last minterm is recorded later for prime implicants table
}
temp1=temp1->right;
}
}
void store_minterms() //array to store all the minterms
{
int i=0;
struct node* temp;
temp=root;
while(temp!=NULL)
{
minterms[i]=temp->data[0];
i++;
temp=temp->right;
}
}
void swap(struct node* temp1,struct node* temp2) //swapping all the data of two nodes
{
int x,y,i=0;
i=var;
for(i=0;i<var;i++) //binary notation is exchanged
{
y=temp1->bin[i];
temp1->bin[i]=temp2->bin[i];
temp2->bin[i]=y;
}
y=temp1->noofones; //no. of ones is exchanged
temp1->noofones=temp2->noofones;
temp2->noofones=y;
x=temp1->data[0]; //data(minterm) is exchanged
temp1->data[0]=temp2->data[0];
temp2->data[0]=x;
}
void group1() //where the minterms are arranged according to the number of ones
{
int i,count=0,j,k=0;
struct node *temp,*next;
temp=save=root;
root=next=(struct node*)malloc(sizeof(struct node));
for(i=0;i<=var;i++)
{
temp=save;
while(temp!=NULL)
{
if(temp->noofones==i) //minterms are arranged according to no. of ones , first 0 ones then 1 ones... and so on
{
next->data[0]=temp->data[0];
k++;
for(j=0;j<var;j++)
{
next->bin[j]=temp->bin[j];
}
next->noofones=temp->noofones;
next=next->right=(struct node*)malloc(sizeof(struct node));
}
temp=temp->right;
}
}
minterms[k]=-1;
next->right=NULL;
}
void disp() //for displaying the various column with pairings
{
int i,j=min;
struct node* temp;
temp=root;
printf("\n\nColumn #%d\n\n\n",number); //number tells us which column is being printed
while(temp->right!=NULL)
{
printf("%d\t",temp->data[0]);
for(i=var-1;i>=0;i--)
{
printf("%d",temp->bin[i]);
}
temp=temp->right;
printf("\n");
}
temp->right=NULL;
number++;
}
void end_loop(struct node* ptr) //reducing the number of nodes in a list with one extra node
{
struct node* temp;
temp=ptr;
while(temp->right->right!=NULL)
{
temp=temp->right;
}
temp->right=NULL;
}
void further_groupings() //grouping based on difference in binary notation
{
int i,count,k,j,x;
struct node *temp,*next,*p,*imp;
check=0;
if(columns==2) //for second column
{
imp=improot=(struct node*)malloc(sizeof(struct node));
p=head;
}
else //for other columns
{
imp=save;
root=head;
p=head=(struct node*)malloc(sizeof(struct node));
}
temp=root;
implicants(root);
printf("\n\nColumn #%d\n\n\n",number);
while(temp!=NULL)
{
next=temp->right;
while(next!=NULL)
{
count=0;
if(next->noofones-temp->noofones==1) //if two terms differ in their no. of ones by one
{
for(i=0;i<var;i++)
{
if(temp->bin[i]!=next->bin[i])
{
k=i; //the place in which they differ is noted
count++;
}
}
}
if(count==1) //checks if the two terms differ by one place in binary notation
{
temp->isimplicant=0; //if they do then they are not a prime implicant
next->isimplicant=0;
check++;
for(i=0;i<var;i++)
{
p->bin[i]=temp->bin[i]; //binary notation is stored
}
p->bin[k]=-1;
x=0;
for(j=0;j<columns/2;j++) //data from first term is stored
{
p->data[x]=temp->data[j];
x++;
}
for(j=0;j<columns/2;j++) //data from second term is stored
{
p->data[x]=next->data[j];
x++;
}
p->noofones=temp->noofones;
for(j=0;j<columns;j++) //the pair formed is displayed
{
printf("%d,",p->data[j]);
}
printf("\b ");
printf("\t");
for(i=var-1;i>=0;i--)
{
if(p->bin[i]==-1)
printf("-");
else
printf("%d",p->bin[i]);
}
printf("\n");
p=p->right=(struct node*)malloc(sizeof(struct node)); // one extra node that is to be deleted
}
next=next->right;
}
temp=temp->right;
}
p->right=NULL;
if(check!=0)
{
end_loop(head); //extra node is deleted
}
temp=root;
while(temp!=NULL) //for selecting the prime implicants
{
if(temp->isimplicant==1) // if term is a prime implicant it is stored separately in list with head pointer improot
{
i=0;
for(i=0;i<columns/2;i++)
{
imp->data[i]=temp->data[i];
}
imp->data[i]=-1;
for(i=0;i<var;i++)
{
imp->bin[i]=temp->bin[i];
}
imp=imp->right=(struct node*)malloc(sizeof(struct node));
}
temp=temp->right;
}
save=imp;
columns=columns*2;
number++;
}
void display_implicants() //displays the implicants
{
int i=0;
struct node* temp;
temp=improot;
printf("\n\nThe prime implicants are:- \n\n");
while(temp!=NULL)
{
i=0;
i=var-1;
while(i>=0) //displays the binary notation
{
if(temp->bin[i]==-1)
{
printf("-");
}
else
{
printf("%d",temp->bin[i]);
}
i--;
}
printf("\t\t");
i=0;
while(temp->data[i]!=-1) //displays the minterm pairs
{
printf("%d,",temp->data[i]);
i++;
}
printf("\b ");
temp=temp->right;
printf("\n\n");
counter++;
}
}
void implicants(struct node* ptr) //initializing each term as a prime implicant
{
struct node* temp;
temp=ptr;
while(temp!=NULL)
{
temp->isimplicant=1;
temp=temp->right;
}
}
void collect() //reduces the terms that occur more than once to a single
{
int common=0,i;
struct node *temp1,*temp2,*temp3;
temp1=temp2=improot;
while(temp1!=NULL)
{
temp2=temp1->right;
while(temp2!=NULL)
{
common=0;
for(i=0;i<var;i++) //if their binary notation is same one will be deleted
{
if(temp2->bin[i]==temp1->bin[i])
{
common++;
}
}
if(common==var)
{
temp3=improot;
while(temp3->right!=temp2) //the repeated term is deleted
{
temp3=temp3->right;
}
temp3->right=temp2->right;
temp2=temp3;
}
temp2=temp2->right;
}
temp1=temp1->right;
}
}
void variables() //stores variables(alphabets)
{
int i;
for(i=0;i<26;i++)
{
a[i]=65+i; //variables
b[i]=97+i; //their compliments
}
}
void convert() //it converts the binary notation of each term to variables
{
int i,j;
struct node* temp;
temp=fin;
while(temp!=NULL)
{
j=0;
for(i=0;i<var;i++)
{
if(temp->bin[i]==0)
{
temp->term[j]=b[i];
j++;
}
if(temp->bin[i]==1)
{
temp->term[j]=a[i];
j++;
}
}
temp=temp->right;
}
}
void func() //displays the minimized function in SOP form
{
struct node* temp;
temp=fin;
printf("\n\nThe minimized function is :- ");
while(temp!=NULL)
{
printf("%s",temp->term);
if(temp->right!=NULL)
{
printf(" + ");
}
temp=temp->right;
}
printf("\n\n");
}
void implicants_table() //function for creating prime implicants table as well as selecting essential prime implicants
{
struct node* temp;
int i,j,k,m,n,x,y,count=0,count2=0,a=0;
for(i=0;i<counter;i++)
{
for(j=0;j<=limit;j++)
{
imptable[i][j]=0; //0 or - is placed in all places of a table
}
}
i=0;
j=0;
k=0;
temp=improot;
while(temp!=NULL)
{
k=0;
while(temp->data[k]!=-1)
{
imptable[i][temp->data[k]]=1; // 1 or X is placed for the column with same index as that of the number in the pair
k++;
}
i++;
temp=temp->right;
}
printf("\n\n\t\t\tPrime Implicants Table\n\n\n");
temp=improot;
i=0;
printf(" ");
while(minterms[i]!=-1)
{
printf("%d\t",minterms[i]); //the minterms are displayed in row
i++;
}
printf("\n\n");
for(i=0;i<counter;i++) //X and - are placed for the terms with corresponding minterm values
{
printf(" ");
a=0;
for(j=0;j<=limit;j++)
{
if(j==minterms[a])
{
if(imptable[i][j]==0)
{
printf("-");
}
if(imptable[i][j]==1)
{
printf("X");
}
printf("\t");
a++;
}
}
y=0;
while(temp->data[y]!=-1) //prints the minterm pair
{
printf("%d,",temp->data[y]);
y++;
}
printf("\b ");
temp=temp->right;
printf("\n\n");
}
printf("\n\n");
for(i=0;i<counter;i++) //for finding essential prime implicants
{
for(j=0;j<=limit;j++)
{
count=0;
if(imptable[i][j]==1)
{
y=j;
x=i;
for(k=0;k<counter;k++)
{
if(imptable[k][j]==1) //checks if there is only one X in a column
{
count++;
}
}
if(count==1) //places - in place of X in every column of the table whose one row contains only one X in a column
{
essential[t]=x;
t++;
for(n=0;n<=limit;n++)
{
if(imptable[i][n]==1)
{
for(m=0;m<counter;m++)
{
imptable[m][n]=0;
}
}
}
}
}
}
}
essential[t]=-1;
i=0;
}
void other_implicants() //after finding the essential prime implicants other terms necessary are marked
{
no=0; //to check if any term is found in each iteration
int count1=0,count2=0;
int i,j;
for(i=0;i<counter;i++)
{
count1=0;
for(j=0;j<=limit;j++)
{
if(imptable[i][j]==1) //no. of X's or 1's are calculated
{
no++;
count1++;
}
}
if(count1>count2) //to find the term with maximum X's in a row
{
essential[t]=i;
count2=count1;
}
}
for(j=0;j<=limit;j++) //removing the X's in the row as well a those X's which are in same column
{
if(imptable[essential[t]][j]==1)
{
for(i=0;i<counter;i++)
{
imptable[i][j]=0;
}
}
}
t++;
essential[t]=-1;
if(no>0) //if one or more terms is found the function is called again otherwise not
{
other_implicants();
}
}
void final_terms() //in this function all the terms in the minimized expression are stored in a linked list
{
int i=0,j,c=0,x;
struct node *temp,*ptr;
fin=temp=(struct node*)malloc(sizeof(struct node));
while(essential[i]!=-1)
{
ptr=improot;
x=essential[i];
for(j=0;j<x;j++) //so that pointer points to the node whose index was stored in array named essential
{
ptr=ptr->right;
}
j=0;
while(ptr->data[j]!=-1) // the data of the node is stored
{
temp->data[j]=ptr->data[j];
j++;
}
temp->data[j]=-1;
for(j=0;j<var;j++) //the binary code is stored
{
temp->bin[j]=ptr->bin[j];
}
temp=temp->right=(struct node*)malloc(sizeof(struct node));
i++;
c++;
}
temp->right=NULL;
}