Ⅰ c語言100行簡單一點的代碼
登錄幼兒園200個小朋友的數據:姓名、性別、年齡、身高、體重、出生日期,分別按年齡排序後輸出。
#include<stdio.h>
#define N 200
struct child
{
char name[10];
char sex[3];
int age;
int height;
float weight;
struct {
int year;
int month;
int day;
}bdate;
}ch[N];
void input()
{
int i;
for(i=0;i<N;i++)
{
printf("\n請輸入第%d名小朋友信息:\n",i+1);
printf("姓名:");
scanf("%s",ch[i].name);
printf("性別:");
scanf("%s",ch[i].sex);
printf("年齡:");
scanf("%d",&ch[i].age);
printf("身高:");
scanf("%d",&ch[i].height);
printf("體重:");
scanf("%f",&ch[i].weight);
printf("出生日期[YYYY-MM-DD]:");
scanf("%d-%d-%d",&ch[i].bdate.year,&ch[i].bdate.month,&ch[i].bdate.day);
}
}
void sort()
{
struct child ct;
int i,j;
for(i=0;i<N-1;i++)
for(j=0;j<N-i-1;j++)
if(ch[j].height<ch[j+1].height)
{
ct=ch[j];
ch[j]=ch[j+1];
ch[j+1]=ct;
}
}
void output()
{
int i;
printf("\n\t幼兒園小朋友一覽(依身高排序)\n");
printf("===================================================\n");
printf(" 姓名 性別 年齡 身高 體重 出生日期 \n");
printf("===================================================\n");
for(i=0;i<N;i++)
printf(" %-8s %-2s %2d %d %3.1f %d.%d.%d\n",ch[i].name,ch[i].sex,ch[i].age,ch[i].height,ch[i].weight,ch[i].bdate.year,ch[i].bdate.month,ch[i].bdate.day);
}
void main()
{
input();
sort();
output();
}
Ⅱ 求幾C語言個小游戲代碼,簡單的,要注釋、、謝謝了、
// Calcu24.cpp : Defines the entry point for the console application.
//
/*
6-6
24點游戲
*/
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "string.h"/*
從一副撲克牌中,任取4張。
2-10 按其點數計算(為了表示方便10用T表示),J,Q,K,A 統一按 1 計算
要求通過加減乘除四則運算得到數字 24。
本程序可以隨機抽取紙牌,並用試探法求解。
*/void GivePuzzle(char* buf)
{
char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}; for(int i=0; i<4; i++){
buf[i] = card[rand() % 13];
}
}
void shuffle(char * buf)
{
for(int i=0; i<5; i++){
int k = rand() % 4;
char t = buf[k];
buf[k] = buf[0];
buf[0] = t;
}
}
int GetCardValue(int c)
{
if(c=='T') return 10;
if(c>='0' && c<='9') return c - '0';
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return '+';
case 1:
return '-';
case 2:
return '*';
case 3:
return '/';
} return ' ';
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2;
case 1:
return op1 - op2;
case 2:
return op1 * op2;
case 3:
if(fabs(op2)>0.0001)
return op1 / op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i<4; i++)
{
if( question[i] == 'T' )
strcpy(p[i], "10");
else
sprintf(p[i], "%c", question[i]);
}
switch(type)
{
case 0:
sprintf(answer, "%s %c (%s %c (%s %c %s))",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer, "%s %c ((%s %c %s) %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer, "(%s %c %s) %c (%s %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer, "((%s %c %s) %c %s) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer, "(%s %c (%s %c %s)) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
// 等待考生完成
int type[5]={0,1,2,3,4};//計算類型
double p[4];
double sum=0;
//
for(int i=0; i<4; i++) //循環取得點數
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i<5;i++)
{
MakeAnswer(answer,type[i],question,oper); //獲取可能的答案
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
採用隨機試探法:就是通過隨機數字產生 加減乘除的 組合,通過大量的測試來命中的解法
提示:
1. 需要考慮用括弧控制計算次序的問題 比如:( 10 - 4 ) * ( 3 + A ), 實際上計算次序的數目是有限的:
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2. 需要考慮計算結果為分數的情況:( 3 + (3 / 7) ) * 7
3. 題目中牌的位置可以任意交換
*/
bool TryResolve(char* question, char* answer)
{
int oper[3]; // 存儲運算符,0:加法 1:減法 2:乘法 3:除法
for(int i=0; i<1000 * 1000; i++)
{
// 打亂紙牌順序
shuffle(question);
// 隨機產生運算符
for(int j=0; j<3; j++)
oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;
} return false;
}
int main(int argc, char* argv[])
{
// 初始化隨機種子
srand( (unsigned)time( NULL ) ); char buf1[4]; // 題目
char buf2[30]; // 解答
printf("***************************\n");
printf("計算24\n");
printf("A J Q K 均按1計算,其它按牌點計算\n");
printf("目標是:通過四則運算組合出結果:24\n");
printf("***************************\n\n");
for(;;)
{
GivePuzzle(buf1); // 出題
printf("題目:");
for(int j=0; j<4; j++){
if( buf1[j] == 'T' )
printf("10 ");
else
printf("%c ", buf1[j]);
} printf("\n按任意鍵參考答案...\n");
getch(); if( TryResolve(buf1, buf2) ) // 解題
printf("參考:%s\n", buf2);
else
printf("可能是無解...\n"); printf("按任意鍵出下一題目,x 鍵退出...\n");
if( getch() == 'x' ) break;
} return 0;
}
Ⅲ c語言游戲編程
你錯了~
理論上說,其他語言做到的c語言一定能夠做到~而且效率高得多,只不過其他語言一句話就能做到的,C語言可能要上百行代碼~
c語言時代,一個大軟體
動輒幾十萬行,幾百萬行代碼,其中出一個錯誤,是絕對令人抓狂的,所以有c++,java,。net
等應運而生~
linux
系統是純c語言寫的哦~
也能有vista
那樣美的圖形用戶界面~
發明c語言的人真的很了不起~
從匯編的角度看C語言是垃圾,從C語言的角度看其他語言,同樣是垃圾~
其中也包括了懷舊的
MAX
3
界面(3DSMaxR3.cui)。當然如果你有足夠的時間和耐心,也可以自己設計...
Ⅳ 關於用C語言編寫的小游戲的游戲代碼,如黑白棋貪吃蛇等
我這兒有c語言的自寫俄羅斯方塊,請指教:#include
#include
#include
#include
#include
#include
#include
#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define SPACE 0x3920
#define Y 0x1579
#define N 0x316e
#define clearkbd(); while(bioskey(1)) bioskey(0); /*清空鍵盤緩沖隊列*/
void update();
void messagebox();
void process();
void initremove();
void initinfo();
void initbox();
void initposition();
void next_shape();
typedef struct shape /*形狀單一狀態的記錄*/
{ int attr;
int co[8];
}shape;
typedef struct RE_AB /*相對,絕對坐標記錄*/
{ int Rx,Ry;
int x1,x2,y1,y2;
}RE_AB;
RE_AB RA;
shape p[19]={ { RED,0,1,1,0,1,1,2,1 }, /*數組中保證y最大的在最後,以便initposition使用*/
{ RED,0,1,1,0,1,1,1,2 },
{ RED,0,0,1,0,2,0,1,1 },
{ RED,0,0,0,1,1,1,0,2 },
{ GREEN,0,0,1,0,2,0,3,0 },
{ GREEN,0,0,0,1,0,2,0,3 },
{ CYAN,0,0,0,1,1,0,1,1 },
{ BROWN,0,0,1,0,1,1,2,1 },
{ BROWN,1,0,0,1,1,1,0,2 },
{ BLUE,1,0,2,0,1,1,0,1 },
{ BLUE,0,0,0,1,1,1,1,2 },
{ MAGENTA,0,0,0,1,0,2,1,2 },
{ MAGENTA,2,0,0,1,1,1,2,1},
{ MAGENTA,0,0,1,0,1,1,1,2 },
{ MAGENTA,0,0,0,1,1,0,2,0 },
{ YELLOW,0,2,1,0,1,1,1,2 },
{ YELLOW,0,0,1,0,2,0,2,1 },
{ YELLOW,1,0,0,0,0,1,0,2},
{ YELLOW,0,0,0,1,1,1,2,1 },
};
int nback,nleft,nright,r_f[12][22],rs1,rs2,xcors,xcorb,ycors,ycorb;
/*檢查方快有沒有左,右,下接觸,游戲區內所有格子有無顏色記錄數組,rs1形狀記錄,rs2為提示框用,記錄小格子在游戲區中的位置,按鍵存儲*/
void interrupt (*oldint)(); /*系統定時中斷*/
int count_down=0,count_other=0; /*中斷記時*/
void interrupt newint() /*設置新的中斷程序*/
{ count_down++;
count_other++;
oldint();
}
void intenable() /*設置中斷向量表,啟動新的中斷程序*/
{ oldint=getvect(0x1c);
disable();
setvect(0x1c,newint);
enable();
}
void intrestore() /*恢復中斷向量*/
{ disable();
setvect(0x1c,oldint);
enable();
}
void HZ12(int x0,int y0,int w,int color,char *s) /*根據字模,在dos下顯示漢字*/
/*橫坐標,縱坐標,字間隔,漢字顏色,漢字字元串*/
{ FILE *fp;
register char buffer[24];
register char str[2];
unsigned long fpos;/*fpos為最終偏移動量*/
register int i,j,k;
fp=fopen(hzk12,r);/*打開12*12漢字苦*/
while(*s)/*一直到字元串結束為止*/
{
if(*s<0)/*漢字輸出*/
{ str[0]=(*s)-0xa0;
str[1]=*(s+1)-0xa0;
fpos=((str[0]-1)*94+(str[1]-1))*24L;/*計算漢字在hzk12的偏移量*/
fseek(fp,fpos,SEEK_SET);/*指針移動到當前位置*/
fread(buffer,24,1,fp);/*讀取一個漢字到數組中*/
for(i=0;i<12;i++)/*12行*/
for(j=0;j<2;j++)/*兩個位元組*/
for(k=0;k<8;k++)/*8位*/
if (((buffer[i*2+j]>>(7-k))&0x1)!=NULL)/*是一就畫點*/
putpixel(x0+8*j+k,y0+i,color);
s+=2;/*一個漢字占兩個位元組,現在將指針移動兩個位元組*/
x0+=w;/*顯示坐標也按照間隔移動*/
}
else/*顯示非漢字字元*/
{ settextstyle(0,0,1);
setcolor(color);
str[0]=*s;str[1]=0;
outtextxy(x0,y0+3,str);/*顯示單個字元*/
x0+=w-7;/*顯示單個字元後的x坐標變化*/
s++;/*指針移動到下一個位元組*/
}
}
fclose(fp);
}
void translation() /*把相對坐標解釋為絕對坐標*/
{ if(RA.Rx==1)
{ RA.x1=1; RA.x2=16; }
else
{ RA.x1=16*(RA.Rx-1); RA.x2=16*RA.Rx; }
if(RA.Ry==1)
{ RA.y1=1; RA.y2=16; }
else
{ RA.y1=16*(RA.Ry-1); RA.y2=16*RA.Ry; }
}
int check_b() /*檢查是否到達低部*/
{ int x,y,i,zf=0; /*zf為是否有顏色填充記錄*/
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>=6)
zf+=r_f[x-15][y-6+1];
}
if(zf==0)
return 1;
else
return 0;
}
int finish()
{ int tfull=0,i; /*判斷頂層空間是否有填充*/
for(i=1;i<11;i++)
tfull+=r_f[i][1];
if(tfull!=0)
return 1; /*告訴judge()可以結束了*/
}
int check_l() /*檢查形狀是否與左接觸*/
{ int x,y,i,zf=0;
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15-1][y-6];
if(y<=6&&x==16)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}
int check_r() /*檢查形狀是否與右接觸*/
{ /*zf為是否有顏色填充記錄*/
int x,y,i,zf=0; /*zf為是否有顏色填充記錄*/
for(i=0;i<7;i++,i++)
{
x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15+1][y-6];
if(y<=6&&x==25)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}
void check_touch()
{ nback=check_b();
nleft=check_l();
nright=check_r();
}
void draw(int cb) /*畫形狀,cb=1以填充色畫形狀,cb=2以背景色畫形狀,cb=3以白色畫形狀*/
{ int i,recordx=RA.Rx,recordy=RA.Ry;
for(i=0;i<7;i++,i++)
{ RA.Rx+=p[rs1].co[i];
RA.Ry+=p[rs1].co[i+1];
if(RA.Ry<=6)
{ RA.Rx=recordx;
RA.Ry=recordy;
continue;
}
translation();
if(cb==1)
setfillstyle(1,p[rs1].attr);
else
if(cb==2)
setfillstyle(1,BLACK);
else
if(cb==3)
{ setfillstyle(1,WHITE);
r_f[RA.Rx-15][RA.Ry-6]=1; /*置對應數組標記元素*/
}
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}
void mov(int key) /*向下,左,右移動方塊*/
{ draw(2);
if(key==LEFT&&nleft)
RA.Rx--;
else
if(key==RIGHT&&nright)
RA.Rx++;
else
RA.Ry++;
nback=check_b();
if(nback) /*判斷形狀有沒有到達底部,有就將其顏色變為白色*/
draw(1);
else
draw(3);
}
void change() /*變換形狀*/
{ int status=rs1,buffer,i,x,y,zf=0;
if(p[rs1].attr==p[rs1+1].attr)
rs1++;
else
while(p[rs1].attr==p[rs1-1].attr)
rs1--;
for(i=0;i<7;i++,i++) /*檢查變化形狀後是否與已存形狀發生沖突*/
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15][y-6];
}
if(zf!=0)
rs1=status;
buffer=rs1;
rs1=status;
status=buffer;
draw(2);
buffer=rs1;
rs1=status;
status=buffer;
nback=check_b(); /*判斷變化後的形狀是不是到達了低部,這個檢查是十分必要的*/
if(nback)
draw(1);
else
draw(3);
}
void accelerate()
{ if(count_down>=1)
{ check_touch(); /*消除上一步動作對方塊狀態的影響*/
count_down=0;
if(nback) /*0表示到達底部,1表示沒有到達*/
mov(DOWN);
}
}
void drawbox() /*畫方塊所在方框*/
{ int xcor,ycor;
for(xcor=xcors;xcor<=xcorb;xcor++)
for(ycor=ycors;ycor<=ycorb;ycor++)
{ if(xcor==xcors||xcor==xcorb||ycor==ycors||ycor==ycorb)
{ RA.Rx=xcor;
RA.Ry=ycor;
translation();
setfillstyle(1,DARKGRAY);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
}
}
}
void erasure(int k)
{ int i,j,recordx=RA.Rx,recordy=RA.Ry;
{ j=k-1;
for(;j>0;j--)
{ for(i=1;i<11;i++)
{ r_f[i][j+1]=r_f[i][j];
RA.Rx=i+15;
RA.Ry=j+1+6;
translation();
if(r_f[i][j+1]==1)
setfillstyle(1,WHITE);
else
setfillstyle(1,BLACK);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}
}
}
void pause()
{ HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,暫停);
for(;;)
if(bioskey(1)&&bioskey(0)==SPACE)
{ clearkbd();
HZ12(450,400,15,BLACK,暫停);
HZ12(450,400,15,RED,正常);
return;
}
}
void judge()
{ int i,j,full=0; /*full等於10說明某一行滿,該消除了*/
if(finish()) /*判斷游戲是否該結束了*/
messagebox(); /*win編程里有這個函數*/
for(j=1;j<21;j++) /*判斷某一行是否滿了*/
{ for(i=1;i<11;i++)
full+=r_f[i][j];
if(full==10)
erasure(j); /*消除這行*/
full=0;
}
}
void update() /*使程序可以重新運行*/
{ cleardevice();
setbkcolor(BLACK);
initinfo(); /*提示信息初始化*/
initbox(); /*游戲框架初始化*/
srand((unsigned)time(NULL)); /*隨機器函數的初始化*/
rs1=random(19);
rs2=random(19);
next_shape();
initposition(); /*方塊最開始的出現位置*/
initremove(); /*記錄每個方格有無顏色填充數組初始化*/
HZ12(450,400,15,RED,正常);
process();
}
void EXIT()
{ closegraph();
intrestore(); /*恢復中斷向量*/
exit(0);
}
void initremove()
{ int i,j;
for(i=0;i<12;i++)
for(j=0;j<22;j++)
if(i==0||i==11||j==0||j==21)
r_f[i][j]=1;
else
r_f[i][j]=0;
}
void initinfo()
{ char aStr[2];
setcolor(RED);
outtextxy(450,100,This game's writer is:);
HZ12(450,140,15,RED,該程序作者:NULL);
outtextxy(525,110,NULL);
outtextxy(450,180,FUNCTION FOR KEYS:);
outtextxy(450,200,UP:change the shape);
outtextxy(450,210,DOWN:accelerate);
outtextxy(450,220,LEFT:move left);
outtextxy(450,230,RIGHT:move right);
outtextxy(450,240,ESC:exit this game);
outtextxy(450,250,SPACE:pause);
HZ12(450,260,20,RED,上:);
HZ12(450,280,20,RED,下:);
HZ12(450,300,20,RED,左:);
HZ12(450,320,20,RED,右:);
HZ12(450,340,20,RED,ESC:退出);
HZ12(450,360,15,RED,空格: 暫停/開始);
HZ12(450,380,15,RED,目前狀態:);
HZ12(20,200,15,RED,下一個形狀);
aStr[0]=24;
aStr[1]=0;
aStr[6]=0;
HZ12(480,260,12,GREEN,aStr);
HZ12(500,260,12,GREEN,( 變形 ));
aStr[0]=25;
aStr[1]=0;
HZ12(480,280,12,GREEN,aStr);
HZ12(500,280,12,GREEN,( 加速 ));
aStr[0]=27;
aStr[1]=0;
HZ12(480,300,12,GREEN,aStr);
HZ12(500,300,12,GREEN,向左);
aStr[0]=26;
aStr[1]=0;
HZ12(480,320,12,GREEN,aStr);
HZ12(500,320,12,GREEN,向右);
}
void messagebox()
{ int key;
setcolor(GREEN);
setfillstyle(1,DARKGRAY);
rectangle(220,200,420,300);
bar(221,201,419,299);
HZ12(280,210,15,GREEN,GAME OVER);
HZ12(275,230,15,GREEN,重新游戲: Y);
HZ12(275,270,15,GREEN,退出遊戲: N);
HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,GAME OVER);
for(;;)
if(bioskey(1))
{ key=bioskey(0);
if(key==Y)
{ clearkbd();
update();
}
else
if(key==N)
{ clearkbd();
EXIT();
}
else
clearkbd();
}
}
void initbox()
{ xcors=15; /*畫游戲框*/
xcorb=26;
ycors=6;
ycorb=27;
drawbox();
xcors=2; /*畫提示框*/
xcorb=7;
ycors=6;
ycorb=11;
drawbox();
}
void initposition()
{ RA.Rx=18;
RA.Ry=6-p[rs1].co[7];;
RA.x1=0;
RA.x2=0;
RA.y1=0;
RA.y2=0;
}
void next_shape() /*畫下一形狀提示框*/
{ int recordx=RA.Rx,recordy=RA.Ry,buffer;
RA.Rx=3;
RA.Ry=7;
draw(2);
buffer=rs1;
rs1=rs2;
rs2=buffer;
draw(1);
RA.Rx=recordx;
RA.Ry=recordy;
buffer=rs1;
rs1=rs2;
rs2=buffer;
}
void process() /*游戲過程*/
{ for(;;)
{ check_touch();
if(!nback)
{ rs1=rs2;
rs2=random(19); /*產生另一種方塊的碼數*/
initposition();
judge(); /*判斷某一行是否滿了和這個游戲是否可以結束了*/
draw(1);
next_shape();
}
if(count_other>=1)
{ count_other=0;
if(bioskey(1)) /*對按鍵的處理*/
{ int key=bioskey(0);
clearkbd(); /*清除鍵盤緩沖隊列*/
if(key==ESC)
EXIT();
if(key==LEFT&&nleft&&nback)
mov(LEFT);
if(key==RIGHT&&nright&&nback)
mov(RIGHT);
if(key==UP&&nback)
change();
if(key==SPACE)
pause();
if(key==DOWN)
accelerate();
}
}
if(count_down>=4)
{ check_touch(); /*消除上一步動作對方塊狀態的影響*/
count_down=0;
if(nback) /*0表示到達底部,1表示沒有到達*/
mov(DOWN);
}
}/*for*/
}
main()
{ int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,d:turboc); /*啟動圖形與中斷部分*/
intenable();
update();
}
Ⅳ 跪求100行左右的c語言簡單代碼,大一水平就行,什麼類型都可以。
//學生成績管理系統C代碼
/*頭文件*/
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>/*其它說明*/
#include<string.h>/*字元串函數*/
#include<mem.h>/*內存操作函數*/
#include<ctype.h>/*字元操作函數*/
#include<alloc.h>/*動態地址分配函數*/
#defineLENsizeof(STUDENT)
typedefstructstu/*定義結構體數組用於緩存數據*/
{
charnum[6];
charname[5];
intscore[3];
intsum;
floataverage;
intorder;
structstu*next;
}STUDENT;
/*函數原型*/
STUDENT*init();/*初始化函數*/
intmenu_select();/*菜單函數*/
STUDENT*create();/*創建鏈表*/
voidprint(STUDENT*head);/*顯示全部記錄*/
voidsearch(STUDENT*head);/*查找記錄*/
STUDENT*delete(STUDENT*head);/*刪除記錄*/
STUDENT*sort(STUDENT*head);/*排序*/
STUDENT*insert(STUDENT*head,STUDENT*newnode);/*插入記錄*/
voidsave(STUDENT*head);/*保存文件*/
STUDENT*load();/*讀文件*/
/*主函數界面*/
main()
{
STUDENT*head,newnode;
head=init();/*鏈表初始化,使head的值為NULL*/
for(;;)/*循環無限次*/
{
switch(menu_select())
{
case1:head=create();break;
case2:print(head);break;
case3:search(head);break;
case4:head=delete(head);break;
case5:head=sort(head);break;
case6:head=insert(head,&newnode);break;/*&newnode表示返回地址*/
case7:save(head);break;
case8:head=load();break;
case9:exit(0);/*如菜單返回值為9則程序結束*/
}
}
}
/*初始化函數*/
STUDENT*init()
{
returnNULL;/*返回空指針*/
}
/*菜單選擇函數*/
menu_select()
{
intn;
structdated;/*定義時間結構體*/
getdate(&d);/*讀取系統日期並把它放到結構體d中*/
printf("pressanykeytoenterthemenu......");/*按任一鍵進入主菜單*/
getch();/*從鍵盤讀取一個字元,但不顯示於屏幕*/
clrscr();/*清屏*/
printf("******************************************************************************** ");
printf(" Welcometo ");
printf(" Thestudentscoremanagesystem ");
printf("*************************************MENU*************************************** ");
printf(" 1.Entertherecord ");/*輸入學生成績記錄*/
printf(" 2.Printtherecord ");/*顯示*/
printf(" 3.Searchrecordonname ");/*尋找*/
printf(" 4.Deletearecord ");/*刪除*/
printf(" 5.Sorttomakenewafile ");/*排序*/
printf(" 6.Insertrecordtolist ");/*插入*/
printf(" 7.Savethefile ");/*保存*/
printf(" 8.Loadthefile ");/*讀取*/
printf(" 9.Quit ");/*退出*/
printf(" MadebyHuHaihong. ");
printf("******************************************************************************** ");
printf(" %d\%d\%d ",d.da_year,d.da_mon,d.da_day);/*顯示當前系統日期*/
do{
printf(" Enteryourchoice(1~9):");
scanf("%d",&n);
}while(n<1||n>9);/*如果選擇項不在1~9之間則重輸*/
return(n);/*返回選擇項,主函數根據該數調用相應的函數*/
}
/*輸入函數*/
STUDENT*create()
{
inti,s;
STUDENT*head=NULL,*p;/*定義函數.此函數帶回一個指向鏈表頭的指針*/
clrscr();
for(;;)
{p=(STUDENT*)malloc(LEN);/*開辟一個新的單元*/
if(!p)/*如果指針p為空*/
{printf(" Outofmemory.");/*輸出內存溢出*/
return(head);/*返回頭指針,下同*/
}
printf("Enterthenum(0:listend):");
scanf("%s",p->num);
if(p->num[0]=='0')break;/*如果學號首字元為0則結束輸入*/
printf("Enterthename:");
scanf("%s",p->name);
printf("Pleaseenterthe%dscores ",3);/*提示開始輸入成績*/
s=0;/*計算每個學生的總分,初值為0*/
for(i=0;i<3;i++)/*3門課程循環3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0||p->score[i]>100)/*確保成績在0~100之間*/
printf("Dataerror,pleaseenteragain. ");
}while(p->score[i]<0||p->score[i]>100);
s=s+p->score[i];/*累加各門成績*/
}
p->sum=s;/*將總分保存*/
p->average=(float)s/3;/*先用強制類型轉換將s轉換成float型,再求平均值*/
p->order=0;/*未排序前此值為0*/
p->next=head;/*將頭結點做為新輸入結點的後繼結點*/
head=p;/*新輸入結點為新的頭結點*/
}
return(head);
}
/*顯示全部記錄函數*/
voidprint(STUDENT*head)
{
inti=0;/*統計記錄條數*/
STUDENT*p;/*移動指針*/
clrscr();
p=head;/*初值為頭指針*/
printf(" ************************************STUDENT************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Rec|Num|Name|Sc1|Sc2|Sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
while(p!=NULL)
{
i++;
printf("|%3d|%4s|%-4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
i,p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("------------------------------------------------------------------------------- ");
printf("**************************************END************************************** ");
}
/*查找記錄函數*/
voidsearch(STUDENT*head)
{
STUDENT*p;/*移動指針*/
chars[5];/*存放姓名用的字元數組*/
clrscr();
printf("Pleaseenternameforsearching. ");
scanf("%s",s);
p=head;/*將頭指針賦給p*/
while(strcmp(p->name,s)&&p!=NULL)/*當記錄的姓名不是要找的,或指針不為空時*/
p=p->next;/*移動指針,指向下一結點*/
if(p!=NULL)/*如果指針不為空*/
{printf(" *************************************FOUND************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Num|Name|sc1|sc2|sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
printf("|%4s|%4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("------------------------------------------------------------------------------- ");
printf("***************************************END************************************** ");
}
else
printf(" Thereisnonum%sstudentonthelist. ",s);/*顯示沒有該學生*/
}
/*刪除記錄函數*/
STUDENT*delete(STUDENT*head)
{intn;
STUDENT*p1,*p2;/*p1為查找到要刪除的結點指針,p2為其前驅指針*/
charc,s[6];/*s[6]用來存放學號,c用來輸入字母*/
clrscr();
printf("Pleaseenterthedeletednum:");
scanf("%s",s);
p1=p2=head;/*給p1和p2賦初值頭指針*/
while(strcmp(p1->num,s)&&p1!=NULL)/*當記錄的學號不是要找的,或指針不為空時*/
{p2=p1;/*將p1指針值賦給p2作為p1的前驅指針*/
p1=p1->next;/*將p1指針指向下一條記錄*/
}
if(strcmp(p1->num,s)==0)/*學號找到了*/
{printf("**************************************FOUND************************************ ");
printf("------------------------------------------------------------------------------- ");
printf("|Num|Name|sc1|sc2|sc3|Sum|Ave|Order| ");
printf("------------------------------------------------------------------------------- ");
printf("|%4s|%4s|%3d|%3d|%3d|%3d|%4.2f|%-5d| ",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("------------------------------------------------------------------------------- ");
printf("***************************************END************************************** ");
printf("AreyousuretodeletethestudentY/N?");/*提示是否要刪除,輸入Y刪除,N則退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N')break;/*如果不刪除,則跳出本循環*/
if(c=='y'||c=='Y')
{
if(p1==head)/*若p1==head,說明被刪結點是首結點*/
head=p1->next;/*把第二個結點地址賦予head*/
else
p2->next=p1->next;/*否則將一下結點地址賦給前一結點地址*/
n=n-1;
printf(" Num%sstudenthavebeendeleted. ",s);
printf("Don'tforgettosave. ");break;/*刪除後就跳出循環*/
}
}
}
else
printf(" Thereisnonum%sstudentonthelist. ",s);/*找不到該結點*/
return(head);
}
/*排序函數*/
STUDENT*sort(STUDENT*head)
{inti=0;/*保存名次*/
STUDENT*p1,*p2,*t,*temp;/*定義臨時指針*/
temp=head->next;/*將原表的頭指針所指的下一個結點作頭指針*/
head->next=NULL;/*第一個結點為新表的頭結點*/
while(temp!=NULL)/*當原表不為空時,進行排序*/
{
t=temp;/*取原表的頭結點*/
temp=temp->next;/*原表頭結點指針後移*/
p1=head;/*設定移動指針p1,從頭指針開始*/
p2=head;/*設定移動指針p2做為p1的前驅,初值為頭指針*/
while(t->average<p1->average&&p1!=NULL)/*作成績平均分比較*/
{
p2=p1;/*待排序點值小,則新表指針後移*/
p1=p1->next;
}
if(p1==p2)/*p1==p2,說明待排序點值大,應排在首位*/
{
t->next=p1;/*待排序點的後繼為p*/
head=t;/*新頭結點為待排序點*/
}
else/*待排序點應插入在中間某個位置p2和p1之間,如p為空則是尾部*/
{
t->next=p1;/*t的後繼是p1*/
p2->next=t;/*p2的後繼是t*/
}
}
p1=head;/*已排好序的頭指針賦給p1,准備填寫名次*/
while(p1!=NULL)/*當p1不為空時,進行下列操作*/
{
i++;/*結點序號*/
p1->order=i;/*將結點序號賦值給名次*/
p1=p1->next;/*指針後移*/
}
printf("Sortingissucessful. ");/*排序成功*/
return(head);
}
/*插入記錄函數*/
STUDENT*insert(STUDENT*head,STUDENT*newnode)
{STUDENT*p0,*p1,*p2;
intn,sum1,i;
p1=head;/*使p1指向第一個結點*/
p0=newnode;/*p0指向要插入的結點*/
printf(" Pleaseenteranewnoderecord. ");/*提示輸入記錄信息*/
printf("Enterthenum:");
scanf("%s",newnode->num);
printf("Enterthename:");
scanf("%s",newnode->name);
printf("Pleaseenterthe%dscores. ",3);
sum1=0;/*保存新記錄的總分,初值為0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("Dataerror,pleaseenteragain. ");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1+newnode->score[i];/*累加各門成績*/
}
newnode->sum=sum1;/*將總分存入新記錄中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==NULL)/*原來的鏈表是空表*/
{head=p0;p0->next=NULL;}/*使p0指向的結點作為頭結點*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1;/*使p2指向剛才p1指向的結點*/
p1=p1->next;/*p1後移一個結點*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0;/*插到原來第一個結點之前*/
elsep2->next=p0;/*插到p2指向的結點之後*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}/*插到最後的結點之後*/
}
n=n+1;/*結點數加1*/
head=sort(head);/*調用排序的函數,將學生成績重新排序*/
printf(" Student%shavebeeninserted. ",newnode->name);
printf("Don'tforgettosavethenewnodefile. ");
return(head);
}
/*保存數據到文件函數*/
voidsave(STUDENT*head)
{FILE*fp;/*定義指向文件的指針*/
STUDENT*p;/*定義移動指針*/
charoutfile[10];
printf("Enteroutfilename,forexamplec:\score ");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)/*為輸出打開一個二進制文件,為只寫方式*/
{
printf("Cannotopenthefile ");
return;/*若打不開則返回菜單*/
}
printf(" Savingthefile...... ");
p=head;/*移動指針從頭指針開始*/
while(p!=NULL)/*如p不為空*/
{
fwrite(p,LEN,1,fp);/*寫入一條記錄*/
p=p->next;/*指針後移*/
}
fclose(fp);/*關閉文件*/
printf("Savethefilesuccessfully! ");
}
/*從文件讀數據函數*/
STUDENT*load()
{STUDENT*p1,*p2,*head=NULL;/*定義記錄指針變數*/
FILE*fp;/*定義指向文件的指針*/
charinfile[10];
printf("Enterinfilename,forexamplec:\score ");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)/*打開一個二進制文件,為只讀方式*/
{
printf("Cannotopenthefile. ");
return(head);
}
printf(" Loadingthefile! ");
p1=(STUDENT*)malloc(LEN);/*開辟一個新單元*/
if(!p1)
{
printf("Outofmemory! ");
return(head);
}
head=p1;/*申請到空間,將其作為頭指針*/
while(!feof(fp))/*循環讀數據直到文件尾結束*/
{
if(fread(p1,LEN,1,fp)!=1)break;/*如果沒讀到數據,跳出循環*/
p1->next=(STUDENT*)malloc(LEN);/*為下一個結點開辟空間*/
if(!p1->next)
{
printf("Outofmemory! ");
return(head);
}
p2=p1;/*使p2指向剛才p1指向的結點*/
p1=p1->next;/*指針後移,新讀入數據鏈到當前表尾*/
}
p2->next=NULL;/*最後一個結點的後繼指針為空*/
fclose(fp);
printf("! ");
return(head);
}
Ⅵ 用C語言編寫的小游戲代碼是什麼
/*貪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],
int zuobiao[2][80],
char direction);
zuobiao[0][tail]=1;
zuobiao[1][tail]=1;
zuobiao[0][1]=1;
zuobiao[1][1]=2;zuobiao[0
[2]=1;
zuobiao[1][2]=3;
zuobiao[0][head]=1;
zuobiao[1][head]=4;
/*處理棋盤*/
char qipan[20][80];
//定義棋盤
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盤
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
Ⅶ c語言小游戲代碼
「貪吃蛇」C代碼,在dev C++試驗通過(用4個方向鍵控制)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#define W 78 //游戲框的寬,x軸
#define H 26 //游戲框的高,y軸
int dir=3; //方向變數,初值3表示向「左」
int Flag=0; //吃了食物的標志(1是0否)
int score=0; //玩家得分
struct food{ int x; //食物的x坐標
int y; //食物的y坐標
}fod; //結構體fod有2個成員
struct snake{ int len; //蛇身長
int speed; //移動速度
int x[100]; //蛇身某節x坐標
int y[100]; //蛇身某節y坐標
}snk; //結構體snk有4個成員
void gtxy( int x,int y) //控制游標移動的函數
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void gtxy( int x,int y); //以下聲明要用到的幾個自編函數
void csh( ); //初始化界面
void keymove( ); //按鍵操作移動蛇
void putFod( ); //投放食物
int Over( ); //游戲結束(1是0否)
void Color(int a); //設定顯示顏色的函數
int main( ) //主函數
{ csh( );
while(1)
{ Sleep(snk.speed);
keymove( );
putFod( );
if(Over( ))
{ system(「cls」);
gtxy(W/2+1,H/2); printf(「游戲結束!T__T」);
gtxy(W/2+1,H/2+2); printf(「玩家總分:%d分」,score);
getch( );
break;
}
}
return 0;
}
void csh( ) //初始化界面
{ int i;
gtxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下兩行是隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<=W;i=i+2) //橫坐標要為偶數,因為這個要列印的字元佔2個位置
{Color(2); //設定列印顏色為綠色
gtxy(i,0); printf("■"); //列印上邊框
gtxy(i,H); printf("■"); //列印下邊框
}
for(i=1;i<H;i++)
{ gtxy(0,i); printf("■"); //列印左邊框
gtxy(W,i); printf("■"); //列印右邊框
}
while(1)
{ srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )
fod.x=rand()%(W-4)+2; //隨機函數rand( )產生一個從0到比」(W-4)」小1的數再加2
fod.y=rand()%(H-2)+1; //隨機函數rand( )產生一個從0到比」(H-2)」小1的數再加1
if (fod.x%2==0) break; //fod.x是食物的橫坐標,要是2的倍數(為偶數)
}
Color(12); //設定列印顏色為淡紅
gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物
snk.len=3; //蛇身長初值為3節
snk.speed=350; //刷新蛇的時間,即移動速度初值為350毫秒
snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)
snk.y[0]=H/2; //蛇頭縱坐標
Color(9); //設定列印顏色為淡藍
gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭
for(i=1;i<snk.len;i++)
{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];
gtxy(snk.x[i],snk.y[i]); printf("■"); //列印蛇身
}
Color(7, 0); //恢復默認的白字黑底
return;
}
void keymove( ) //按鍵操作移動蛇
{ int key;
if( kbhit( ) ) //如有按鍵輸入才執行下面操作
{ key=getch( );
if (key==224) //值為224表示按下了方向鍵,下面要再次獲取鍵值
{ key=getch( );
if(key==72&&dir!=2)dir=1; //72表示按下了向上方向鍵
if(key==80&&dir!=1)dir=2; //80為向下
if(key==75&&dir!=4)dir=3; //75為向左
if(key==77&&dir!=3)dir=4; //77為向右
}
if (key==32)
{ while(1) if((key=getch( ))==32) break; } //32為空格鍵,這兒用來暫停
}
if (Flag==0) //如沒吃食物,才執行下面操作擦掉蛇尾
{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }
int i;
for (i = snk.len - 1; i > 0; i--) //從蛇尾起每節存儲前一節坐標值(蛇頭除外)
{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }
switch (dir) //判斷蛇頭該往哪個方向移動,並獲取最新坐標值
{ case 1: snk.y[0]--; break; //dir=1要向上移動
case 2: snk.y[0]++; break; //dir=2要向下移動
case 3: snk.x[0]-=2; break; //dir=3要向左移動
case 4: snk.x[0]+=2; break; //dir=4要向右移動
}
Color(9);
gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭
if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物則執行以下操作
{ printf("7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是響鈴
else Flag = 0; //沒吃到食物Flag的值為0
if(snk.speed<150) snk.speed= snk.speed+5; //作弊碼,不讓速度無限加快
}
void putFod( ) //投放食物
{ if (Flag == 1) //如吃到食物才執行以下操作,生成另一個食物
{ while (1)
{ int i,n= 1;
srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )
fod.x = rand( ) % (W - 4) + 2; //產生在游戲框范圍內的一個x坐標值
fod.y = rand( ) % (H - 2) + 1; //產生在游戲框范圍內的一個y坐標值
for (i = 0; i < snk.len; i++) //隨機生成的食物不能在蛇的身體上
{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }
if (n && fod.x % 2 == 0) break; //n不為0且橫坐標為偶數,則食物坐標取值成功
}
Color(12); //設定字元為紅色
gtxy(fod.x, fod.y); printf("●"); //游標到取得的坐標處列印食物
}
return;
}
int Over( ) //判斷游戲是否結束的函數
{ int i;
Color(7);
gtxy(2,H+1); printf(「暫停鍵:space.」); //以下列印一些其它信息
gtxy(2,H+2); printf(「游戲得分:%d」,score);
if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇頭觸碰左右邊界
if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇頭觸碰上下邊界
for (i = 1; i < snk.len; i++)
{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇頭觸碰自身
return 0; //沒碰到邊界及自身時就返回0
}
void Color(int a) //設定顏色的函數
{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }