📄 kanoi.c
字号:
}
for(i=0,p=group[0];i<&*border-&*group[0];i++,p++)
if(*(*p+9)==1){free(*p);*p=NULL;} /*撤消可比较内容*/
r=border;r++;
while(*r!=NULL)
{k=*(*r+9);
if(k!=-1) {*(*r+10+k)='-';*(*r+9)=-1;**r=**r-1;}
r++;
};
r=border;r++;
for(p=r;p<key+80;p++)
if(*p!=NULL)
{for(q=p+1;q<key+80;q++)
if(*q!=NULL)
{for(k=0,n=0;k<4;k++)
{temp=S((*(*p+10+k)),(*(*q+10+k)));
n=n+temp;
}
if(n==0){free(*q);*q=NULL;}
}else continue;
}else continue;
}
/*==========================简化最小项组合,寻求多结果============================*/
char number[12]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
void simplelist(void)
{
int i,j,k,n=0,temporary;char (*p)[18],*q;
char pri[14][18];
/*第10行标记最小项包含次数,第18列标记项的个数及独立项标记,第17列标记地址*/
for(i=0;i<12;i++) number[i]=-1;
for(i=0;i<14;i++)for(j=0;j<18;j++) pri[i][j]=-1;
p=pri;q=&pri[0][0];
for(i=0;i<6;i++)group[i]=NULL;/*sort(q);*/
for(i=0;i<16;i++)
if(kive[i]=='*') *(*p+i)='x';n++;/*n标记二维数组行*/
for(i=0;i<16;i++) /*对最小项标记*/
if(key[i]!=NULL)
{q=key[i];
for(j=1;j<=12;j++)
{temporary=*(q+j);
if(temporary!=-1) *(*(p+n)+temporary)='x';else break;
}
*(*(p+n)+16)=i;*(*(p+n)+17)=0;n++;
}else break;
for(j=0;j<16;j++) /*列最小项统计*/
{if(pri[0][j]=='x') /*Fill the list of kives contained*/
{ for(i=1,n=0;i<=12;i++)
{q=&pri[0][j];if(*(q+18*i)=='x') n++;
pri[13][j]=n;
}
}
}
for(j=0;j<16;j++) /*标记独立项*/
{q=&pri[13][j];
if(*q==1)
for(i=1;i<=12;i++)
if(pri[i][j]=='x')
{pri[i][j]='@';pri[i][17]=1;break;}
}
for(i=1,n=0;i<12;i++) /*对多结果标记并简化重复项*/
if((pri[i][17])==0) /*标记非独立行*/ /*Find the lonely kives*/
{number[i]=-2;n++;}
if(n==1)for(i=0;i<12;i++)if(number[i]==-2){k=pri[i][16];*(key[k]+9)='@';}
for(i=0,n=0;i<12;i++) /* 最多只有一对最小项组构成多输出可能*/
if(number[i]==-2) /* 不含独立项的最小项组合是多结果的原因 */
for(j=i+1;j<12;j++) /* 任两个不含独立项的组合有相同的项,则 */
if(number[j]==-2) /* 可能结果一2的阶乘递增 。 */
for(k=0;k<16;k++)
if((pri[i][k]=='x')&&(pri[j][k]=='x')&&(pri[13][k]==2))
{number[i]=pri[i][16];number[j]=pri[j][16];temporary=j;
*(*recorder+n)=pri[i][16];*(*recorder+3-n)=j;n++;
pri[i][17]=1;pri[j][17]=1;
}
for(i=1;i<12;i++)if(pri[i][17]==0){k=pri[i][16];*(key[k]+9)='@';}
for(i=0,n=0;i<12;i++)if(number[i]>-1)n++;if(n>0)n=log(n)/log(2); /*record of different results*/
if(n==1){k=recorder[0][3];recorder[0][3]=-1;recorder[1][0]=number[k];}
if(n==2)
{recorder[1][0]=recorder[0][0];k=recorder[0][3];recorder[0][3]=-1;/*多结果的标记*/
recorder[2][0]=recorder[3][0]=number[k];
recorder[2][1]=recorder[0][1];k=recorder[0][2];recorder[0][2]=-1;
recorder[1][1]=recorder[3][1]=number[k];
}
}
/*==================================输出函数=====================================*/
void output(void)
{ int i,j,k=0;char *p;
cp=0; /*cp为输出计数器*/
setcolor(RED); /*红色输出*/
if(count>0) for(i=0;i<4;i++)
{if(recorder[count-1][i]!=-1) {k=recorder[count-1][i];*(key[k]+9)='@';}}
count--; /*count初值即为结果个数*/
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,0);
for(i=0;i<16;i++)
{
outtextxy(45,444+count*8,"=");
if((key[i]!=NULL)&&(*(key[i]+9)!='@'))
{
p=key[i];
for(j=10;j<14;j++)
{
switch(j)
{case 10:{
if(*(p+10)==1){outtextxy(53+8*cp,444+count*8,"A");cp++;}
if(*(p+10)==0){outtextxy(53+8*cp,444+count*8,"A'");cp+=2;}
}break;
case 11:{
if(*(p+11)==1){outtextxy(53+8*cp,444+count*8,"B");cp++;}
if(*(p+11)==0){outtextxy(53+8*cp,444+count*8,"B'");cp+=2;}
}break;
case 12:{
if(*(p+12)==1){outtextxy(53+8*cp,444+count*8,"C");cp++;}
if(*(p+12)==0){outtextxy(53+8*cp,444+count*8,"C'");cp+=2;}
}break;
case 13:{
if(*(p+13)==1){outtextxy(53+8*cp,444+count*8,"D");cp++;}
if(*(p+13)==0){outtextxy(53+8*cp,444+count*8,"D'");cp+=2;}
}break;
default:break;
}
}outtextxy(53+8*cp,444+count*8,"+");cp++;
}
}setcolor(LIGHTBLUE);outtextxy(45+8*cp,444+count*8,"+");
}
/*===============================初始化简空间=====================================*/
void initial(void)
{
int i,j,k;
char *p,*q,*head,queue[14],n;
count=0; /*多结果计数器count清零*/
/*setfillstyle(1,LIGHTBLUE);
bar(45,420,365,470);*/ /*清理输出区*/
for(i=0;i<4;i++)for(j=0;j<4;j++)recorder[i][j]=-1;
for(i=0;i<6;i++) group[i]=NULL;
for(i=0;i<80;i++) key[i]=NULL; /*初始化简表*/
for(i=0,n=0;i<16;i++)if(kive[i]=='*')n++;
if(n==16)
{
outtextxy(61,436,"=1");
delay(1000000);
clear(); /*清理画图区域*/
setfillstyle(1,LIGHTBLUE);
bar(45,420,365,470); /*清理字符输出区域*/
/*closegraph();exit(0);*/
} /*溢出处理*/
for(i=0;i<16;i++)
if(kive[i]=='*')
{head=malloc(sizeof(queue));p=head;
for(j=0;j<14;j++,p++) *p=-1;
p=head;n=i;k=0;
if(n>=8) {*(p+10)=1;k++;n-=8;}else *(p+10)=0;
if(n>=4) {*(p+11)=1;k++;n-=4;}else *(p+11)=0;
if(n>=2) {*(p+12)=1;k++;n-=2;}else *(p+12)=0;
*(p+13)=n;if(n==1) k++;
*p=k;key[i]=head;*(p+1)=i;
} /*设置表初值*/
}
/*=================================化简函数====================================*/
void melt(void)
{
int i,*p,*q,flag0=0,flag1=0;
p=&flag0;sort(p);q=&flag1;
do
{
flag0=0;flag1=0;
balance(q);
for(i=0;i<6;i++)group[i]=NULL;sort(p);
}while(flag1&&flag0); /*化简至最简*/
simplelist(); /*找出多个结果*/
for(i=0,count=0;i<12;i++)
if(number[i]>-1)count++; /*count为多结果个数*/
/*total=count;*/
}
/*================================退出界面====================================*/
void exit_kive(void)
{
setfillstyle(1,RED);
bar(255,210,385,270);
setfillstyle(1,BROWN);
bar(260,215,380,265);
setcolor(MAGENTA);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(320,240,"Thanks");
getch();
cleardevice();
closegraph();
exit(0);
}
/*================================鼠标操作====================================*/
void mouse(void)
{
int size,newx=45,newy=75,back,state,x_max=640,y_max=480;
int initmouse(int maxx,int maxy);
void newxy(int *newx,int *mewy,int *state);
void cursor(int,int);
void cursorcover(int,int);
back=initmouse(x_max-1,y_max-1);
store=&S;
initstack(store); /*初始化栈为空*/
if(back==0)
{
printf("The mouse device hasn't been installed!");
delay(50000);
exit(1);
}
setwritemode(XOR_PUT);
cursor(45,75);
for(;;)
{
newxy(&newx,&newy,&state);
if(newx>=585&&newx<=615&&newy>=10&&newy<40&&state)
{
exit_kive();
}
}
}
void cursor(int x,int y) /*画一个鼠标*/
{
int i,d[]={0,0,7,12,9,11,15,17,17,15,11,9,12,7,0,0};
for(i=0;i<16;i++)
d[i]=i%2==0?x+d[i]:y+d[i];
setcolor(YELLOW);
moveto(d[0],d[1]) ;
for(i=0;i<16;i+=2)
lineto(d[i],d[i+1]);
}
int initmouse(int maxx,int maxy)
{ /*对鼠标器使用初始化*/
int recode;
reg.x.ax=0;
int86(VIDEO,®,®);
recode=reg.x.ax;
if(recode==0)return recode;
reg.x.dx=maxx-1;
reg.x.cx=1;
reg.x.ax=7;
int86(VIDEO,®,®);
reg.x.cx=1;
reg.x.dx=maxy-1;
reg.x.ax=8;
int86(VIDEO,®,®);
return recode;
}
int test(int *foundx,int *foundy,int *position) /*测试鼠标的新位置及按键状态*/
{
int tempx,tempy,tempp=*position;
do
{
reg.x.ax=3;
int86(VIDEO,®,®);
tempx=reg.x.cx;
tempy=reg.x.dx;
*position=reg.x.bx;
}while(*foundx==tempx&&*foundy==tempy&&*position==tempp);
*foundx=tempx;
*foundy=tempy;
if(*position)
return 1;
else
return 0;
}
/*(((((((((((((((((((((((((((((不同按钮的响应处理)))))))))))))))))))))))))))))))))))*/
void newxy(int *newx,int *newy,int *state) /*鼠标新位置处理*/
{
int tempx,tempy,flag,i,x,y;
int tempx0,tempy0,size;
tempx0=*newx;
tempy0=*newy;
setwritemode(XOR_PUT);
flag=test(&tempx,&tempy,state);/*check the new position and state*/
if(flag) /*There has been a button pressed*/
{
cursor(tempx0,tempy0);
cursor(tempx,tempy);
graph(9); /*用亮蓝色(背景色)画图*/
setcolor(RED);
if(tempx>=45&&tempy>=75&&tempx<=365&&tempy<=395) /*判断最小项标记*/
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
settextjustify(CENTER_TEXT,CENTER_TEXT);
for(i=0;i<16;i++) /*输出最小项标记*/
{
search(i,&x,&y);
if(tempx-x<=40&&tempy-y<=40&&x<=tempx&&y<=tempy)
{
outtextxy(x+20,y+20,"1");/*输出图框内的函数标记1*/
if(kive[i]=='*') kive[i]=-1;
else kive[i]='*';
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,0);
outtextxy(525,113+15*i,"1");
if(kive[i]==-1) /*点击后标志消失*/
{setcolor(BLUE);outtextxy(525,113+15*i,"1");}
setcolor(RED);
break;
}
}
initial();melt(); /*用亮红色画图*/
setfillstyle(1,LIGHTBLUE);
bar(45,420,365,470); /*清理输出区*/
output();graph(13);
while(count>0)
{
getch();graph(9);output();graph(13);
}
}
if(tempx>=521&&tempy>=100&&tempx<=535&&tempy<=340)
{
i=(int)((tempy-100)/15); /*输出真值表内的函数标记1*/
/*graph();*/
outtextxy(525,113+15*i,"1");
if(kive[i]=='*')
{setcolor(BLUE);outtextxy(525,113+15*i,"1");kive[i]=-1;setcolor(RED);}
else kive[i]='*';
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
settextjustify(CENTER_TEXT,CENTER_TEXT);
search(i,&x,&y);
outtextxy(x+20,y+20,"1");
initial();melt();
setfillstyle(1,LIGHTBLUE);
bar(45,420,365,470); /*清理输出区*/
output();graph(13);
while(count>0)
{
graph(9);output();graph(13);
}
}
if(tempx>=370&&tempy>=360&&tempx<=600&&tempy<=440)
{ /*接受输入字符*/
if(tempx>=375&&tempy>=360&&tempx<=405&&tempy<=390)
{outtextxy(53+cp*8,428,"A");push('A',store);cp++;}
if(tempx>=423&&tempy>=360&&tempx<=453&&tempy<=390)
{outtextxy(53+cp*8,428,"B");push('B',store);cp++;}
if(tempx>=471&&tempy>=360&&tempx<=501&&tempy<=390)
{outtextxy(53+cp*8,428,"C");push('C',store);cp++;}
if(tempx>=519&&tempy>=360&&tempx<=549&&tempy<=390)
{outtextxy(53+cp*8,428,"D");push('D',store);cp++;}
if(tempx>=567&&tempy>=360&&tempx<=597&&tempy<=390)
{outtextxy(53+cp*8,428,"'");push('\x27',store);cp++;}
if(tempx>=375&&tempy>=405&&tempx<=405&&tempy<=435)
{outtextxy(53+cp*8,428,"+");push('\x2b',store);cp++;}
/*if(tempx>=375&&tempy>=405&&tempx<=405&&tempy<=435)
{cp--;setcolor(BLUE);outtextxy(53+cp*8,428,"+");
pop(p);setcolor(RED);} */ /*输出回退*/
if(tempx>=425&&tempy>=405&&tempx<=500&&tempy<=435) /*清理图形区域*/
{
clear();initial();
setfillstyle(1,LIGHTBLUE);
bar(45,420,365,470); /*字符输出区清理*/
}
if(tempx>=519&&tempy>=405&&tempx<=579&&tempy<=435) /*输入完成*/
{
clear(); /*清理输出区域*/
input();initial();
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);/*输出图形区域标记*/
settextjustify(CENTER_TEXT,CENTER_TEXT);
for(i=0;i<16;i++)
if(kive[i]=='*')
{
search(i,&x,&y);outtextxy(x+20,y+20,"1");
}
settextstyle(DEFAULT_FONT,HORIZ_DIR,1); /*输出函数区域标记*/
settextjustify(LEFT_TEXT,0);
for(i=0;i<16;i++)
if(kive[i]=='*')
outtextxy(525,113+15*i,"1");
melt(); /*化简并画图*/
output();graph(13);
while(count>0)
{
graph(9);output();graph(13);
}
initstack(store); /*接受下一次输入*/
}
}
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,0);
}
else /*No buttons have been pressed but moved*/
{
cursor(tempx0,tempy0);
cursor(tempx,tempy);
}
*newx=tempx;
*newy=tempy;
}
/*=================================主函数====================================*/
main()
{
int graphdriver=VGA,graphmode=VGAHI;
initgraph(&graphdriver,&graphmode,"");
setbkcolor(BLUE);
setcolor(YELLOW);
cleardevice();
Frame_kive();
mouse();
closegraph();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -