A. 尋求c語言版的俄羅斯方塊程序
#include<stdio.h>
#include<string.h>
#include<bios.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
int dx[4],dy[4]; /*定義全局變數*/
int zt1,zt2,str[15][19];
/*str[15][19]是把整個屏幕分為15*19個方格,每一個方格用一個數組單元表示,
如果=15,則這個方格已被佔用,=0,則還是空的*/
int cx[8][5][4],cy[8][5][4]; /*該變數表示每種狀態下,旋轉時坐標的改變*/
int x,y,j,ji,c;
int maxzt[8]={0,2,1,4,2,2,4,4}; /*各個種類的方塊分別有幾種狀態*/
cir() /*旋轉的處理函數*/
{ dx[0]=dx[0]+cx[zt1][zt2][0];dy[0]=dy[0]+cy[zt1][zt2][0];
dx[2]=dx[2]+cx[zt1][zt2][2];dy[2]=dy[2]+cy[zt1][zt2][2];
dx[3]=dx[3]+cx[zt1][zt2][3];dy[3]=dy[3]+cy[zt1][zt2][3];
}
jiance() /*檢測旋轉或移動能否進行的函數,能則j=1,不能j=0*/
{ j=1;
for(ji=0;ji<4;ji++)
{ x=dx[ji];y=dy[ji];
if(str[x][y]!=' ') j=0;
}
c=bioskey(1);
if(c!=0) c=bioskey(0);
}
main()
{ int dotx[4],doty[4],score; /*dotx[]doty[]表示一個方塊個點的坐標*/
int ddx,ddy;
int rzt1,rzt2,i,u,t=1;
int a[5],b[11],o,p,an,bn;
int rotx[4],roty[4],spd=0;
begin: system("cls"); /*游戲初始化階段*/
printf("londing...");
for(i=0;i<12;i++) /*變數初始階段*/
{ for(u=0;u<19;u++)
str[i][u]=' ';
}
for(i=0;i<12;i++)
{ str[i][0]='-';str[i][18]='-'; }
for(u=0;u<19;u++)
{ str[0][u]='|';str[11][u]='|'; }
cx[1][1][0]=1;cx[1][1][2]=-1;cx[1][1][3]=-2; /*對旋轉變數進行賦值*/
cy[1][1][0]=1;cy[1][1][2]=-1;cy[1][1][3]=-2;
cx[1][2][0]=-1;cx[1][2][2]=1;cx[1][2][3]=2;
cy[1][2][0]=-1;cy[1][2][2]=1;cy[1][2][3]=2;
cx[2][1][0]=0;cx[2][1][2]=0;cx[2][1][3]=0;
cy[2][1][0]=0;cy[2][1][2]=0;cy[2][1][3]=0;
cx[3][1][0]=1;cx[3][1][2]=-1;cx[3][1][3]=1;
cy[3][1][0]=-1;cy[3][1][2]=1;cy[3][1][3]=1;
cx[3][2][0]=1;cx[3][2][2]=-1;cx[3][2][3]=-1;
cy[3][2][0]=1;cy[3][2][2]=-1;cy[3][2][3]=1;
cx[3][3][0]=-1;cx[3][3][2]=1;cx[3][3][3]=-1;
cy[3][3][0]=1;cy[3][3][2]=-1;cy[3][3][3]=-1;
cx[3][4][0]=-1;cx[3][4][2]=1;cx[3][4][3]=1;
cy[3][4][0]=-1;cy[3][4][2]=1;cy[3][4][3]=-1;
cx[4][1][0]=-1;cx[4][1][2]=1;cx[4][1][3]=2;
cy[4][1][0]=1;cy[4][1][2]=1;cy[4][1][3]=0;
cx[4][2][0]=1;cx[4][2][2]=-1;cx[4][2][3]=-2;
cy[4][2][0]=-1;cy[4][2][2]=-1;cy[4][2][3]=0;
cx[5][1][0]=1;cx[5][1][2]=1;cx[5][1][3]=0;
cy[5][1][0]=-1;cy[5][1][2]=1;cy[5][1][3]=2;
cx[5][2][0]=-1;cx[5][2][2]=-1;cx[5][2][3]=0;
cy[5][2][0]=1;cy[5][2][2]=-1;cy[5][2][3]=-2;
cx[6][1][0]=1;cx[6][1][2]=-1;cx[6][1][3]=0;
cy[6][1][0]=-1;cy[6][1][2]=1;cy[6][1][3]=2;
cx[6][2][0]=1;cx[6][2][2]=-1;cx[6][2][3]=-2;
cy[6][2][0]=1;cy[6][2][2]=-1;cy[6][2][3]=0;
cx[6][3][0]=-1;cx[6][3][2]=1;cx[6][3][3]=0;
cy[6][3][0]=1;cy[6][3][2]=-1;cy[6][3][3]=-2;
cx[6][4][0]=-1;cx[6][4][2]=1;cx[6][4][3]=2;
cy[6][4][0]=-1;cy[6][4][2]=1;cy[6][4][3]=0;
cx[7][1][0]=-1;cx[7][1][2]=1;cx[7][1][3]=2;
cy[7][1][0]=1;cy[7][1][2]=-1;cy[7][1][3]=0;
cx[7][2][0]=-1;cx[7][2][2]=1;cx[7][2][3]=0;
cy[7][2][0]=-1;cy[7][2][2]=1;cy[7][2][3]=2;
cx[7][3][0]=1;cx[7][3][2]=-1;cx[7][3][3]=-2;
cy[7][3][0]=-1;cy[7][3][2]=1;cy[7][3][3]=0;
cx[7][4][0]=1;cx[7][4][2]=-1;cx[7][4][3]=0;
cy[7][4][0]=1;cy[7][4][2]=-1;cy[7][4][3]=-2;
srand(time(0)); /*對隨機數函數rand()進行初始化*/
zt1=rand()%7+1; /*生成第一、二個方塊*/
if(zt1==2) zt2=1;
if(zt1==1||zt1==4||zt1==5) zt2=rand()%2+1;
if(zt1==3||zt1==6||zt1==7) zt2=rand()%4+1;
rzt1=rand()%7+1;
if(rzt1==2) rzt2=1;
if(rzt1==1||rzt1==4||rzt1==5) rzt2=rand()%2+1;
if(rzt1==3||rzt1==6||rzt1==7) rzt2=rand()%4+1;
score=0;
for(o=1;o<11;o++) b[o]=0;
switch(zt1*10+zt2)
/*zt1和zt2分別代表方塊的種類和狀態,這步是根據這兩個變數確定方塊的四個點的坐標*/
{ case 11: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=7;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=2;
break;
case 12: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=5;
doty[0]=4;doty[1]=3;doty[2]=2;doty[3]=1;
break;
case 21: dotx[0]=5;dotx[1]=6;dotx[2]=5;dotx[3]=6;
doty[0]=1;doty[1]=1;doty[2]=2;doty[3]=2;
break;
case 31: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=5;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 32: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=2;
break;
case 33: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=5;
doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2;
break;
case 34: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5;
doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=2;
break;
case 41: dotx[0]=6;dotx[1]=5;dotx[2]=5;dotx[3]=4;
doty[0]=2;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 42: dotx[0]=5;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=3;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 51: dotx[0]=4;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=2;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 52: dotx[0]=5;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=1;doty[1]=2;doty[2]=2;doty[3]=3;
break;
case 61: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 62: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=3;
break;
case 63: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=4;
doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2;
break;
case 64: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5;
doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 71: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=4;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 72: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 73: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2;
break;
case 74: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5;
doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=3;
break;
}
switch(rzt1*10+rzt2) /*確定第二個方塊各個點的坐標*/
{ case 11: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=7;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=2;
break;
case 12: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=5;
roty[0]=4;roty[1]=3;roty[2]=2;roty[3]=1;
break;
case 21: rotx[0]=5;rotx[1]=6;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=2;roty[3]=2;
break;
case 31: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=5;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 32: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=2;
break;
case 33: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=5;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 34: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=2;
break;
case 41: rotx[0]=6;rotx[1]=5;rotx[2]=5;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 42: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 51: rotx[0]=4;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 52: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=2;roty[3]=3;
break;
case 61: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 62: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
case 63: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 64: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 71: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 72: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 73: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 74: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
}
system("cls"); /*顯示初始階段*/
printf("\n\n\n"); /*游戲區域下移3*/
for(u=0;u<19;u++)
{ for(i=0;i<12;i++)
printf("%c",str[i][u]);
printf("\n");
}
gotoxy(16,5);printf("--------");
gotoxy(16,12);printf("--------");
for(i=6;i<12;i++)
{ gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); }
for(i=6;i<12;i++)
{ gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); }
for(i=0;i<4;i++)
{ gotoxy(rotx[i]+14,roty[i]+6);printf("%c",15);
}
begin2: delay(26000); /*游戲開始,延遲1*/
speed: delay(10000); /*加速,延遲2*/
gotoxy(16,14);printf("Score:%d",score);
for(i=0;i<4;i++)
{ gotoxy(dotx[i]+1,doty[i]+4);printf(" ");
ddx=dotx[i];ddy=doty[i];
str[ddx][ddy]=' ';
}
an=an-0.4; /*表示按鍵是否一直按著,用於方塊落地後的移動*/
c=bioskey(1); /*按鍵處理部分*/
/*bioskey(1)是用來檢測是否按下案件的函數*/
if(c!=0)
{ c=bioskey(0);
if(c==8292||c==19712)
{ for(i=0;i<4;i++)
{ dx[i]=dotx[i]+1;dy[i]=doty[i]; }
jiance();
for(i=0;i<4;i++)
dotx[i]=(j)? dx[i] : dotx[i];
an=(j||bn);
}
if(c==7777||c==19200)
{ for(i=0;i<4;i++)
{ dx[i]=dotx[i]-1;dy[i]=doty[i]; }
jiance();
for(i=0;i<4;i++)
dotx[i]=(j)? dx[i] : dotx[i];
an=(j||bn);
}
if(c==6512) /*暫停的處理*/
{ while(1)
{ c=bioskey(0);
if(c==6512) break;
}
goto begin3;
}
if(c==8051||c==20480) spd=1; /*加速(spd==1表示加速狀態)*/
if(c==4471||c==18432) /*旋轉的處理*/
{ for(i=0;i<4;i++)
{ dx[i]=dotx[i];dy[i]=doty[i]; }
/*dx[]與dy[]是臨時變數,這樣一旦判斷為不能旋轉,就可方便的回復旋轉前的坐標*/
cir(); /*旋轉*/
jiance(); /*判斷旋轉是否能進行*/
for(i=0;i<4;i++)
{ dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; }
/*根據jiance()得到的j值,判斷是對dotx[]與doty[]賦旋轉後的還是旋轉前的值*/
if(j==1) /*如果旋轉可已經行,就對原方塊的狀態進行改變*/
{ an=(j||bn);zt2=zt2+1;
if(zt2>maxzt[zt1]) zt2=1;
goto overif; /*結束旋轉的處理*/
}
for(i=0;i<4;i++)
{ dx[i]=dotx[i]+1;dy[i]=doty[i]; }
/*如果不能旋轉,再判斷坐標右移一個後能否旋轉*/
cir();
jiance();
for(i=0;i<4;i++)
{ dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; }
if(j==1)
{ an=(j||bn);zt2=zt2+1;
if(zt2>maxzt[zt1]) zt2=1;
goto overif;
}
if(dotx[2]==1) goto overif;
for(i=0;i<4;i++)
{ dx[i]=dotx[i]-1;dy[i]=doty[i]; }
/*判斷坐標左移一個後能否旋轉*/
cir();
jiance();
for(i=0;i<4;i++)
{ dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; }
if(j==1)
{ an=(j||bn);zt2=zt2+1;
if(zt2>maxzt[zt1]) zt2=1;
goto overif;
}
overif: ;
}
}
begin3: for(i=0;i<4;i++) /*方塊下移的處理*/
{ dx[i]=dotx[i];dy[i]=doty[i]+1; }
jiance();
bn=j;
for(i=0;i<4;i++)
doty[i]=(j)? dy[i] : doty[i];
for(i=0;i<4;i++)
{ gotoxy(dotx[i]+1,doty[i]+4);printf("%c",15);
ddx=dotx[i];ddy=doty[i];
str[ddx][ddy]=15;
}
if(j==1&&spd==1) { spd=0;goto speed; }
if(j==1||an>0) goto begin2;
for(u=17;u>0;u--) /*方塊停止下移(方塊移動到底了)的處理*/
{ for(i=1;i<11;i++) /*判斷每一行是否排滿*/
if(str[i][u]==15) b[i]=1;
if (b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9]+b[10]<10)
{ for(o=1;o<11;o++) b[o]=0;
continue;
}
for(o=1;o<11;o++) b[o]=0;
a[t]=u;t++;
}
score+=(t)*(t-1)/2;
for(i=1;i<11;i++)
if(str[i][1]==15) b[i]=1;
if (b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9]+b[10]>0 &&t==1) goto over;
for(o=1;o<11;o++) b[o]=0;
if(t==1) goto ran;
switch(t) /*消除方塊的處理,t=要消除的函數+1*/
{ case 5: for(u=a[4];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; }
/*將要消除的行中,最上面一行,上面的方格整體下移,下面的case 4,3,2類似*/
case 4: for(u=a[3];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; }
case 3: for(u=a[2];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; }
case 2: for(u=a[1];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; }
}
t=1;
for(u=1;u<18;u++)
{ for(i=1;i<11;i++)
{ gotoxy(i+1,u+4);
printf("%c",str[i][u]);
}
}
ran: zt1=rzt1;zt2=rzt2;rzt1=rand()%7+1; /*生成下兩個方塊*/
if(rzt1==2) rzt2=1;
if(rzt1==1||rzt1==4||rzt1==5) rzt2=rand()%2+1;
if(rzt1==3||rzt1==6||rzt1==7) rzt2=rand()%4+1;
for(i=0;i<4;i++)
{ dotx[i]=rotx[i];doty[i]=roty[i];
gotoxy(dotx[i]+1,doty[i]+4);printf("%c",15);
}
switch(rzt1*10+rzt2)
{ case 11: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=7;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=2;
break;
case 12: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=5;
roty[0]=4;roty[1]=3;roty[2]=2;roty[3]=1;
break;
case 21: rotx[0]=5;rotx[1]=6;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=2;roty[3]=2;
break;
case 31: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=5;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 32: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=2;
break;
case 33: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=5;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 34: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=2;
break;
case 41: rotx[0]=6;rotx[1]=5;rotx[2]=5;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 42: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 51: rotx[0]=4;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 52: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=2;roty[3]=3;
break;
case 61: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 62: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
case 63: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 64: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 71: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 72: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 73: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 74: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
}
for(i=6;i<12;i++) /*刷新一下用來顯示下一個方塊的那個區域*/
{ gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); }
for(i=6;i<12;i++)
for(u=17;u<23;u++) { gotoxy(u,i);printf("%c",32); }
for(i=0;i<4;i++)
{ gotoxy(rotx[i]+14,roty[i]+6);printf("%c",15); }
c=bioskey(1);an=0;
if(c!=0) c=bioskey(0);
if(spd==1) { spd=0;goto speed; }
goto begin2;
over: system("cls");
gotoxy(36,11);printf("GAME OVER");
bioskey(0);
system("cls");
printf("Your score is %d\n\n",score);
printf("Press 'Q' to exit\nIf you want to play again,please press other keys.");
score=0;
c=bioskey(0);
if(c!=4209) goto begin;
}
B. C語言第一單元單元測試題,今天必須交了,好難啊,哪位大哥幫忙解決下,在線等,最好給個100分啊!
22D 23B 24B 25C 26A 27D 28B 29D 30C 31A 32D 33不會 34B 35B 36D 37A 38C 39C 40A
題目都挺有特點,最多有兩三道錯的。
C. C語言求最大值。
int max_x, max_y, max_z;
max=0;
for(i=0;i<319;i++){
scanf("%d%d%d",&x,&y,&z);
ans[i]=(exp(0.25*(a*x*x+b*y*y+c*z*z)))/(8*PI*sqrt(PI)*sqrt(a1)*sqrt(b1)*sqrt(c1));
if(ans[i]>max)
{
max=ans[i];
max_x = x;
max_y = y;
max_z = z;
}
}
printf("X=%d,Y=%d,Z=%d %.4lf\n",max_x, max_y, max_z, max);
D. C語言 學生成績管理系統
我找的一個代碼!試一下:
c語言學生成績管理系統
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int shoudsave=0; /* */
struct student
{
char num[10];/* 學號 */
char name[20];
char sex[4];
int cgrade;
int mgrade;
int egrade;
int totle;
int ave;
char neartime[10];/* 最近更新時間 */
};
typedef struct node
{
struct student data;
struct node *next;
}Node,*Link;
void menu()
{
printf("********************************************************************************");
printf("\t1登記學生資料\t\t\t\t\t2刪除學生資料\n");
printf("\t3查詢學生資料\t\t\t\t\t4修改學生資料\n");
printf("\t5保存學生資料\t\t\t\t\t0退出系統\n");
printf("********************************************************************************\n");
}
void printstart()
{
printf("-----------------------------------------------------------------------\n");
}
void Wrong()
{
printf("\n=====>提示:輸入錯誤!\n");
}
void Nofind()
{
printf("\n=====>提示:沒有找到該學生!\n");
}
void printc() /* 本函數用於輸出中文 */
{
printf(" 學號\t 姓名 性別 英語成績 數學成績 C語言成績 總分 平均分\n");
}
void printe(Node *p)/* 本函數用於輸出英文 */
{
printf("%-12s%s\t%s\t%d\t%d\t%d\t %d\t %d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade,p->data.totle,p->data.ave);
}
Node* Locate(Link l,char findmess[],char nameornum[]) /* 該函數用於定位連表中符合要求的接點,並返回該指針 */
{
Node *r;
if(strcmp(nameornum,"num")==0) /* 按學號查詢 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
}
}
else if(strcmp(nameornum,"name")==0) /* 按姓名查詢 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->data.name,findmess)==0)
return r;
r=r->next;
}
}
return 0;
}
void Add(Link l) /* 增加學生 */
{
Node *p,*r,*s;
char num[10];
r=l;
s=l->next;
while(r->next!=NULL)
r=r->next; /* 將指針置於最末尾 */
while(1)
{
printf("請你輸入學號(以'0'返回上一級菜單:)");
scanf("%s",num);
if(strcmp(num,"0")==0)
break;
while(s)
{
if(strcmp(s->data.num,num)==0)
{
printf("=====>提示:學號為'%s'的學生已經存在,若要修改請你選擇'4 修改'!\n",num);
printstart();
printc();
printe(s);
printstart();
printf("\n");
return;
}
s=s->next;
}
p=(Node *)malloc(sizeof(Node));
strcpy(p->data.num,num);
printf("請你輸入姓名:");
scanf("%s",p->data.name);
getchar();
printf("請你輸入性別:");
scanf("%s",p->data.sex);
getchar();
printf("請你輸入c語言成績:");
scanf("%d",&p->data.cgrade);
getchar();
printf("請你輸入數學成績:");
scanf("%d",&p->data.mgrade);
getchar();
printf("請你輸入英語成績:");
scanf("%d",&p->data.egrade);
getchar();
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle / 3;
/* 信息輸入已經完成 */
p->next=NULL;
r->next=p;
r=p;
shoudsave=1;
}
}
void Qur(Link l) /* 查詢學生 */
{
int sel;
char findmess[20];
Node *p;
if(!l->next)
{
printf("\n=====>提示:沒有資料可以查詢!\n");
return;
}
printf("\n=====>1按學號查找\n=====>2按姓名查找\n");
scanf("%d",&sel);
if(sel==1)/* 學號 */
{
printf("請你輸入要查找的學號:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
printf("\t\t\t\t查找結果\n");
printstart();
printc();
printe(p);
printstart();
}
else
Nofind();
}
else if(sel==2) /* 姓名 */
{
printf("請你輸入要查找的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{
printf("\t\t\t\t查找結果\n");
printstart();
printc();
printe(p);
printstart();
}
else
Nofind();
}
else
Wrong();
}
void Del(Link l) /* 刪除 */
{
int sel;
Node *p,*r;
char findmess[20];
if(!l->next)
{
printf("\n=====>提示:沒有資料可以刪除!\n");
return;
}
printf("\n=====>1按學號刪除\n=====>2按姓名刪除\n");
scanf("%d",&sel);
if(sel==1)
{
printf("請你輸入要刪除的學號:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:該學生已經成功刪除!\n");
shoudsave=1;
}
else
Nofind();
}
else if(sel==2)
{
printf("請你輸入要刪除的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:該學生已經成功刪除!\n");
shoudsave=1;
}
else
Nofind();
}
else
Wrong();
}
void Modify(Link l)
{
Node *p;
char findmess[20];
if(!l->next)
{
printf("\n=====>提示:沒有資料可以修改!\n");
return;
}
printf("請你輸入要修改的學生學號:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{
printf("請你輸入新學號(原來是%s):",p->data.num);
scanf("%s",p->data.num);
printf("請你輸入新姓名(原來是%s):",p->data.name);
scanf("%s",p->data.name);
getchar();
printf("請你輸入新性別(原來是%s):",p->data.sex);
scanf("%s",p->data.sex);
printf("請你輸入新的c語言成績(原來是%d分):",p->data.cgrade);
scanf("%d",&p->data.cgrade);
getchar();
printf("請你輸入新的數學成績(原來是%d分):",p->data.mgrade);
scanf("%d",&p->data.mgrade);
getchar();
printf("請你輸入新的英語成績(原來是%d分):",p->data.egrade);
scanf("%d",&p->data.egrade);
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle/3;
printf("\n=====>提示:資料修改成功!\n");
shoudsave=1;
}
else
Nofind();
}
void Disp(Link l)
{
int count=0;
Node *p;
p=l->next;
if(!p)
{
printf("\n=====>提示:沒有資料可以顯示!\n");
return;
}
printf("\t\t\t\t顯示結果\n");
printstart();
printc();
printf("\n");
while(p)
{
printe(p);
p=p->next;
}
printstart();
printf("\n");
}
void Tongji(Link l)
{
Node *pm,*pe,*pc,*pt,*pa; /* 用於指向分數最高的接點 */
Node *r=l->next;
if(!r)
{
printf("\n=====>提示:沒有資料可以統計!\n");
return ;
}
pm=pe=pc=pt=pa=r;
while(r!=NULL)
{
if(r->data.cgrade>=pc->data.cgrade)
pc=r;
if(r->data.mgrade>=pm->data.mgrade)
pm=r;
if(r->data.egrade>=pe->data.egrade)
pe=r;
if(r->data.totle>=pt->data.totle)
pt=r;
if(r->data.ave>=pa->data.ave)
pa=r;
r=r->next;
}
printf("------------------------------統計結果--------------------------------\n");
printf("總分最高者:\t%s %d分\n",pt->data.name,pt->data.totle);
printf("平均分最高者:\t%s %d分\n",pa->data.name,pa->data.ave);
printf("英語最高者:\t%s %d分\n",pe->data.name,pe->data.egrade);
printf("數學最高者:\t%s %d分\n",pm->data.name,pm->data.mgrade);
printf("c語言最高者:\t%s %d分\n",pc->data.name,pc->data.cgrade);
printstart();
}
void Sort(Link l)
{
Link ll;
Node *p,*rr,*s;
ll=(Link)malloc(sizeof(Node)); /* 用於做新的連表 */
ll->next=NULL;
if(l->next==NULL)
{
printf("\n=====>提示:沒有資料可以排序!\n");
return ;
}
p=l->next;
while(p)
{
s=(Node*)malloc(sizeof(Node)); /* 新建接點用於保存信息 */
s->data=p->data;
s->next=NULL;
rr=ll;
while(rr->next!=NULL && rr->next->data.totle>=p->data.totle)
rr=rr->next;
if(rr->next==NULL)
rr->next=s;
else
{
s->next=rr->next;
rr->next=s;
}
p=p->next;
}
free(l);
l->next=ll->next;
printf("\n=====>提示:排序已經完成!\n");
}
void Save(Link l)
{
FILE* fp;
Node *p;
int flag=1,count=0;
fp=fopen("c:\\student","wb");
if(fp==NULL)
{
printf("\n=====>提示:重新打開文件時發生錯誤!\n");
exit(1);
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p->next;
count++;
}
else
{
flag=0;
break;
}
}
if(flag)
{
printf("\n=====>提示:文件保存成功.(有%d條記錄已經保存.)\n",count);
shoudsave=0;
}
fclose(fp);
}
void main()
{
Link l;/* 連表 */
FILE *fp; /* 文件指針 */
int sel;
char ch;
char jian;
int count=0;
Node *p,*r;
printf("\t\t\t\t學生成績管理系統\n\t\t\t\t-------福建農業職業學院計應0501 黃歡(32號)\n");
l=(Node*)malloc(sizeof(Node));
l->next=NULL;
r=l;
fp=fopen("C:\\student","rb");
if(fp==NULL)
{
printf("\n=====>提示:文件還不存在,是否創建?(y/n)\n");
scanf("%c",&jian);
if(jian=='y'||jian=='Y')
fp=fopen("C:\\student","wb");
else
exit(0);
}
printf("\n=====>提示:文件已經打開,正在導入記錄......\n");
while(!feof(fp))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp)) /* 將文件的內容放入接點中 */
{
p->next=NULL;
r->next=p;
r=p; /* 將該接點掛入連中 */
count++;
}
}
fclose(fp); /* 關閉文件 */
printf("\n=====>提示:記錄導入完畢,共導入%d條記錄.\n",count);
while(1)
{
menu();
printf("請你選擇操作:");
scanf("%d",&sel);
if(sel==0)
{
if(shoudsave==1)
{ getchar();
printf("\n=====>提示:資料已經改動,是否將改動保存到文件中(y/n)?\n");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(l);
}
printf("\n=====>提示:你已經退出系統,再見!\n");
break;
}
switch(sel)
{
case 1:Add(l);break; /* 增加學生 */
case 2:Del(l);break;/* 刪除學生 */
case 3:Qur(l);break;/* 查詢學生 */
case 4:Modify(l);break;/* 修改學生 */
case 5:Save(l);break;/* 保存學生 */
case 9:printf("\t\t\t==========幫助信息==========\n");break;
default: Wrong();getchar();break;
}
}
}
/* */
/*
學生成績管理程序
編制一個統計學生考試分數的管理程序。
設學生成績已以一個學生一個記錄的形式存儲在文件中,
每位學生記錄包含的信息有:姓名,學號和各門功課的成績。
程序具有以下幾項功能:求出各門課程的總分,平均分,按姓名,
按學號尋找其記錄並顯示,瀏覽全部學生成績和按總分由高到低顯示學生信息等。
*/
#include <stdio.h>
#define SWN 3 /* 課程數 */
#define NAMELEN 20 /* 姓名最大字元數 */
#define CODELEN 10 /* 學號最大字元數 */
#define FNAMELEN 80 /* 文件名最大字元數 */
#define BUFLEN 80 /* 緩沖區最大字元數 */
/* 課程名稱表 */
char schoolwork[SWN][NAMELEN+1] = {"Chinese","Mathematic","English"};
struct record
{
char name[NAMELEN+1]; /* 姓名 */
char code[CODELEN+1]; /* 學號 */
int marks[SWN]; /* 各課程成績 */
int total; /* 總分 */
}stu;
struct node
{
char name[NAMELEN+1]; /* 姓名 */
char code[CODELEN+1]; /* 學號 */
int marks[SWN]; /* 各課程成績 */
int total; /* 總分 */
struct node *next; /* 後續表元指針 */
}*head; /* 鏈表首指針 */
int total[SWN]; /* 各課程總分 */
FILE *stfpt; /* 文件指針 */
char stuf[FNAMELEN]; /* 文件名 */
/* 從指定文件讀入一個記錄 */
int readrecord(FILE *fpt,struct record *rpt)
{
char buf[BUFLEN];
int i;
if(fscanf(fpt,"%s",buf)!=1)
return 0; /* 文件結束 */
strncpy(rpt->name,buf,NAMELEN);
fscanf(fpt,"%s",buf);
strncpy(rpt->code,buf,CODELEN);
for(i=0;i<SWN;i++)
fscanf(fpt,"%d",&rpt->marks[i]);
for(rpt->total=0,i=0;i<SWN;i++)
rpt->total+=rpt->marks[i];
return 1;
}
/* 對指定文件寫入一個記錄 */
writerecord(FILE *fpt,struct record *rpt)
{
int i;
fprintf(fpt,"%s\n",rpt->name);
fprintf(fpt,"%s\n",rpt->code);
for(i=0;i<SWN;i++)
fprintf(fpt,"%d\n",rpt->marks[i]);
return ;
}
/* 顯示學生記錄 */
displaystu(struct record *rpt)
{
int i;
printf("\nName : %s\n",rpt->name);
printf("Code : %s\n",rpt->code);
printf("Marks :\n");
for(i=0;i<SWN;i++)
printf(" %-15s : %4d\n",schoolwork[i],rpt->marks[i]);
printf("Total : %4d\n",rpt->total);
}
/* 計算各單科總分 */
int totalmark(char *fname)
{
FILE *fp;
struct record s;
int count,i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
for(i=0;i<SWN;i++)
total[i]=0;
count=0;
while(readrecord(fp,&s)!=0)
{
for(i=0;i<SWN;i++)
total[i]+=s.marks[i];
count++;
}
fclose(fp);
return count; /* 返回記錄數 */
}
/* 列表顯示學生信息 */
void liststu(char *fname)
{
FILE *fp;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return ;
}
while(readrecord(fp,&s)!=0)
{
displaystu(&s);
printf("\n Press ENTER to continue...\n");
while(getchar()!='\n');
}
fclose(fp);
return;
}
/* 構造鏈表 */
struct node *makelist(char *fname)
{
FILE *fp;
struct record s;
struct node *p,*u,*v,*h;
int i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return NULL;
}
h=NULL;
p=(struct node *)malloc(sizeof(struct node));
while(readrecord(fp,(struct record *)p)!=0)
{
v=h;
while(v&&p->total<=v->total)
{
u=v;
v=v->next;
}
if(v==h)
h=p;
else
u->next=p;
p->next=v;
p=(struct node *)malloc(sizeof(struct node));
}
free(p);
fclose(fp);
return h;
}
/* 順序顯示鏈表各表元 */
void displaylist(struct node *h)
{
while(h!=NULL)
{
displaystu((struct record *)h);
printf("\n Press ENTER to continue...\n");
while(getchar()!='\n');
h=h->next;
}
return;
}
/* 按學生姓名查找學生記錄 */
int retrievebyn(char *fname, char *key)
{
FILE *fp;
int c;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(readrecord(fp,&s)!=0)
{
if(strcmp(s.name,key)==0)
{
displaystu(&s);
c++;
}
}
fclose(fp);
if(c==0)
printf("The student %s is not in the file %s.\n",key,fname);
return 1;
}
/* 按學生學號查找學生記錄 */
int retrievebyc(char *fname, char *key)
{
FILE *fp;
int c;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(readrecord(fp,&s)!=0)
{
if(strcmp(s.code,key)==0)
{
displaystu(&s);
c++;
break;
}
}
fclose(fp);
if(c==0)
printf("The student %s is not in the file %s.\n",key,fname);
return 1;
}
main()
{
int i,j,n;
char c;
char buf[BUFLEN];
FILE *fp;
struct record s;
clrscr();
printf("Please input the students marks record file's name: ");
scanf("%s",stuf);
if((fp=fopen(stuf,"r"))==NULL)
{
printf("The file %s doesn't exit, do you want to creat it? (Y/N) ",stuf);
getchar();
c=getchar();
if(c=='Y'||c=='y')
{
fp=fopen(stuf,"w");
printf("Please input the record number you want to write to the file: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input the student's name: ");
scanf("%s",&s.name);
printf("Input the student's code: ");
scanf("%s",&s.code);
for(j=0;j<SWN;j++)
{
printf("Input the %s mark: ",schoolwork[j]);
scanf("%d",&s.marks[j]);
}
writerecord(fp,&s);
}
fclose(fp);
}
}
fclose(fp);
getchar();
/*clrscr();*/
puts("Now you can input a command to manage the records.");
puts("m : mean of the marks.");
puts("t : total of the marks.");
puts("n : search record by student's name.");
puts("c : search record by student's code.");
puts("l : list all the records.");
puts("s : sort and list the records by the total.");
puts("q : quit!");
while(1)
{
puts("Please input command:");
scanf(" %c",&c); /* 輸入選擇命令 */
if(c=='q'||c=='Q')
{
puts("\n Thank you for your using.");
break; /* q,結束程序運行 */
}
switch(c)
{
case 'm': /* 計算平均分 */
case 'M':
if((n=totalmark(stuf))==0)
{
puts("Error!");
break;
}
printf("\n");
for(i=0;i<SWN;i++)
printf("%-15s's average is: %.2f.\n",schoolwork[i],(float)total[i]/n);
break;
case 't': /* 計算總分 */
case 'T':
if((n=totalmark(stuf))==0)
{
puts("Error!");
break;
}
printf("\n");
for(i=0;i<SWN;i++)
printf("%-15s's total mark is: %d.\n",schoolwork[i],total[i]);
break;
case 'n': /* 按學生的姓名尋找記錄 */
case 'N':
printf("Please input the student's name you want to search: ");
scanf("%s",buf);
retrievebyn(stuf,buf);
break;
case 'c': /* 按學生的學號尋找記錄 */
case 'C':
printf("Please input the student's code you want to search: ");
scanf("%s",buf);
retrievebyc(stuf,buf);
break;
case 'l': /* 列出所有學生記錄 */
case 'L':
liststu(stuf);
break;
case 's': /* 按總分從高到低排列顯示 */
case 'S':
if((head=makelist(stuf))!=NULL)
displaylist(head);
break;
default: break;
}
}
}
E. 雙向鏈表排序c語言程序設計
/************************************************************************/
/*
文件名 doublelnk.h
作用 定義必要的結構體,並對雙向鏈表的操作函數做聲明
*/
/************************************************************************/
#ifndefDList_H
#defineDList_H
typedefintItem;
typedefstructNode*PNode;
typedefPNodePosition;
/*定義節點類型*/
typedefstructNode
{
Itemid; /*編號*/
Itemdata; /*數據域*/
PNodeprevious;/*指向前驅*/
PNodenext; /*指向後繼*/
}Node;
/*定義鏈表類型*/
typedefstruct
{
PNodehead; /*指向頭節點*/
PNodetail; /*指向尾節點*/
intsize;
}DList;
enumenumSortType
{
ID,
DATA
};
/*分配值為i的節點,並返回節點地址*/
PositionMakeNode(Itemid,Itemdata);
/*釋放p所指的節點*/
voidFreeNode(PNodep);
/*構造一個空的雙向鏈表*/
DList*InitList();
/*摧毀一個雙向鏈表*/
voidDestroyList(DList*plist);
/*將一個鏈表置為空表,釋放原鏈表節點空間*/
voidClearList(DList*plist);
/*返回頭節點地址*/
PositionGetHead(DList*plist);
/*返回尾節點地址*/
PositionGetTail(DList*plist);
/*返回鏈表大小*/
intGetSize(DList*plist);
/*返回p的直接後繼位置*/
PositionGetNext(Positionp);
/*返回p的直接前驅位置*/
PositionGetPrevious(Positionp);
/*將pnode所指節點插入第一個節點之前*/
PNodeInsFirst(DList*plist,PNodepnode);
/*將鏈表第一個節點刪除並返回其地址*/
PNodeDelFirst(DList*plist);
/*獲得節點的數據項*/
ItemGetItem(Positionp);
/*設置節點的數據項*/
voidSetItem(Positionp,Itemi);
/*刪除鏈表中的尾節點並返回其地址,改變鏈表的尾指針指向新的尾節點*/
PNodeRemove(DList*plist);
/*在鏈表中p位置之前插入新節點S*/
PNodeInsBefore(DList*plist,Positionp,PNodes);
/*在鏈表中p位置之後插入新節點s*/
PNodeInsAfter(DList*plist,Positionp,PNodes);
/*返回在鏈表中第i個節點的位置*/
PNodeLocatePos(DList*plist,inti);
voidListTraverse(DList*plist,void(*visit)(Node));
/*對雙向鏈表按照執行的排序方式進行排序*/
voidSortDLnk(DList*plist,enumSortTypesortType);
voidSortDLnkbyID(DList*plist);
voidSortDLnkbyData(DList*plist);
voidswapnode(PNodeanode,PNodebnode);
#endif
/************************************************************************/
/*
文件名 doublelnk.cpp
作用 對雙向鏈表的操作函數進行具體實現
*/
/************************************************************************/
#include"doublelnk.h"
#include<malloc.h>
#include<stdlib.h>
/*分配值為i的節點,並返回節點地址*/
PositionMakeNode(Itemid,Itemdata)
{
PNodep=NULL;
p=(PNode)malloc(sizeof(Node));
if(p!=NULL)
{
p->id=id;
p->data=data;
p->previous=NULL;
p->next=NULL;
}
returnp;
}
/*釋放p所指的節點*/
voidFreeNode(PNodep)
{
free(p);
}
/*構造一個空的雙向鏈表*/
DList*InitList()
{
DList*plist=(DList*)malloc(sizeof(DList));
PNodehead=MakeNode(0,0);
if(plist!=NULL)
{
if(head!=NULL)
{
plist->head=head;
plist->tail=head;
plist->size=0;
}
else
returnNULL;
}
returnplist;
}
/*摧毀一個雙向鏈表*/
voidDestroyList(DList*plist)
{
ClearList(plist);
free(GetHead(plist));
free(plist);
}
/*判斷鏈表是否為空表*/
intIsEmpty(DList*plist)
{
if(GetSize(plist)==0&&GetTail(plist)==GetHead(plist))
return1;
else
return0;
}
/*將一個鏈表置為空表,釋放原鏈表節點空間*/
voidClearList(DList*plist)
{
PNodetemp,p;
p=GetTail(plist);
while(!IsEmpty(plist))
{
temp=GetPrevious(p);
FreeNode(p);
p=temp;
plist->tail=temp;
plist->size--;
}
}
/*返回頭節點地址*/
PositionGetHead(DList*plist)
{
returnplist->head;
}
/*返回尾節點地址*/
PositionGetTail(DList*plist)
{
returnplist->tail;
}
/*返回鏈表大小*/
intGetSize(DList*plist)
{
returnplist->size;
}
/*返回p的直接後繼位置*/
PositionGetNext(Positionp)
{
returnp->next;
}
/*返回p的直接前驅位置*/
PositionGetPrevious(Positionp)
{
returnp->previous;
}
/*將pnode所指節點插入第一個節點之前*/
PNodeInsFirst(DList*plist,PNodepnode)
{
Positionhead=GetHead(plist);
if(IsEmpty(plist))
plist->tail=pnode;
plist->size++;
pnode->next=head->next;
pnode->previous=head;
if(head->next!=NULL)
head->next->previous=pnode;
head->next=pnode;
returnpnode;
}
/*將鏈表第一個節點刪除,返回該節點的地址*/
PNodeDelFirst(DList*plist)
{
Positionhead=GetHead(plist);
Positionp=head->next;
if(p!=NULL)
{
if(p==GetTail(plist))
plist->tail=p->previous;
head->next=p->next;
head->next->previous=head;
plist->size--;
}
returnp;
}
/*獲得節點的數據項*/
ItemGetItem(Positionp)
{
returnp->data;
}
/*設置節點的數據項*/
voidSetItem(Positionp,Itemi)
{
p->data=i;
}
/*刪除鏈表中的尾節點並返回地址,改變鏈表的尾指針指向新的尾節點*/
PNodeRemove(DList*plist)
{
Positionp=NULL;
if(IsEmpty(plist))
returnNULL;
else
{
p=GetTail(plist);
p->previous->next=p->next;
plist->tail=p->previous;
plist->size--;
returnp;
}
}
/*在鏈表中p位置之前插入新節點s*/
PNodeInsBefore(DList*plist,Positionp,PNodes)
{
s->previous=p->previous;
s->next=p;
p->previous->next=s;
p->previous=s;
plist->size++;
returns;
}
/*在鏈表中p位置之後插入新節點s*/
PNodeInsAfter(DList*plist,Positionp,PNodes)
{
s->next=p->next;
s->previous=p;
if(p->next!=NULL)
p->next->previous=s;
p->next=s;
if(p=GetTail(plist))
plist->tail=s;
plist->size++;
returns;
}
/*返回在鏈表中第i個節點的位置*/
PNodeLocatePos(DList*plist,inti)
{
intcnt=0;
Positionp=GetHead(plist);
if(i>GetSize(plist)||i<1)
returnNULL;
while(++cnt<=i)
{
p=p->next;
}
returnp;
}
/*依次對鏈表中每個元素調用函數visit()*/
voidListTraverse(DList*plist,void(*visit)(Node))
{
Positionp=GetHead(plist);
if(IsEmpty(plist))
exit(0);
else
{
while(p->next!=NULL)
{
p=p->next;
visit(*p);
}
}
}
voidSortDLnk(DList*plist,enumSortTypesortType)
{
switch(sortType)
{
caseID:
SortDLnkbyID(plist);
break;
caseDATA:
SortDLnkbyData(plist);
break;
}
}
voidSortDLnkbyID(DList*plist)
{
PNodehead=plist->head;
Nodetmpnode;
Positioncurrnode=head;
Positionbianlinode;
while((currnode=currnode->next)!=NULL)
{
bianlinode=currnode;
while((bianlinode=bianlinode->next)!=NULL)
{
if(currnode->id>bianlinode->id)
{
swapnode(currnode,bianlinode);
}
}
}
}
voidSortDLnkbyData(DList*plist)
{
PNodehead=plist->head;
Nodetmpnode;
Positioncurrnode=head;
Positionbianlinode;
while((currnode=currnode->next)!=NULL)
{
bianlinode=currnode;
while((bianlinode=bianlinode->next)!=NULL)
{
if(currnode->data>bianlinode->data)
{
swapnode(currnode,bianlinode);
}
}
}
}
voidswapnode(PNodeanode,PNodebnode)
{//這裡面要特別注意防止前驅節點是頭結點和後驅節點是Null的情況
Nodetmpnode;
tmpnode.id=anode->id;
tmpnode.data=anode->data;
anode->id=bnode->id;
anode->data=bnode->data;
bnode->id=tmpnode.id;
bnode->data=tmpnode.data;
}/************************************************************************/
/*
文件名 program.cpp
作用 對雙向鏈表的操作函數進行測試
*/
/************************************************************************/
#include"doublelnk.h"
#include<stdio.h>
voidprint(Nodenode)
{
printf("數據項:id=%d,data=%d ",node.id,node.data);
}
intmain()
{
DList*plist=NULL;
PNodep=NULL;
plist=InitList();
p=InsFirst(plist,MakeNode(12,23));
InsBefore(plist,p,MakeNode(2,54));
InsAfter(plist,p,MakeNode(3,34));
printf("p前驅位置的值為%d ",GetItem(GetPrevious(p)));
printf("p位置的值為%d ",GetItem(p));
printf("p後繼位置的值為%d ",GetItem(GetNext(p)));
printf("遍歷輸出各節點數據項: ");
ListTraverse(plist,print);
printf("按照ID排序後遍歷輸出: ");
SortDLnk(plist,ID);
ListTraverse(plist,print);
printf("按照Data排序後遍歷輸出: ");
SortDLnk(plist,DATA);
ListTraverse(plist,print);
printf("除了頭節點該鏈表共有%d個節點 ",GetSize(plist));
FreeNode(DelFirst(plist));
printf("刪除第一個節點後重新遍歷輸出為: ");
ListTraverse(plist,print);
printf("除了頭節點該鏈表共有%d個節點 ",GetSize(plist));
DestroyList(plist);
printf("鏈表已被銷毀 ");
return0;
}
程序總共分三部分, 分別是雙向鏈表的頭文件、源文件和測試程序
建立工程後,分別建立相應的文件並添加相應代碼應該就可以
下面的圖片是我的運行效果(聲明:程序中很多代碼參考了以為前輩的代碼http://blog.csdn.net/hopeyouknow/article/details/6716177)
F. 關於電子信息工程(成信的)
專業就業:作為本專業的2010級學長,切身體會,學起來有點難度,特別需要你的耐心和毅力,還要經常動手焊接啊,設計電路之類的。專業分為電子設計和通信工程兩個方向。電子設計主要是集成半集成電路的設計,精簡,維護等,發明創造性要求較高,發展潛力好;通信工程主要是對一些已有設備的維護,維修,檢測,安裝等,發明創造要求較低,實用性強,好上手。
進入實驗室:沒有什麼具體的硬性條件,先是按興趣報名,然後進行一段時間的培訓,在進行選拔,跟你的學習成績沒有什麼很大關聯。因為實驗室主要看中你的實際操作動手能力,對理論要求不高。實驗室設施還算不錯,關鍵是分為較好,一級一級的學長學姐往下帶,有一種傳承。
G. c語言求絕對值最大值
#include<stdio.h>
intmain()
{
intn,i,a,max;
scanf("%d ",&n);
scanf("%d ",&a);
if(a<0)max=-a;//a小於0是max=-a,所以max是正數
elsemax=a;//a不小於0時max=a,所以max是正數
for(i=1;i<n;i=i+1)
{scanf("%d",&a);
if(a<0&&max<-a)max=-a;//a小於0時max=-a,所以max是正數
if(a>=0&&max<a)max=a;}//a不小於0時max=a,所以max是正數
printf("%d ",max);
return0;
}//因為這四個賦值都是把正數賦值給max,所以你這個程序里max沒有負數。
望採納。。。。。。
H. C語言求最大公因數和最小公倍數的N種方法
(A)
#include<stdio.h>
int f(int a,int b,int n)
{if(a%n+b%n) n=f(a,b,n-1);
return n;
}
void main()
{int a,b,t,n,m;
printf("input a,b:\n");
scanf("%d%d",&a,&b);
n=(a>b)?b:a;
m=a*b/f(a,b,n);
printf("zui da gong yue shu:%d\nzui xiao gong bei shu:%d\n",f(a,b,n),m);
}
(B)
把上面的遞歸換成循環
#include<stdio.h>
void main()
{
int x,y,a,m,n,t;
printf("please input two num x,y\n");
scanf("%d%d",&x,&y);
for(a=x>y?x:y;a>0;a--)
if(x%a==0&&y%a==0)
{
printf("x,y最大公約數 %d\n",a);
break;
}
m=x/a;
n=y/a;
t=a*m*n;
printf("x,y最小公倍數 %d\n",t);
}
(C)
輾轉相除法,這才是最好的方法,一定要掌握
http://ke..com/view/255668.html?wtp=tt
有了大公約,兩數乘後除以大公約即為小公倍