本文目錄一覽:
- 1、求C語言小程序源代碼,300行左右
- 2、能不能用C語言編個小遊戲,代碼在三百行左右?
- 3、求一個300行左右的簡單的c語言程序
- 4、急求急急急急急求,急求用C語言編寫一個計算器程序,代碼量為三百行左右!!!!!各位大神們,幫幫忙
- 5、完成一個貪吃蛇C語言程序,代碼量300行左右,可以運行還有答辯理解代碼,求幫忙
- 6、用C語言編的計算器,200~300行
求C語言小程序源代碼,300行左右
黑白棋遊戲
#include “graphics.h” /*圖形系統頭文件*/
#define LEFT 0x4b00 /*光標左鍵值*/
#define RIGHT 0x4d00 /*光標右鍵值*/
#define DOWN 0x5000 /*光標下鍵值*/
#define UP 0x4800 /*光標上鍵值*/
#define ESC 0x011b /* ESC鍵值*/
#define ENTER 0x1c0d /* 回車鍵值*/
int a[8][8]={0},key,score1,score2;/*具體分數以及按鍵與存放棋子的變量*/
char playone[3],playtwo[3];/*兩個人的得分轉換成字符串輸出*/
void playtoplay(void);/*人人對戰函數*/
void DrawQp(void);/*畫棋盤函數*/
void SetPlayColor(int x);/*設置棋子第一次的顏色*/
void MoveColor(int x,int y);/*恢復原來棋盤狀態*/
int QpChange(int x,int y,int z);/*判斷棋盤的變化*/
void DoScore(void);/*處理分數*/
void PrintScore(int n);/*輸出成績*/
void playWin(void);/*輸出勝利者信息*/
/******主函數*********/
void main(void)
{
int gd=DETECT,gr;
initgraph(gd,gr,”c:\\tc”); /*初始化圖形系統*/
DrawQp();/*畫棋盤*/
playtoplay();/*人人對戰*/
getch();
closegraph();/*關閉圖形系統*/
}
void DrawQp()/*畫棋盤*/
{
int i,j;
score1=score2=0;/*棋手一開始得分都為0*/
setbkcolor(BLUE);
for(i=100;i=420;i+=40)
{
line(100,i,420,i);/*畫水平線*/
line(i,100,i,420); /*畫垂直線*/
}
setcolor(0);/*取消圓周圍的一圈東西*/
setfillstyle(SOLID_FILL,15);/*白色實體填充模式*/
fillellipse(500,200,15,15); /*在顯示得分的位置畫棋*/
setfillstyle(SOLID_FILL,8); /*黑色實體填充模式*/
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;/*初始兩個黑棋*/
a[3][4]=a[4][3]=2;/*初始兩個白棋*/
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,15,15);
score1=score2=2; /*有棋後改變分數*/
DoScore();/*輸出開始分數*/
}
void playtoplay()/*人人對戰*/
{
int x,y,t=1,i,j,cc=0;
while(1)/*換棋手走棋*/
{
x=120,y=80;/*每次棋子一開始出來的坐標,x為行坐標,y為列坐標*/
while(1) /*具體一個棋手走棋的過程*/
{
PrintScore(1);/*輸出棋手1的成績*/
PrintScore(2);/*輸出棋手2的成績*/
SetPlayColor(t);/*t變量是用來判斷棋手所執棋子的顏色*/
fillellipse(x,y,15,15);
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*跳出遊戲*/
break;
else
if(key==ENTER)/*如果按鍵確定就可以跳出循環*/
{
if(y!=80a[(x-120)/40][(y-120)/40]!=1
a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置沒有棋子*/
{
if(t%2==1)/*如果是棋手1移動*/
a[(x-120)/40][(y-120)/40]=1;
else/*否則棋手2移動*/
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))/*落子後判斷棋盤的變化*/
{
a[(x-120)/40][(y-120)/40]=0;/*恢復空格狀態*/
cc++;/*開始統計嘗試次數*/
if(cc=64-score1-score2) /*如果嘗試超過空格數則停步*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;/*如果按鍵無效*/
}
DoScore();/*分數的改變*/
break;/*棋盤變化了,則輪對方走棋*/
}
else/*已經有棋子就繼續按鍵*/
continue;
}
else /*四個方向按鍵的判斷*/
if(key==LEFTx120)/*左方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHTx400y80)/*右方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UPy120)/*上方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWNy400)/*下方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)/*結束遊戲*/
break;
if((score1+score2)==64||score1==0||score2==0)/*格子已經佔滿或一方棋子為0判斷勝負*/
{
playWin();/*輸出最後結果*/
break;
}
t=t%2+1; /*一方走後,改變棋子顏色即輪對方走*/
cc=0; /*計數值恢復為0*/
} /*endwhile*/
}
void SetPlayColor(int t)/*設置棋子顏色*/
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);/*白色*/
else
setfillstyle(SOLID_FILL,8);/*灰色*/
}
void MoveColor(int x,int y)/*走了一步後恢復原來格子的狀態*/
{
if(y100)/*如果是從起點出發就恢復藍色*/
setfillstyle(SOLID_FILL,BLUE);
else/*其他情況如果是1就恢復白色棋子,2恢復黑色棋子,或恢復藍色棋盤*/
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break; /*白色*/
case 2:
setfillstyle(SOLID_FILL,8);break; /*黑色*/
default:
setfillstyle(SOLID_FILL,BLUE); /*藍色*/
}
}
int QpChange(int x,int y,int t)/*判斷棋盤的變化*/
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40; /*計算數組元素的行下標*/
j=(y-120)/40; /*計算數組元素的列下標*/
SetPlayColor(t);/*設置棋子變化的顏色*/
/*開始往8個方向判斷變化*/
if(j6)/*往右邊*/
{
for(k=j+1;k8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格結束*/
break;
if(a[i][k]!=0k8)
{
for(kk=j+1;kkkk8;kk++)/*判斷右邊*/
{
a[i][kk]=a[i][j]; /*改變棋子顏色*/
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1) /*條件成立則有棋子改變過顏色*/
yes=1;
}
}
if(j1)/*判斷左邊*/
{
for(k=j-1;k=0;k–)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0k=0)
{
for(kk=j-1;kkkk=0;kk–)
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i6)/*判斷下邊*/
{
for(k=i+1;k8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0k8)
{
for(kk=i+1;kkkk8;kk++)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i1)/*判斷上邊*/
{
for(k=i-1;k=0;k–)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0k=0)
{
for(kk=i-1;kkkk=0;kk–)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i1j6)/*右上*/
{
for(k=i-1,kk=j+1;k=0kk8;k–,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]k=0kk8)
{
for(ii=i-1,jj=j+1;iikk=0;ii–,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i6j1)/*左下*/
{
for(k=i+1,kk=j-1;k8kk=0;k++,kk–)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0k8kk=0)
{
for(ii=i+1,jj=j-1;iikk8;ii++,jj–)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i1j1)/*左上*/
{
for(k=i-1,kk=j-1;k=0kk=0;k–,kk–)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0k=0kk=0)
{
for(ii=i-1,jj=j-1;iikk=0;ii–,jj–)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i6j6)/* 右下*/
{
for(k=i+1,kk=j+1;kk8kk8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0kk8k8)
{
for(ii=i+1,jj=j+1;iikk8;ii++,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;/*返回是否改變過棋子顏色的標記*/
}
void DoScore()/*處理分數*/
{
int i,j;
score1=score2=0;/*重新開始計分數*/
for(i=0;i8;i++)
for(j=0;j8;j++)
if(a[i][j]==1)/*分別統計兩個人的分數*/
score1++;
else
if(a[i][j]==2)
score2++;
}
void PrintScore(int playnum)/*輸出成績*/
{
if(playnum==1)/*清除以前的成績*/
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);/*設置文本輸出樣式*/
if(playnum==1)/*判斷輸出哪個棋手的分,在不同的位置輸出*/
{
sprintf(playone,”%d”,score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,”%d”,score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}
void playWin()/*輸出最後的勝利者結果*/
{
settextstyle(0,0,4);
setcolor(12);
if(score2score1)/*開始判斷最後的結果*/
outtextxy(100,50,”black win!”);
else
if(score2score1)
outtextxy(100,50,”white win!”);
else
outtextxy(60,50,”you all win!”);
}
五子棋遊戲
/*五子棋*/
#includestdio.h
#includestdlib.h
#includegraphics.h
#includebios.h
#includeconio.h
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920
#define BILI 20
#define JZ 4
#define JS 3
#define N 19
int box[N][N];
int step_x,step_y ;
int key ;
int flag=1 ;
void draw_box();
void draw_cicle(int x,int y,int color);
void change();
void judgewho(int x,int y);
void judgekey();
int judgeresult(int x,int y);
void attentoin();
void attention()
{
char ch ;
window(1,1,80,25);
textbackground(LIGHTBLUE);
textcolor(YELLOW);
clrscr();
gotoxy(15,2);
printf(“遊戲操作規則:”);
gotoxy(15,4);
printf(“Play Rules:”);
gotoxy(15,6);
printf(“1、按左右上下方向鍵移動棋子”);
gotoxy(15,8);
printf(“1. Press Left,Right,Up,Down Key to move Piece”);
gotoxy(15,10);
printf(“2、按空格確定落棋子”);
gotoxy(15,12);
printf(“2. Press Space to place the Piece”);
gotoxy(15,14);
printf(“3、禁止在棋盤外按空格”);
gotoxy(15,16);
printf(“3. DO NOT press Space outside of the chessboard”);
gotoxy(15,18);
printf(“你是否接受上述的遊戲規則(Y/N)”);
gotoxy(15,20);
printf(“Do you accept the above Playing Rules? [Y/N]:”);
while(1)
{
gotoxy(60,20);
ch=getche();
if(ch==’Y’||ch==’y’)
break ;
else if(ch==’N’||ch==’n’)
{
window(1,1,80,25);
textbackground(BLACK);
textcolor(LIGHTGRAY);
clrscr();
exit(0);
}
gotoxy(51,12);
printf(” “);
}
}
void draw_box()
{
int x1,x2,y1,y2 ;
setbkcolor(LIGHTBLUE);
setcolor(YELLOW);
gotoxy(7,2);
printf(“Left, Right, Up, Down KEY to move, Space to put, ESC-quit.”);
for(x1=1,y1=1,y2=18;x1=18;x1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x1+JZ)*BILI,(y2+JS)*BILI);
for(x1=1,y1=1,x2=18;y1=18;y1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x2+JZ)*BILI,(y1+JS)*BILI);
for(x1=1;x1=18;x1++)
for(y1=1;y1=18;y1++)
box[x1][y1]=0 ;
}
void draw_circle(int x,int y,int color)
{
setcolor(color);
setlinestyle(SOLID_LINE,0,1);
x=(x+JZ)*BILI ;
y=(y+JS)*BILI ;
circle(x,y,8);
}
void judgekey()
{
int i ;
int j ;
switch(key)
{
case LEFT :
if(step_x-10)
break ;
else
{
for(i=step_x-1,j=step_y;i=1;i–)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i1)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case RIGHT :
if(step_x+118)
break ;
else
{
for(i=step_x+1,j=step_y;i=18;i++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i18)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case DOWN :
if((step_y+1)18)
break ;
else
{
for(i=step_x,j=step_y+1;j=18;j++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j18)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case UP :
if((step_y-1)0)
break ;
else
{
for(i=step_x,j=step_y-1;j=1;j–)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j1)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case ESC :
break ;
case SPACE :
if(step_x=1step_x=18step_y=1step_y=18)
{
if(box[step_x][step_y]==0)
{
box[step_x][step_y]=flag ;
if(judgeresult(step_x,step_y)==1)
{
sound(1000);
delay(1000);
nosound();
gotoxy(30,4);
if(flag==1)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定義一個圖形窗口*/
setfillstyle(1,2);
/*綠色以實填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,5);
/*三重筆劃字體, 水平放?5倍*/
outtextxy(20,20,”The White Win !”);
setcolor(15);
settextstyle(3,0,5);
/*無襯筆劃字體, 水平放大5倍*/
outtextxy(120,120,”The White Win !”);
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
if(flag==2)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定義一個圖形窗口*/
setfillstyle(1,2);
/*綠色以實填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,8);
/*三重筆劃字體, 水平放大8倍*/
outtextxy(20,20,”The Red Win !”);
setcolor(15);
settextstyle(3,0,5);
/*無襯筆劃字體, 水平放大5倍*/
outtextxy(120,120,”The Red Win !”);
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
}
change();
break ;
}
}
else
break ;
}
}
void change()
{
if(flag==1)
flag=2 ;
else
flag=1 ;
}
void judgewho(int x,int y)
{
if(flag==1)
draw_circle(x,y,15);
if(flag==2)
draw_circle(x,y,4);
}
int judgeresult(int x,int y)
{
int j,k,n1,n2 ;
while(1)
{
n1=0 ;
n2=0 ;
/*水平向左數*/
for(j=x,k=y;j=1;j–)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*水平向右數*/
for(j=x,k=y;j=18;j++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1=5)
{
return(1);
break ;
}
/*垂直向上數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;k=1;k–)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*垂直向下數*/
for(j=x,k=y;k=18;k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1=5)
{
return(1);
break ;
}
/*向左上方數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j=1,k=1;j–,k–)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向右下方數*/
for(j=x,k=y;j=18,k=18;j++,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1=5)
{
return(1);
break ;
}
/*向右上方數*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j=18,k=1;j++,k–)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向左下方數*/
for(j=x,k=y;j=1,k=18;j–,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1=5)
{
return(1);
break ;
}
return(0);
break ;
}
}
void main()
{
int gdriver=VGA,gmode=VGAHI;
clrscr();
attention();
initgraph(gdriver,gmode,”c:\\tc”);
/* setwritemode(XOR_PUT);*/
flag=1 ;
draw_box();
do
{
step_x=0 ;
step_y=0 ;
/*draw_circle(step_x,step_y,8); */
judgewho(step_x-1,step_y-1);
do
{
while(bioskey(1)==0);
key=bioskey(0);
judgekey();
}
while(key!=SPACEkey!=ESC);
}
while(key!=ESC);
closegraph();
}
能不能用C語言編個小遊戲,代碼在三百行左右?
打字遊戲
#includestdio.h
#includetime.h
char *kw[]={“Q W E R T Y U I O P [ ]”,”A S D F G H J K L ; ‘”,”Z X C V B N M , . / “};
long AllCounter=0,RightCounter=0,WrongCounter=0;
main()
{
int i,j;
int fun_Esc();
clrscr();
gotoxy(18,1);
printf(“%s\n”,kw[0]);
gotoxy(20,3);
printf(“%s\n”,kw[1]);
gotoxy(22,5);
printf(“%s\n”,kw[2]);
gotoxy(11,25);
for(i=0;i60;i++)
{
printf(“=”);
}
gotoxy(1,1);
printf(“AllCh: %ld\nRight: %ld\nWrong: %ld”,AllCounter,RightCounter,WrongCounter);
gotoxy(50,1);
printf(“Press Esc to exit”);
gotoxy(50,2);
printf(“Enter to pause”);
gotoxy(26,12);
printf(“* * * * * * * * * * * * * * “);
gotoxy(26,13);
printf(“* Press any key to start! *”);
gotoxy(26,14);
printf(“* * * * * * * * * * * * * * “);
gotoxy(51,13);
if(getch()==27)
{
if(fun_Esc()==1)
{
clrscr();
exit(0);
}
}
gotoxy(23,12);
printf(” “);
gotoxy(23,13);
printf(” “);
gotoxy(23,14);
printf(” “);
while(1)
fun_Play();
}
int fun_Play()
{
int x,y,i,j;
unsigned int Timer;
char ch;
char cur;
time_t t;
srand((unsigned)time(t));
gotoxy(26,12);
printf(” “);
gotoxy(26,13);
printf(” “);
gotoxy(26,14);
printf(” “);
y = 6;
Timer = 100000;
i = rand()%3;
j = rand()%(9-i);
ch = kw[i][j*4];
x = 18+i*2+j*4;
while(y=24)
{
if(kbhit())
{
cur = getch();
if(cur==ch || cur==ch+32)
{
ch = ‘*’; Timer = 1000;
}
else if(cur==27)
{
if(fun_Esc()==1)
{
clrscr();
exit(0);
}
}
else if(cur==’\r’)
{
gotoxy(x,y-1);
printf(” “);
gotoxy(26,12);
printf(“* * * * * * * * * * * * * * *”);
gotoxy(26,13);
printf(“* Press any key to continue *”);
gotoxy(26,14);
printf(“* * * * * * * * * * * * * * * “);
getch();
gotoxy(28,13);
printf(” “);
}
else
{
WrongCounter++;
}
}
if(y6)
{
gotoxy(x,y-1);
printf(” “);
}
gotoxy(x,y);
printf(“%c”,ch);
gotoxy(1,1);
printf(“AllCh: %ld\nRight: %ld\nWrong: %ld”,AllCounter,RightCounter,WrongCounter);
delay(Timer);
y++;
}
AllCounter++;
if(ch == ‘*’)
{
RightCounter++;
}
}
int fun_Esc()
{
int key = ‘#’;
gotoxy(26,12);
printf(“* * * * * * * * * * * * * * * * “);
gotoxy(26,13);
printf(“* Are you sure to exit? (Y/N) *”);
gotoxy(26,14);
printf(“* * * * * * * * * * * * * * * * “);
gotoxy(51,13);
while(key!=’Y’ key!=’y’ key!=’N’ key!=’n’)
{
key = getch();
if(key==’Y’ || key==’y’)
{
return 1;
}
if(key==’N’ || key==’n’)
{
gotoxy(24,12);
printf(” “);
gotoxy(24,13);
printf(” “);
gotoxy(24,14);
printf(” “);
return 0;
}
}
}
求一個300行左右的簡單的c語言程序
300行還簡單啊,呵呵,你自己去C語言代碼庫找找啊,應該有你想要的#include stdio.h
#include stdlib.h
#include string.hstruct STU
{
long Num;
int mathScore;
int englishScore;
int computerScore;
int allScore;
int averageScore;
};void sort();
void cal();
void stuPrint();
void stuInput();
#define STUNUM 60/*定義學生數*/STU stu[STUNUM];int main(int argc, char* argv[])
{
stuInput();
cal();
sort();
stuPrint(); return 0;
}void stuInput()
{
int i = 0;
for (;iSTUNUM;i++)
{
system(“cls”);
printf(“請一個學生輸入學號\n”);
scanf(“%d”,(stu[i].Num));
printf(“請輸入該學生數學成績\n”);
scanf(“%d”,(stu[i].mathScore));
printf(“請輸入該學生英語成績\n”);
scanf(“%d”,(stu[i].englishScore));
printf(“請輸入該學生計算機成績\n”);
scanf(“%d”,(stu[i].computerScore));
}
}void cal()
{
int i = 0;
for (;iSTUNUM;i++)
{
stu[i].allScore = stu[i].mathScore + stu[i].computerScore + stu[i].englishScore;
stu[i].averageScore = stu[i].allScore / 3;
}
}void sort()
{
STU temp;
int i = 0 , j =0;
for (;iSTUNUM-1;i++)
{
for ( j = i+1;j STUNUM ;j++)
{
if (stu[i].allScorestu[j].allScore)
{
memcpy(temp,stu[i],sizeof(STU));
memcpy(stu[i],stu[j],sizeof(STU));
memcpy(stu[j],temp,sizeof(STU));
}
}
}
}void stuPrint()
{
int i = 0;
printf(“名次 學號 數學成績 英語成績 計算機成績 總成績 平均成績\n”);
for (;iSTUNUM;i++)
{
printf(“\n———————————————————\n”);
printf(“%d\t%d\t%d\t%d\t%d\t%d\t%d\n”,i+1,stu[i].Num,stu[i].mathScore,
stu[i].englishScore,stu[i].computerScore,
stu[i].allScore,stu[i].averageScore);
}}
急求急急急急急求,急求用C語言編寫一個計算器程序,代碼量為三百行左右!!!!!各位大神們,幫幫忙
#includestdio.h
#includestdlib.h
#includeiostream.h
float add(float x,float y)
{
x=x+y;return x;
}
float sub(float x,float y)
{
x=x-y;return x;
}
float mul(float x,float y)
{
x=x*y;return x;
}
float div(float x,float y)
{x=x/y;return x;}
void print()
{ printf(” /********歡迎使用精益計算機 ********/\n”);
printf(” /******輸入模式a+-*/b*****/\n”);
printf(” /******輸入字符c清屏*****/\n”);
printf(“請輸入數據:”);
}
void main()
{char i;
float a,b,c,d;
print();
loop: cina; //scanf(“%f”,a);
while(1)
{cini;if(i!=’c’)cinb;//scanf(“%c%f”,i,b);
{switch(i)
{ case ‘+’:{c=add(a,b);a=c;printf(“=%0.1f”,c);}break;
case ‘-‘:{c=sub(a,b);a=c;printf(“=%0.1f”,c);}break;
case ‘*’:c=mul(a,b);a=c;printf(“=%0.1f”,c);break;
case ‘/’:c=div(a,b);a=c;printf(“=%0.1f”,c);break;
case ‘c’ : system(“cls”);print();goto loop;break;
}
}
}
}
完成一個貪吃蛇C語言程序,代碼量300行左右,可以運行還有答辯理解代碼,求幫忙
//******友情提示:如想速度快點,請改小_sleep(500)函數中參數*****
#include
#include
#include
#include
#include
const int H = 8; //地圖的高
const int L = 16; //地圖的長
char GameMap[H][L]; //遊戲地圖
int key; //按鍵保存
int sum = 1, over = 0; //蛇的長度, 遊戲結束(自吃或碰牆)
int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向
int dy[4] = {-1, 1, 0, 0};
struct Snake //蛇的每個節點的數據類型
{
int x, y; //左邊位置
int now; //保存當前節點的方向, 0,1,2,3分別為左右上下
}Snake[H*L];
const char Shead = ‘@’; //蛇頭
const char Sbody = ‘#’; //蛇身
const char Sfood = ‘*’; //食物
const char Snode = ‘.’; //’.’在地圖上標示為空
void Initial(); //地圖的初始化
void Create_Food(); //在地圖上隨機產生食物
void Show(); //刷新顯示地圖
void Button(); //取出按鍵,並判斷方向
void Move(); //蛇的移動
void Check_Border(); //檢查蛇頭是否越界
void Check_Head(int x, int y); //檢查蛇頭移動後的位置情況
int main()
{
Initial();
Show();
return 0;
}
void Initial() //地圖的初始化
{
int i, j;
int hx, hy;
system(“title 貪吃蛇”); //控制台的標題
memset(GameMap, ‘.’, sizeof(GameMap)); //初始化地圖全部為空’.’
system(“cls”);
srand(time(0)); //隨機種子
hx = rand()%H; //產生蛇頭
hy = rand()%L;
GameMap[hx][hy] = Shead;
Snake[0].x = hx; Snake[0].y = hy;
Snake[0].now = -1;
Create_Food(); //隨機產生食物
for(i = 0; i H; i++) //地圖顯示
{
for(j = 0; j L; j++)
printf(“%c”, GameMap[i][j]);
printf(“\n”);
}
printf(“\n小小C語言貪吃蛇\n”);
printf(“按任意方向鍵開始遊戲\n”);
getch(); //先接受一個按鍵,使蛇開始往該方向走
Button(); //取出按鍵,並判斷方向
}
void Create_Food() //在地圖上隨機產生食物
{
int fx, fy;
while(1)
{
fx = rand()%H;
fy = rand()%L;
if(GameMap[fx][fy] == ‘.’) //不能出現在蛇所佔有的位置
{
GameMap[fx][fy] = Sfood;
break;
}
}
}
void Show() //刷新顯示地圖
{
int i, j;
while(1)
{
_sleep(500); //延遲半秒(1000為1s),即每半秒刷新一次地圖
Button(); //先判斷按鍵在移動
Move();
if(over) //自吃或碰牆即遊戲結束
{
printf(“\n**遊戲結束**\n”);
printf(” _\n”);
getchar();
break;
}
system(“cls”); //清空地圖再顯示刷新吼的地圖
for(i = 0; i H; i++)
{
for(j = 0; j L; j++)
printf(“%c”, GameMap[i][j]);
printf(“\n”);
}
printf(“\n小小C語言貪吃蛇\n”);
printf(“按任意方向鍵開始遊戲\n”);
}
}
void Button() //取出按鍵,並判斷方向
{
if(kbhit() != 0) //檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0
{
while(kbhit() != 0) //可能存在多個按鍵,要全部取完,以最後一個為主
key = getch(); //將按鍵從控制台中取出並保存到key中
switch(key)
{ //左
case 75: Snake[0].now = 0;
break;
//右
case 77: Snake[0].now = 1;
break;
//上
case 72: Snake[0].now = 2;
break;
//下
case 80: Snake[0].now = 3;
break;
}
}
}
void Move() //蛇的移動
{
int i, x, y;
int t = sum; //保存當前蛇的長度
//記錄當前蛇頭的位置,並設置為空,蛇頭先移動
x = Snake[0].x; y = Snake[0].y; GameMap[x][y] = ‘.’;
Snake[0].x = Snake[0].x + dx[ Snake[0].now ];
Snake[0].y = Snake[0].y + dy[ Snake[0].now ];
Check_Border(); //蛇頭是否越界
Check_Head(x, y); //蛇頭移動後的位置情況,參數為: 蛇頭的開始位置
if(sum == t) //未吃到食物即蛇身移動哦
for(i = 1; i sum; i++) //要從蛇尾節點向前移動哦,前一個節點作為參照
{
if(i == 1) //尾節點設置為空再移動
GameMap[ Snake[i].x ][ Snake[i].y ] = ‘.’;
if(i == sum-1) //為蛇頭後面的蛇身節點,特殊處理
{
Snake[i].x = x;
Snake[i].y = y;
Snake[i].now = Snake[0].now;
}
else //其他蛇身即走到前一個蛇身位置
{
Snake[i].x = Snake[i+1].x;
Snake[i].y = Snake[i+1].y;
Snake[i].now = Snake[i+1].now;
}
GameMap[ Snake[i].x ][ Snake[i].y ] = ‘#’; //移動後要置為’#’蛇身
}
}
void Check_Border() //檢查蛇頭是否越界
{
if(Snake[0].x 0 || Snake[0].x = H
|| Snake[0].y 0 || Snake[0].y = L)
over = 1;
}
void Check_Head(int x, int y) //檢查蛇頭移動後的位置情況
{
if(GameMap[ Snake[0].x ][ Snake[0].y ] == ‘.’) //為空
GameMap[ Snake[0].x ][ Snake[0].y ] = ‘@’;
else
if(GameMap[ Snake[0].x ][ Snake[0].y ] == ‘*’) //為食物
{
GameMap[ Snake[0].x ][ Snake[0].y ] = ‘@’;
Snake[sum].x = x; //新增加的蛇身為蛇頭後面的那個
Snake[sum].y = y;
Snake[sum].now = Snake[0].now;
GameMap[ Snake[sum].x ][ Snake[sum].y ] = ‘#’;
sum++;
Create_Food(); //食物吃完了馬上再產生一個食物
}
else
over = 1;
}
用C語言編的計算器,200~300行
#include dos.h /*DOS接口函數*/
#include math.h /*數學函數的定義*/
#include conio.h /*屏幕操作函數*/
#include stdio.h /*I/O函數*/
#include stdlib.h /*庫函數*/
#include stdarg.h /*變量長度參數表*/
#include graphics.h /*圖形函數*/
#include string.h /*字符串函數*/
#include ctype.h /*字符操作函數*/
#define UP 0x48 /*光標上移鍵*/
#define DOWN 0x50 /*光標下移鍵*/
#define LEFT 0x4b /*光標左移鍵*/
#define RIGHT 0x4d /*光標右移鍵*/
#define ENTER 0x0d /*回車鍵*/
void *rar; /*全局變量,保存光標圖象*/
struct palettetype palette; /*使用調色板信息*/
int GraphDriver; /* 圖形設備驅動*/
int GraphMode; /* 圖形模式值*/
int ErrorCode; /* 錯誤代碼*/
int MaxColors; /* 可用顏色的最大數值*/
int MaxX, MaxY; /* 屏幕的最大分辨率*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*畫邊框函數*/
void initialize(void); /*初始化函數*/
void computer(void); /*計算器計算函數*/
void changetextstyle(int font, int direction, int charsize); /*改變文本樣式函數*/
void mwindow(char *header); /*窗口函數*/
int specialkey(void) ; /*獲取特殊鍵函數*/
int arrow(); /*設置箭頭光標函數*/
/*主函數*/
int main()
{
initialize();/* 設置系統進入圖形模式 */
computer(); /*運行計算器 */
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0); /*結束程序*/
}
/* 設置系統進入圖形模式 */
void initialize(void)
{
int xasp, yasp; /* 用於讀x和y方向縱橫比*/
GraphDriver = DETECT; /* 自動檢測顯示器*/
initgraph( GraphDriver, GraphMode, “” );
/*初始化圖形系統*/
ErrorCode = graphresult(); /*讀初始化結果*/
if( ErrorCode != grOk ) /*如果初始化時出現錯誤*/
{
printf(“Graphics System Error: %s\n”,
grapherrormsg( ErrorCode ) ); /*顯示錯誤代碼*/
exit( 1 ); /*退出*/
}
getpalette( palette ); /* 讀面板信息*/
MaxColors = getmaxcolor() + 1; /* 讀取顏色的最大值*/
MaxX = getmaxx(); /* 讀屏幕尺寸 */
MaxY = getmaxy(); /* 讀屏幕尺寸 */
getaspectratio( xasp, yasp ); /* 拷貝縱橫比到變量中*/
AspectRatio = (double)xasp/(double)yasp;/* 計算縱橫比值*/
}
/*計算器函數*/
void computer(void)
{
struct viewporttype vp; /*定義視口類型變量*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作數和計算結果變量*/
char cnum[5],str2[20]={“”},c,temp[20]={“”};
char str1[]=”1230.456+-789*/Qc=^%”;/* 定義字符串在按鈕圖形上顯示的符號 */
mwindow( “Calculator” ); /* 顯示主窗口 */
color = 7; /*設置灰顏色值*/
getviewsettings( vp ); /* 讀取當前窗口的大小*/
width=(vp.right+1)/10; /* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; /*設置按鈕高度 */
x = width /2; /*設置x的坐標值*/
y = height/2; /*設置y的坐標值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*畫一個二維矩形條顯示運算數和結果*/
setcolor( color+3 ); /*設置淡綠顏色邊框線*/
rectangle( x+width*2, y, x+7*width, y+height );
/*畫一個矩形邊框線*/
setcolor(RED); /*設置顏色為紅色*/
outtextxy(x+3*width,y+height/2,”0.”); /*輸出字符串”0.”*/
x =2*width-width/2; /*設置x的坐標值*/
y =2*height+height/2; /*設置y的坐標值*/
for( j=0 ; j4 ; ++j ) /*畫按鈕*/
{
for( i=0 ; i5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*畫一個矩形條*/
rectangle( x, y, x+width, y+height );
sprintf(str2,”%c”,str1[j*5+i]);
/*將字符保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*移動列坐標*/
}
y +=(height/2)*3; /* 移動行坐標*/
x =2*width-width/2; /*複位列坐標*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*移動光標到x,y位置*/
arrow(); /*顯示光標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,””); /*設置str2為空串*/
while((v=specialkey())!=45) /*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER) /*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); /*顯示光標圖象*/
if(v==RIGHT) /*右移箭頭時新位置計算*/
if(x=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字符位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否則,右移到下一個字符位置*/
if(v==LEFT) /*左移箭頭時新位置計算*/
if(x=x0)
{
x=x0+6*width;
m=4;
} /*如果移到頭,再左移,則移動到最右邊字符位置*/
else
{
x=x-width-width/2;
m–;
} /*否則,左移到前一個字符位置*/
if(v==UP) /*上移箭頭時新位置計算*/
if(y=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到頭,再上移,則移動到最下邊字符位置*/
else
{
y=y-height-height/2;
n–;
} /*否則,移到上邊一個字符位置*/
if(v==DOWN) /*下移箭頭時新位置計算*/
if(y=7*height)
{
y=y0;
n=0;
} /*如果移到尾,再下移,則移動到最上邊字符位置*/
else
{
y=y+height+height/2;
n++;
} /*否則,移到下邊一個字符位置*/
putimage(x,y,rar,XOR_PUT); /*在新的位置顯示光標箭頭*/
}
c=str1[n*5+m]; /*將字符保存到變量c中*/
if(isdigit(c)||c==’.’) /*判斷是否是數字或小數點*/
{
if(flag==-1) /*如果標誌為-1,表明為負數*/
{
strcpy(str2,”-“); /*將負號連接到字符串中*/
flag=1;
} /*將標誌值恢復為1*/
sprintf(temp,”%c”,c); /*將字符保存到字符串變量temp中*/
strcat(str2,temp); /*將temp中的字符串連接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2); /*顯示字符串*/
}
if(c==’+’)
{
num1=atof(str2); /*將第一個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=1; /*做計算加法標誌值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,”0.”); /*顯示字符串*/
}
if(c==’-‘)
{
if(strcmp(str2,””)==0) /*如果str2為空,說明是負號,而不是減號*/
flag=-1; /*設置負數標誌*/
else
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=2; /*做計算減法標誌值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,”0.”); /*顯示字符串*/
}
}
if(c==’*’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=3; /*做計算乘法標誌值*/
setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,”0.”); /*顯示字符串*/
}
if(c==’/’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=4; /*做計算除法標誌值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,”0.”); /*顯示字符串*/
}
if(c==’^’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=5; /*做計算乘方標誌值*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,”0.”); /*顯示字符串*/
}
if(c==’%’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=6; /*做計算模運算乘方標誌值*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,”0.”); /*顯示字符串*/
}
if(c==’=’)
{
num2=atof(str2); /*將第二個操作數轉換為浮點數*/
switch(act) /*根據運算符號計算*/
{
case 1:result=num1+num2;break; /*做加法*/
case 2:result=num1-num2;break; /*做減法*/
case 3:result=num1*num2;break; /*做乘法*/
case 4:result=num1/num2;break; /*做除法*/
case 5:result=pow(num1,num2);break; /*做x的y次方*/
case 6:result=fmod(num1,num2);break; /*做模運算*/
}
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆蓋結果區*/
sprintf(temp,”%f”,result); /*將結果保存到temp中*/
outtextxy(5*width,height,temp); /*顯示結果*/
}
if(c==’c’)
{
num1=0; /*將兩個操作數複位0,符號標誌為1*/
num2=0;
flag=1;
strcpy(str2,””); /*將str2清空*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆蓋結果區*/
outtextxy(5*width,height,”0.”); /*顯示字符串*/
}
if(c==’Q’)exit(0); /*如果選擇了q回車,結束計算程序*/
}
putimage(x,y,rar,XOR_PUT); /*在退出之前消去光標箭頭*/
return; /*返回*/
}
/*窗口函數*/
void mwindow( char *header )
{
int height;
cleardevice(); /* 清除圖形屏幕 */
setcolor( MaxColors – 1 ); /* 設置當前顏色為白色*/
setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 設置視口大小 */
height = textheight( “H” ); /* 讀取基本文本大小 */
settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*設置文本樣式*/
settextjustify( CENTER_TEXT, TOP_TEXT );/*設置字符排列方式*/
outtextxy( MaxX/4, 2, header ); /*輸出標題*/
setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*設置視口大小*/
drawboder(); /*畫邊框*/
}
void drawboder(void) /*畫邊框*/
{
struct viewporttype vp; /*定義視口類型變量*/
setcolor( MaxColors – 1 ); /*設置當前顏色為白色 */
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*設置畫線方式*/
getviewsettings( vp );/*將當前視口信息裝入vp所指的結構中*/
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*畫矩形邊框*/
}
/*設計鼠標圖形函數*/
int arrow()
{
int size;
int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定義多邊形坐標*/
setfillstyle(SOLID_FILL,2); /*設置填充模式*/
fillpoly(8,raw); /*畫出一光標箭頭*/
size=imagesize(4,4,16,16); /*測試圖象大小*/
rar=malloc(size); /*分配內存區域*/
getimage(4,4,16,16,rar); /*存放光標箭頭圖象*/
putimage(4,4,rar,XOR_PUT); /*消去光標箭頭圖象*/
return 0;
}
/*按鍵函數*/
int specialkey(void)
{
int key;
while(bioskey(1)==0); /*等待鍵盤輸入*/
key=bioskey(0); /*鍵盤輸入*/
key=key0xff? key0xff:key8; /*只取特殊鍵的掃描值,其餘為0*/
return(key); /*返回鍵值*/
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246338.html