高分求给我的程序找错
我写了个四则运算训练营,基本上已经是完成了;只是显示数字的时候后一个会将前一个覆盖;然而本人并没有写输出语句;
弄了好久没能找出原因,请各位帮助;主要是代码段:
int core(int x)
{
int i,j,key,sum=0,x1=160,y1=100,key1;
int a,b,n,c,temp;
char str1[2],str2[2],str3[2],op,cmp[200],result[20];
cleardevice();
Form(frmmain);
setfillstyle(SOLID_FILL,WHITE);
bar(150,90,450,380);
for(i=0;i<10;i++)
{
N_ew:
srand(i);
a=rand()%x+1;
srand(time(0)+i);
b=rand()%x+1;
srand(i*time(0));
n=rand()%4;
switch(n)
{
case 0 : op='+';break;
case 1 : op='-';break;
case 2 : op='*';break;
default : op='/';break;
}
if(n==0) c=addition(a,b);
else if(n==1)
{
if(a<b)
{
temp=a;
a=b;
b=temp;
}
c=subtraction(a,b);
}
else if(n==2) c=multiplication(a,b);
else if(n==3)
{
if(a%b!=0) goto N_ew;
else c=division(a,b);
}
sprintf(str1,"%d",a);
sprintf(str2,"%d",b);
setcolor(BLACK);
printf("%2s",str1);
printf("%2c",op);
printf("%2s",str2);
outtextxy(x1+85,y1+15,str1);
outtextxy(x1+105,y1+15,&op);
outtextxy(x1+135,y1+15,str2);
outtextxy(x1+155,y1+15,"=");
j=0;
while(j<200)
{
if(bioskey(1)==0) continue;
key1=bioskey(0);
switch(key1)
{ case NUM0: key1=0;break;
case KEY0: key1=0;break;
case NUM1: key1=1;break;
case KEY1: key1=1;break;
case NUM2: key1=2;break;
case KEY2: key1=2;break;
case NUM3: key1=3;break;
case KEY3: key1=3;break;
case NUM4: key1=4;break;
case KEY4: key1=4;break;
case NUM5: key1=5;break;
case KEY5: key1=5;break;
case NUM6: key1=6;break;
case KEY6: key1=6;break;
case NUM7: key1=7;break;
case KEY7: key1=7;break;
case NUM8: key1=8;break;
case KEY8: key1=8;break;
case NUM9: key1=9;break;
case KEY9: key1=9;break;
case ENTER: key1=10;break;
default : key1=-1;break;
}
if(key1==10) {x1-=(j*15);break;}
else if(key1<0) continue;
else
{
sprintf(cmp+j,"%d",key1);
outtextxy(x1+190,y1+15,&cmp[j]);
}
x1+=15;
j++;
}
sprintf(result,"%d",c);
printf("cmp%2s",cmp);
printf("result%2s",result);
if(strcmp(result,cmp)==0)
{
outtextxy(x1+130,y1+25,"you are right!");
sum+=1;
}
else outtextxy(x1+130,y1+25,"come on!");
y1+=20;
}
sprintf(str3,"%d%",sum*10);
outtextxy(290,325,"your per cent is:");
outtextxy(370,325,str3);
if(sum>=8)
{
outtextxy(300,345,"you are very good!try in twenty?(y,n)");
for(i=7;i<=8;i++)
CommandButton(cmdbutton[i]);
while(1)
{
if(bioskey(1)==0) continue;
key=bioskey(0);
switch(key)
{
case VK_Y: key=1;break;
case VK_y: key=1;break;
case VK_N: key=0;break;
case VK_n: key=0;break;
default : key=-1;break;
}
if(key<0) continue;
else break;
}
if(key==0) {CommandButton_KeyboardDown(8);delay(300000);CommandButton_KeyboardUp(8);operation();}
if(key==1) {CommandButton_KeyboardDown(7);delay(300000);CommandButton_KeyboardUp(7);core(19);}
}
else
{
outtextxy(295,345,"do you want to try again!(y,n)");
for(i=7;i<=8;i++)
CommandButton(cmdbutton[i]);
while(1)
{
if(bioskey(1)==0) continue;
key=bioskey(0);
switch(key)
{
case VK_Y: key=1;break;
case VK_y: key=1;break;
case VK_N: key=0;break;
case VK_n: key=0;break;
default : key=-1;break;
}
if(key<0) continue;
else break;
}
if(key==0) {CommandButton_KeyboardDown(8);delay(300000);CommandButton_KeyboardUp(8);operation();}
if(key==1) {CommandButton_KeyboardDown(7);delay(300000);CommandButton_KeyboardUp(7);core(9);}
}
}
这个里边的输出语句问题;
下面是整个程序的代码;请各位找找原因,帮助一下;谢谢!!!!
#include<stdio.h>
#include<bios.h>
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
#define VK_Y 0x1579
#define VK_y 0x1559
#define VK_n 0x314e
#define VK_N 0x316e
#define VK_R 0x1352
#define VK_r 0x1372
#define VK_H 0x2368
#define VK_h 0X2348
#define VK_S 0x1f73
#define VK_s 0x1f53
#define VK_ESC 0x11b
#define ENTER 0x1c0d
#define NUM0 0x5230
#define NUM1 0x4f31
#define NUM2 0x5032
#define NUM3 0x5133
#define NUM4 0x4b34
#define NUM5 0x4c35
#define NUM6 0x4d36
#define NUM7 0x4737
#define NUM8 0x4838
#define NUM9 0x4939
#define KEY0 0xb30
#define KEY1 0x231
#define KEY2 0x332
#define KEY3 0x433
#define KEY4 0x534
#define KEY5 0x635
#define KEY6 0x736
#define KEY7 0x837
#define KEY8 0x938
#define KEY9 0xa39
struct Block
{ int left,top,width,height;
char caption[50];
int fontcolor,fontsize,status;
};
struct Block frmmain,cmdbutton[11];
void unload();
void help();
void operation();
void show_intro(int xs,int ys);
void start();
int addition(int x,int y);
int subtraction(int x,int y);
int multiplication(int x,int y);
int division(int x,int y);
int core(int x);
void CommandButton(struct Block cmdbutton);
void CommandButton_KeyboardDown(int i);
void CommandButton_KeyboardUp(int key);
void Form(struct Block form);
void main()
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"");
setbkcolor(BLUE);
frmmain.left=100; frmmain.top=50; frmmain.width=400; frmmain.height=400; frmmain.fontcolor=BLACK; frmmain.fontsize=1; strcpy(frmmain.caption," << Arithmetic >>");frmmain.status=1;
cmdbutton[0].left=200-35;cmdbutton[0].top=125;cmdbutton[0].width=50;cmdbutton[0].height=30;cmdbutton[0].fontcolor=BLUE;cmdbutton[0].fontsize=1;strcpy(cmdbutton[0].caption,"START");cmdbutton[0].status=1;
cmdbutton[1].left=200-35;cmdbutton[1].top=155;cmdbutton[1].width=50;cmdbutton[1].height=30;cmdbutton[1].fontcolor=BLUE;cmdbutton[1].fontsize=1;strcpy(cmdbutton[1].caption,"HELP");cmdbutton[1].status=1;
cmdbutton[2].left=200-35;cmdbutton[2].top=185;cmdbutton[2].width=50;cmdbutton[2].height=30;cmdbutton[2].fontcolor=BLUE;cmdbutton[2].fontsize=1;strcpy(cmdbutton[2].caption,"EXIT");cmdbutton[2].status=1;
cmdbutton[3].left=200-35;cmdbutton[3].top=155;cmdbutton[3].width=50;cmdbutton[3].height=30;cmdbutton[3].fontcolor=BLUE;cmdbutton[3].fontsize=1;strcpy(cmdbutton[3].caption,"TWENTY");cmdbutton[3].status=1;
cmdbutton[4].left=200-35;cmdbutton[4].top=125;cmdbutton[4].width=50;cmdbutton[4].height=30;cmdbutton[4].fontcolor=BLUE;cmdbutton[4].fontsize=1;strcpy(cmdbutton[4].caption,"TEN");cmdbutton[4].status=1;
cmdbutton[5].left=80;cmdbutton[5].top=350;cmdbutton[5].width=50;cmdbutton[5].height=30;cmdbutton[5].fontcolor=BLUE;cmdbutton[5].fontsize=1;strcpy(cmdbutton[5].caption,"RETURN");cmdbutton[5].status=1;
cmdbutton[6].left=80+175;cmdbutton[6].top=350;cmdbutton[6].width=50;cmdbutton[6].height=30;cmdbutton[6].fontcolor=BLUE;cmdbutton[6].fontsize=1;strcpy(cmdbutton[6].caption,"EXIT");cmdbutton[6].status=1;
cmdbutton[7].left=80;cmdbutton[7].top=350;cmdbutton[7].width=50;cmdbutton[7].height=30;cmdbutton[7].fontcolor=BLUE;cmdbutton[7].fontsize=1;strcpy(cmdbutton[7].caption,"YES");cmdbutton[7].status=1;
cmdbutton[8].left=80+175;cmdbutton[8].top=350;cmdbutton[8].width=50;cmdbutton[8].height=30;cmdbutton[8].fontcolor=BLUE;cmdbutton[8].fontsize=1;strcpy(cmdbutton[8].caption,"NO");cmdbutton[8].status=1;
operation();
}
void unload()
{
cleardevice();
closegraph();
exit(0);
}
void operation()
{
int key,i;
Form(frmmain);
for (i=0;i<=2;i++)
CommandButton(cmdbutton[i]);
show_intro(150,300);
while(1)
{
if(bioskey(1)==0) continue;
key=bioskey(0);
switch(key)
{
case VK_S: key=1;break;
case VK_s: key=1;break;
case VK_H: key=2;break;
case VK_h: key=2;break;
case VK_ESC: key=0;break;
default : key=-1;break;
}
if(key<0) continue;
else break;
}
if(key==1) {CommandButton_KeyboardDown(0);delay(300000);CommandButton_KeyboardUp(0);start();}
if(key==2) {CommandButton_KeyboardDown(1);delay(300000);CommandButton_KeyboardUp(1);help();}
if(key==0) {CommandButton_KeyboardDown(2);delay(300000);CommandButton_KeyboardUp(2);unload();}
}
void start()
{
int key,i;
cleardevice();
Form(frmmain);
for(i=2;i<=4;i++)
CommandButton(cmdbutton[i]);
show_intro(150,300);
while(1)
{
if(bioskey(1)==0) continue;
key=bioskey(0);
switch(key)
{
case NUM1: key=1;break;
case KEY1: key=1;break;
case NUM2: key=2;break;
case KEY2: key=2;break;
case VK_ESC: key=0;break;
default : key=-1;break;
}
if(key<0) continue;
else break;
}
if(key==1) {CommandButton_KeyboardDown(4);delay(300000);CommandButton_KeyboardUp(4);core(9);}
if(key==2) {CommandButton_KeyboardDown(3);delay(300000);CommandButton_KeyboardUp(3);core(19);}
if(key==0) {CommandButton_KeyboardDown(2);delay(300000);CommandButton_KeyboardUp(2);unload();}
}
int core(int x)
{
int i,j,key,sum=0,x1=160,y1=100,key1;
int a,b,n,c,temp;
char str1[2],str2[2],str3[2],op,cmp[200],result[20];
cleardevice();
Form(frmmain);
setfillstyle(SOLID_FILL,WHITE);
bar(150,90,450,380);
for(i=0;i<10;i++)
{
N_ew:
srand(i);
a=rand()%x+1;
srand(time(0)+i);
b=rand()%x+1;
srand(i*time(0));
n=rand()%4;
switch(n)
{
case 0 : op='+';break;
case 1 : op='-';break;
case 2 : op='*';break;
default : op='/';break;
}
if(n==0) c=addition(a,b);
else if(n==1)
{
if(a<b)
{
temp=a;
a=b;
b=temp;
}
c=subtraction(a,b);
}
else if(n==2) c=multiplication(a,b);
else if(n==3)
{
if(a%b!=0) goto N_ew;
else c=division(a,b);
}
sprintf(str1,"%d",a);
sprintf(str2,"%d",b);
setcolor(BLACK);
printf("%2s",str1);
printf("%2c",op);
printf("%2s",str2);
outtextxy(x1+85,y1+15,str1);
outtextxy(x1+105,y1+15,&op);
outtextxy(x1+135,y1+15,str2);
outtextxy(x1+155,y1+15,"=");
j=0;
while(j<200)
{
if(bioskey(1)==0) continue;
key1=bioskey(0);
switch(key1)
{ case NUM0: key1=0;break;
case KEY0: key1=0;break;
case NUM1: key1=1;break;
case KEY1: key1=1;break;
case NUM2: key1=2;break;
case KEY2: key1=2;break;
case NUM3: key1=3;break;
case KEY3: key1=3;break;
case NUM4: key1=4;break;
case KEY4: key1=4;break;
case NUM5: key1=5;break;
case KEY5: key1=5;break;
case NUM6: key1=6;break;
case KEY6: key1=6;break;
case NUM7: key1=7;break;
case KEY7: key1=7;break;
case NUM8: key1=8;break;
case KEY8: key1=8;break;
case NUM9: key1=9;break;
case KEY9: key1=9;break;
case ENTER: key1=10;break;
default : key1=-1;break;
}
if(key1==10) {x1-=(j*15);break;}
else if(key1<0) continue;
else
{
sprintf(cmp+j,"%d",key1);
outtextxy(x1+190,y1+15,&cmp[j]);
}
x1+=15;
j++;
}
sprintf(result,"%d",c);
printf("cmp%2s",cmp);
printf("result%2s",result);
if(strcmp(result,cmp)==0)
{
outtextxy(x1+130,y1+25,"you are right!");
sum+=1;
}
else outtextxy(x1+130,y1+25,"come on!");
y1+=20;
}
sprintf(str3,"%d%",sum*10);
outtextxy(290,325,"your per cent is:");
outtextxy(370,325,str3);
if(sum>=8)
{
outtextxy(300,345,"you are very good!try in twenty?(y,n)");
for(i=7;i<=8;i++)
CommandButton(cmdbutton[i]);
while(1)
{
if(bioskey(1)==0) continue;
key=bioskey(0);
switch(key)
{
case VK_Y: key=1;break;
case VK_y: key=1;break;
case VK_N: key=0;break;
case VK_n: key=0;break;
default : key=-1;break;
}
if(key<0) continue;
else break;
}
if(key==0) {CommandButton_KeyboardDown(8);delay(300000);CommandButton_KeyboardUp(8);operation();}
if(key==1) {CommandButton_KeyboardDown(7);delay(300000);CommandButton_KeyboardUp(7);core(19);}
}
else
{
outtextxy(295,345,"do you want to try again!(y,n)");
for(i=7;i<=8;i++)
CommandButton(cmdbutton[i]);
while(1)
{
if(bioskey(1)==0) continue;
key=bioskey(0);
switch(key)
{
case VK_Y: key=1;break;
case VK_y: key=1;break;
case VK_N: key=0;break;
case VK_n: key=0;break;
default : key=-1;break;
}
if(key<0) continue;
else break;
}
if(key==0) {CommandButton_KeyboardDown(8);delay(300000);CommandButton_KeyboardUp(8);operation();}
if(key==1) {CommandButton_KeyboardDown(7);delay(300000);CommandButton_KeyboardUp(7);core(9);}
}
}
int addition(int x,int y)
{
int z;
z=x+y;
return z;
}
int subtraction(int x,int y)
{
int z;
z=x-y;
return z;
}
int multiplication(int x,int y)
{
int z;
z=x*y;
return z;
}
int division(int x,int y)
{
int z;
z=x/y;
return z;
}
void help()
{
int i,key;
cleardevice();
Form(frmmain);
setcolor (14);
rectangle(150,150,450,300);
setcolor(14);
outtextxy(300,160,"*******************************");
outtextxy(275,185,"welcome to");
outtextxy(300,205,"the four fundamental operations");
outtextxy(280,230," by 2h");
outtextxy(300,255,"*******************************");
for(i=5;i<=6;i++)
CommandButton(cmdbutton[i]);
while(1)
{
if(bioskey(1)==0) continue;
key=bioskey(0);
switch(key)
{
case VK_R: key=1;break;
case VK_r: key=1;break;
case VK_ESC: key=2;break;
default : key=-1;break;
}
if(key<0) continue;
else break;
}
if(key==1) {CommandButton_KeyboardDown(5);delay(300000);CommandButton_KeyboardUp(5);operation();}
if(key==2) {CommandButton_KeyboardDown(6);delay(300000);CommandButton_KeyboardUp(6);unload();}
}
void show_intro(int xs,int ys)
{
setcolor (14);
rectangle(xs,ys,xs+300,ys+100);
outtextxy(xs+150,ys+25,"S--Start 1--IN TEN");
setcolor(14);
outtextxy(xs+150,ys+50,"R--Return 2--IN TWENTY");
outtextxy(xs+150,ys+75,"Esc--Exit ");
}
void CommandButton_KeyboardDown(int i)
{
cmdbutton[i].status=0;
CommandButton(cmdbutton[i]);
}
void CommandButton_KeyboardUp(int key)
{
cmdbutton[key].status=1;
CommandButton(cmdbutton[key]);
}
void CommandButton(struct Block cmdbutton)
{
int x1=cmdbutton.left+frmmain.left;
int y1=cmdbutton.top+frmmain.top;
int x2=cmdbutton.width+x1-1;
int y2=cmdbutton.height+y1-1;
int c1,c2;
if(cmdbutton.status)
{ c1=WHITE;
c2=DARKGRAY;
}
else
{
c1=DARKGRAY;
c2=WHITE;
}
setcolor(c1);
line(x1,y1,x2,y1);
line(x1,y1,x1,y2);
setcolor(c2);
line(x2,y1,x2,y2);
line(x1,y2,x2,y2);
settextjustify(CENTER_TEXT,CENTER_TEXT);
settextstyle(DEFAULT_FONT,0,cmdbutton.fontsize);
outtextxy((x1+x2)/2,(y1+y2)/2,cmdbutton.caption);
}
void Form(struct Block form)
{ int x1=form.left;
int y1=form.top;
int x2=form.width+x1-1;
int y2=form.height+y1-1;
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x1+1,y1+1,x2-1,y2-1);
setcolor(WHITE);
line(x1,y1,x2,y1);
line(x1,y1,x1,y2);
setcolor(DARKGRAY);
line(x2,y1,x2,y2);
line(x1,y2,x2,y2);
setfillstyle(SOLID_FILL,RED);
bar(x1+2,y1+2,x2-2,y1+15);
settextjustify(LEFT_TEXT,CENTER_TEXT);
settextstyle(DEFAULT_FONT,0,form.fontsize);
setcolor(form.fontcolor);
outtextxy(x1+3,y1+10,form.caption);
if(form.status&1)
{ cmdbutton[10].left=form.width-15;
cmdbutton[10].top=3;
cmdbutton[10].width=12;
cmdbutton[10].height=12;
cmdbutton[10].status=1;
cmdbutton[10].caption[0]=0;
CommandButton(cmdbutton[10]);
x1=cmdbutton[10].left+form.left;
y1=cmdbutton[10].top+form.top;
x2=cmdbutton[10].width+x1-1;
y2=cmdbutton[10].height+y1-1;
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x1+1,y1+1,x2-1,y2-1);
setcolor(DARKGRAY);
line(x1+2,y1+2,x2-2,y2-2);
line(x2-2,y1+2,x1+2,y2-2);
}
}