我正在尝试编写一个使用k-map
来解决布尔表达式的程序。我正在使用从用户接受最小项的逻辑,并检查最小项是否与我初始化的数组中的相应元素匹配,如果匹配,则使该元素1
,如果不匹配,则使0
。我的程序制作了所有元素0
。
#include <stdio.h>
#include <conio.h>
void table(){
printf("\n|-------------------------------------|");
printf("\n|variables|Minterms|Term|Maxterms|Term|");
printf("\n|---------|--------|----|--------|----|");
printf("\n| 0| 0 | 0|x'.y'.z'| m0 |x+y+z | M0 |");
printf("\n| 0| 0 | 1|x'.y'.z | m1 |x+y+z' | M1 |");
printf("\n| 0| 1 | 0|x'.y.z' | m2 |x+y'+z | M2 |");
printf("\n| 0| 1 | 1|x'.y.z | m3 |x+y'+z' | M3 |");
printf("\n| 1| 0 | 0|x.y'.z' | m4 |x'+y+z | M4 |");
printf("\n| 1| 0 | 1|x.y'.z | m5 |x'+y+z' | M5 |");
printf("\n| 1| 1 | 0|x.y.z' | m6 |x'+y'+z | M6 |");
printf("\n| 1| 1 | 1|x.y.z | m7 |x'+y'+z'| M7 |");
printf("\n|-------------------------------------|");
}
void main()
{ int i,j,k,n,nm,ochoice,minterms[10],kmap[4][4]={0,1,3,2,
4,5,7,6,
12,13,15,14,
8,9,11,10};
char v[6];
printf("\t\t\tK-MAP");
printf("\n\t\t\t-----");
printf("\nEnter no of variables(3or4):");
scanf("%d",&n);
printf("\nEnter the variables: ");
for(i=0;i<n;i++)
{ printf("\nEnter variable %d: ",i+1);
scanf("%s",&v[i]);
}
printf("\n1.SOP");
printf("\n2.POS");
printf("\nEnter the required output form(1or2):-");
scanf("%d",&ochoice);
switch(ochoice)
{case 1 :{system("cls");
table();
printf("\nEnter the number of minterms : ");
scanf("%d",&nm);
printf("\nEnter the corresponding minterms number from the table above : ");
for(i=0;i<nm;i++)
{
printf("\nEnter Minterm %d:",i+1);
scanf("%d",&minterms[i]);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<nm;k++){
if(kmap[i][j]==minterms[k]){
kmap[i][j]=1;
}
else{
kmap[i][j]=0;
}
}
}
}
for(i=0;i<4;i++){
printf("\n");
for(j=0;j<4;j++){
printf(" %d ",kmap[i][j]);
}
}
break;
}
}
}
Enter the number of minterms :4
Enter minterm 1:1
Enter minterm 2:5
Enter minterm 3:4
Enter minterm 4:12
output:-0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
expexted output:-0 1 0 0
1 1 0 0
1 0 0 0
0 0 0 0
NB:-该程序仅需一分钟即可正常工作
答案 0 :(得分:0)
@Weather Vane指出了问题 我通过制作另一个数组并对其进行更改来更正了它, 这是代码
#include <stdio.h>
#include <conio.h>
void table(){
printf("\n|-------------------------------------|");
printf("\n|variables|Minterms|Term|Maxterms|Term|");
printf("\n|---------|--------|----|--------|----|");
printf("\n| 0| 0 | 0|x'.y'.z'| m0 |x+y+z | M0 |");
printf("\n| 0| 0 | 1|x'.y'.z | m1 |x+y+z' | M1 |");
printf("\n| 0| 1 | 0|x'.y.z' | m2 |x+y'+z | M2 |");
printf("\n| 0| 1 | 1|x'.y.z | m3 |x+y'+z' | M3 |");
printf("\n| 1| 0 | 0|x.y'.z' | m4 |x'+y+z | M4 |");
printf("\n| 1| 0 | 1|x.y'.z | m5 |x'+y+z' | M5 |");
printf("\n| 1| 1 | 0|x.y.z' | m6 |x'+y'+z | M6 |");
printf("\n| 1| 1 | 1|x.y.z | m7 |x'+y'+z'| M7 |");
printf("\n|-------------------------------------|");
}
void main()
{ int i,j,k,n,nm,ochoice,minterms[10],kmap[4][4]={0,1,3,2,
4,5,7,6,
12,13,15,14,
8,9,11,10};
int k2map[4][4]={0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,};
char v[6];
printf("\t\t\tK-MAP");
printf("\n\t\t\t-----");
printf("\nEnter no of variables(3or4):");
scanf("%d",&n);
printf("\nEnter the variables: ");
for(i=0;i<n;i++)
{ printf("\nEnter variable %d: ",i+1);
scanf("%s",&v[i]);
}
printf("\n1.SOP");
printf("\n2.POS");
printf("\nEnter the required output form(1or2):-");
scanf("%d",&ochoice);
switch(ochoice)
{case 1 :{system("cls");
table();
printf("\nEnter the number of minterms : ");
scanf("%d",&nm);
printf("\nEnter the corresponding minterms number from the table above : ");
for(i=0;i<nm;i++)
{
printf("\nEnter Minterm %d:",i+1);
scanf("%d",&minterms[i]);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<nm;k++){
if(kmap[i][j]==minterms[k]){
k2map[i][j]=1;
}
}
}
}
for(i=0;i<4;i++){
printf("\n");
for(j=0;j<4;j++){
printf(" %d ",k2map[i][j]);
}
}
break;
}
}
}