當前位置:首頁 » 編程語言 » c語言300行搶紅包代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言300行搶紅包代碼

發布時間: 2022-06-17 10:41:52

⑴ 完成一個貪吃蛇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語言編程:求一段發紅包的代碼。(隨機數,能規定紅包總錢數總人數,每個人拿到的錢不為0)

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
intmain(void)
{
floattotal;
printf("輸入總錢數: ");
scanf("%f",&total);

intnum;
printf("輸入紅包數量: ");
scanf("%d",&num);

floatmin=0.01;
floatsafe_total;
floatmoney;
inti;
srand((unsigned)time(NULL));
for(i=1;i<num;i++){
safe_total=(total-(num-i)*min)/(num-1);
money=(float)(rand()%((int)(safe_total*100)))/100+min;
total=total-money;
printf("紅包%2d:%.2f元,余額:%.2f元 ",i,money,total);
}
printf("紅包%2d:%.2f元,余額:0.00元 ",num,total);
return0;
}

⑶ 求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!=80&&a[(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==LEFT&&x>120)/*左方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)/*右方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)/*上方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)/*下方向鍵*/
{
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(y<100)/*如果是從起點出發就恢復藍色*/
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(j<6)/*往右邊*/
{
for(k=j+1;k<8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格結束*/
break;
if(a[i][k]!=0&&k<8)
{
for(kk=j+1;kk<k&&k<8;kk++)/*判斷右邊*/
{
a[i][kk]=a[i][j]; /*改變棋子顏色*/
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1) /*條件成立則有棋子改變過顏色*/
yes=1;
}
}
if(j>1)/*判斷左邊*/
{
for(k=j-1;k>=0;k--)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)/*判斷下邊*/
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk<k&&k<8;kk++)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)/*判斷上邊*/
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)/*右上*/
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)
{
for(ii=i-1,jj=j+1;ii>k&&k>=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(i<6&&j>1)/*左下*/
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)/*左上*/
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=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(i<6&&j<6)/* 右下*/
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii<k&&k<8;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;i<8;i++)
for(j=0;j<8;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(score2>score1)/*開始判斷最後的結果*/
outtextxy(100,50,"black win!");
else
if(score2<score1)
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}

五子棋游戲
/*五子棋*/
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<bios.h>
#include<conio.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-1<0)
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(i<1)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case RIGHT :

if(step_x+1>18)
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(i>18)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(j>18)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(j<1)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case ESC :
break ;

case SPACE :
if(step_x>=1&&step_x<=18&&step_y>=1&&step_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!=SPACE&&key!=ESC);
}
while(key!=ESC);
closegraph();
}

⑷ C語言紅包代碼

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
inti,n;
floata[100],all;
srand(time(NULL));
printf("請輸入紅包金額:");
scanf("%f",&all);
printf("請輸入紅包個數:");
scanf("%d",&n);
srand((unsigned)time(0));
for(i=1;i<n;i)
{
a[i]=(float)rand()/RAND_MAX*all;
if(a[i]>0)
{
all-=a[i];
printf("%f ",a[i]);
i++;
}
}
printf("%f ",all);
}

⑸ 求c語言/c++大神編寫發紅包問題

完整代碼如下:

#include<stdio.h>
voidmain()
{
intmoney;
intmoney1,money2,money5,money10,money20,money50,money100;
printf("請輸入金額:");
scanf("%d",&money);
money100=money/100;
money=money%100;
money50=money/50;
money=money%50;
money20=money/20;
money=money%20;
money10=money/10;
money=money%10;
money5=money/5;
money=money%5;
money2=money/2;
money1=money%2;
printf("100元:%d張 ",money100);
printf("50元:%d張 ",money50);
printf("20元:%d張 ",money20);
printf("10元:%d張 ",money10);
printf("5元:%d張 ",money5);
printf("2元:%d張 ",money2);
printf("1元:%d張 ",money1);
}

運行結果如下圖:

這時我們再編譯、鏈接、執行一下:i = 10, j = 3i=、,、空格和j=全都原樣輸出了。此外需要注意的是:「輸出控制符」和「輸出參數」無論在「順序上」還是在「個數上」一定要一一對應。

4) printf("輸出控制符 非輸出控制符",輸出參數);

這實際上就是上面那個例子。這時候會有一個問題:到底什麼是「輸出控制符」,什麼是「非輸出控制符」?很簡單,凡是以%開頭的基本上都是輸出控制符。

⑹ 大學,C語言程序設計課程設計,至少編寫代碼300行左右

這種題目很多比如員工管理系統,圖書管理系統,電話本之類的而且代碼在網上都很好找,因為太多了,最主要的是完整的設計也很多,比如員工管理系統這樣的完整的設計和代碼直接網路就行了,當然如果你只是想完成作業這些就夠了,你要是想自己研究就要你自己參考了,不過既然你在這提問了也就是說你是第一種,所以去網路吧

⑺ C語言 微信紅包

#include<stdio.h>
#include<string.h>
#include<time.h>

#defineMAX_TOTAL_MONEY200//紅包的最大金額
#defineMIN_PER_PLAYER1//一個人搶到的的最小面額1元
#defineMAX_PLAYER_CNT(MAX_TOTAL_MONEY/MIN_PER_PLAYER)//最大搶紅包的游戲人數

typedefstructplayer
{
char*name;//標記玩家可以不填
unsignedintmoney_get;//搶到的紅包
}PLAYER_T;

//每個人領取到的紅包金額不等這個要求比較難搞暫時不考慮
intmain(intargc,char*argv[])
{
unsignedinttotal_money=0;//不考慮角和分浮點運算比較復雜
unsignedintplayer_cnt=0;
inton_off=0;
inti=0;
intj=0;
PLAYER_Tplayer[MAX_PLAYER_CNT]={0};
PLAYER_Ttmp={0};

printf("輸入紅包金額: ");
scanf("%u",&total_money);
printf("輸入游戲人數: ");
scanf("%u",&player_cnt);
printf("是否需要減小貧富差距(0為關閉其餘為開啟): ");
scanf("%u",&on_off);

//不符合規則的輸入判斷
if(total_money>MAX_TOTAL_MONEY||0==total_money||0==player_cnt||player_cnt*MIN_PER_PLAYER>total_money)
{
printf("紅包金額最小%u元最大%u元游戲人數最小1人最大%u人 ",MIN_PER_PLAYER,MAX_TOTAL_MONEY,MAX_PLAYER_CNT);
return0;
}

for(i=0;i<player_cnt;i++)
{
//設置隨機種子
srand(time(NULL)+i);
//根據隨機種子獲取一個偽隨機數作為搶到的紅包並通過余運算使其始終小於total_money
player[i].money_get=rand()%total_money;

//限制所有人所能搶到的最大紅包為當前金額池的1/5而不是全部
if(0!=on_off)
{
if(total_money>5)//5塊錢以上再限制
{
player[i].money_get=rand()%(total_money/5);
}
}

//最後一個人拿所有剩下的紅包
if(player_cnt-1==i)
{
player[i].money_get=total_money;
}
//運氣差隨機到0元給你最小面額
elseif(0==player[i].money_get)
{
player[i].money_get=MIN_PER_PLAYER;
}
//剩下的要保證每個人能搶到最小面額
elseif(total_money-player[i].money_get<(player_cnt-i-1)*MIN_PER_PLAYER)
{
player[i].money_get=total_money-(player_cnt-i-1)*MIN_PER_PLAYER;
}

//把搶到的金額從紅包池中減掉
total_money-=player[i].money_get;
//如果填了name可以把名字列印出來
printf("第%d個玩家搶到紅包:%u元 ",i+1,player[i].money_get);
}

//冒泡排序找出手氣最佳者
for(i=0;i<player_cnt;i++)
{
for(j=i+1;j<player_cnt;j++)
{
if(player[i].money_get<player[j].money_get)
{
memcpy(&tmp,&player[j],sizeof(PLAYER_T));
memcpy(&player[j],&player[i],sizeof(PLAYER_T));
memcpy(&player[i],&tmp,sizeof(PLAYER_T));
}
}
}
printf("手氣最佳者搶到紅包:%u元 ",player[0].money_get);//如果填了name可以把名字列印出來
return0;
}

⑻ 求一個300行左右的簡單的c語言程序

#include<stdio.h>
#include<malloc.h>
#include<string.h>

#define LEN sizeof(struct record) /*對結構體長度進行宏定義*/

void menu();/*聲明菜單函數*/
struct record*insert(struct record *head);/*聲明添加函數 */
struct record*delet(struct record *head); /*聲明刪除函數 */
struct record*alter(struct record *head); /*聲明修改函數 */
void search(struct record *head); /*聲明查找函數*/
void show(struct record *head); /*聲明顯示函數*/

struct record *head; /*定義全局結構體指針變數*/
int n=0; /*定義全局變數*/

struct record /*聲明結構體*/
{
char number[10];
char name[20];
char phone[20];
char adress[40];
char postcode[10];
char e_mail[30];
struct record *next;
};

/******************************************************************************
* *
* 主函數 *
* *
*******************************************************************************/
main()
{
head=NULL;
menu();
rewind(stdin);
}

/******************************************************************************
* *
* 菜單函數 *
* *
*******************************************************************************/
void menu()
{
int choice;
printf("\n\t\t******************** 主菜單 ********************");
printf("\n\t\t*********** 1-添加紀錄 2-查詢紀錄 ************");
printf("\n\t\t*********** 3-刪除紀錄 4-修改記錄 ************");
printf("\n\t\t*********** 5-顯示紀錄 6-退出系統 ************");
printf("\n\t\t************************************************");
printf("\n\t\t請選擇:");
scanf("%d",&choice); rewind(stdin);
printf("\n");
switch (choice)
{
case 1:
head=insert(head);
rewind(stdin);
menu();
break;
case 2:
search(head);rewind(stdin);
menu();
break;
case 3:
head=delet(head);
rewind(stdin);
menu();
break;
case 4:
head=alter(head);
rewind(stdin);
menu();
break;
case 5:
show(head);
rewind(stdin);
menu();
break;
default:
printf("\n\t\t謝謝使用!!");
break;
}
}

/******************************************************************************
* *
* 添加函數 *
* *
*******************************************************************************/
struct record *insert(struct record *head)
{
struct record *pp,*p1,*p2;
pp=(struct record *)malloc(LEN);
printf("\n\t\t**************** 請輸入用戶信息 ****************\n");
printf("\n\t\t輸入序號:");
scanf("%s",pp->number); rewind(stdin);
printf("\n\t\t輸入姓名:");
scanf("%s",pp->name);rewind(stdin);
printf("\n\t\t輸入電話號碼:");
scanf("%s",pp->phone); rewind(stdin);
printf("\n\t\t輸入地址:");
scanf("%s",pp->adress); rewind(stdin);
printf("\n\t\t輸入郵編:");
scanf("%s",pp->postcode); rewind(stdin);
printf("\n\t\t輸入e-mail:");
scanf("%s",pp->e_mail); rewind(stdin);
if(head==NULL)/*在表頭插入1*/
{
head=pp;
pp->next=NULL;
}
else
{
p1=head;
while((strcmp(pp->number,p1->number)>0)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(strcmp(pp->number,p1->number)<=0)
{
if(head==p1)
head=pp; /*在表頭插入2*/
else
p2->next=pp;/*在表中插入*/
pp->next=p1;
}
else /*在表尾插入*/
{
p1->next=pp;
pp->next=NULL;
}
}
printf("\t添加成功!請繼續選擇功能鍵!\n\n");
n=n+1;
return(head);
}
/******************************************************************************
* *
* 刪除函數 *
* *
*******************************************************************************/
struct record*delet(struct record * head)
{
struct record *p1,*p2;
char number[10];
printf("\t請輸入要刪除用戶的序號number:");
scanf("%s",&number);rewind(stdin);
if(head==NULL)
{
printf("\n\t通訊錄無用戶信息記錄!!\n");
return(head);
}
p1=head;
while(strcmp(number,p1->number)!=0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(number,p1->number)==0)
{
if(p1==head)
{
head=p1->next;
printf("\t刪除成功!請繼續選擇功能鍵!\n\n");
}

else
{
p2=p1->next;
printf("\t已刪 除的用戶的序號為:%s\n",number);
printf("\t刪除成功!請繼續選擇功能鍵!\n\n");
}
n=n-1;
}
else printf("\t通訊錄無該用戶的信息記錄!\n ");
return(head);
}
/******************************************************************************
* *
* 查詢函數 *
* *
*******************************************************************************/
void search(struct record *head)
{
int a;
char f_name[20],f_number[10];
struct record *p1,*p2;
if(head==NULL)
{
printf("\t通訊錄無用戶信息記錄\n");
return;
}
else
{
printf("\t請你選擇你查找的方式:\n\n\t\t1:序號\n\t\t2:姓名\n");
printf("\t\t請選擇:");
scanf("%d",&a);rewind(stdin);
switch(a)
{case 1:
printf("\n\t請輸入要查找用戶的序號number:");
scanf("%s",&f_number);rewind(stdin);
p1=head;
while(strcmp(p1->number,f_number)!=0)
{
if(p1->next==NULL)
{
printf("\n\t通訊錄無此用戶信息記錄\n");
return;
}
else
{
p2=p1->next;
p1=p2;
}
}
printf("\n\t要查找用戶的基本信息為:\n");
printf("\t\t序號: %s\n\t\t姓名:%s\n\t\t電話號碼:%s",p1->number,p1->name,p1->phone);
printf("\n\t\t地址:%s\n\t\t郵編:%s\n\t\te_mail:%s\n",p1->adress,p1->postcode,p1->e_mail);
break;
case 2:
printf("\n\t請輸入要查找用戶的姓名name:");
scanf("%s",f_name);rewind(stdin);
p1=head;
while(strcmp(p1->name,f_name)!=0)
{
if(p1->next==NULL)
{
printf("\n\t通訊錄無此用戶信息記錄\n");
return;
}
else
{
p2=p1->next;
p1=p2;
}
}
printf("\n\t要查找用戶的基本信息為:\n");
printf("\t\t序號: %s\n\t\t姓名:%s\n\t\t電話號碼:%s",p1->number,p1->name,p1->phone);
printf("\n\t\t地址:%s\n\t\t郵編:%s\n\t\te_mail:%s",p1->adress,p1->postcode,p1->e_mail);
break;
}
}
}
/******************************************************************************
* *
* 顯示函數 *
* *
*******************************************************************************/
void show(struct record *head)
{
int i;
struct record *p1,*p2;
p1=head;
if(head==NULL)
{
printf("\t通訊錄無用戶信息記錄\n");
return;
}
else
{
for(i=1;i<=n;i++)
{
printf("\n\t第%d個用戶的基本信息為:",i);
printf("\n\t\t序號: %s 姓名:%s 電話號碼:%s \n\t\t地址:%s 郵編:%s e_mail:%s\n"
,p1->number,p1->name,p1->phone,p1->adress,p1->postcode,p1->e_mail);
p2=p1->next;
p1=p2;
}
}
}

/******************************************************************************
* *
* 修改函數 *
* *
*******************************************************************************/
struct record*alter(struct record*head)
{
struct record *p1,*p2;
int choice1;
char alter_number[10],alter_name[20],alter_phone[20],alter_adress[40],alter_postcode[10],alter_e_mail[30],choice2;
p1=head;
if(head==NULL)
{
printf("通訊錄無用戶信息記錄\n");
return(head);
}
printf("\t請輸入要修改的用戶的序號number:");
scanf("%s",alter_number);
rewind(stdin);
while(strcmp(p1->number,alter_number)!=0)
{
if(p1->next==NULL)
{
printf("\n\t通訊錄無此用戶信息記錄\n");
return(head);
}
else
{
p2=p1;
p1=p1->next;
}

}
if(strcmp(p1->number,alter_number)!=0)
{
printf("通訊錄無用戶信息記錄\n");
return(head);
}
else
{
printf("\t要修改的用戶的基本信息為:\n\t");
printf("\t序號: %s 姓名:%s 電話號碼:%s 地址:%s 郵編:%s e_mail:%s\n"
,p1->number,p1->name,p1->phone,p1->adress,p1->postcode,p1->e_mail);
}

while(1)
{
printf("\t你是否要修改的用戶的基本信息?(y&n)");
scanf("%c",&choice2);
rewind(stdin);
if(choice2=='y')
{
printf("\t請選擇你要修改的項目:\n\t");
printf("1:姓名 2:電話號碼 3:地址 4:郵編 5:e_mail\n");
printf("\t你選擇的序號為: ");
scanf("%d",&choice1);
rewind(stdin);
switch(choice1)
{case 1:printf("\t請輸入更改後的姓名");
scanf("%s",alter_name);rewind(stdin);
strcpy(p1->name,alter_name);
continue;
case 2:printf("\t請輸入更改後的電話號碼");
scanf("%s",alter_phone);rewind(stdin);
strcpy(p1->phone,alter_phone);
continue;
case 3:printf("\t請輸入更改後的地址");
scanf("%s",alter_adress);rewind(stdin);
strcpy(p1->adress,alter_adress);
continue;
case 4:printf("\t請輸入更改後的郵編");
scanf("%s",&alter_postcode);rewind(stdin);strcpy(p1->postcode,alter_postcode);
continue;
case 5:printf("\t請輸入更改後的e_mail");
scanf("%s",alter_e_mail);rewind(stdin);
strcpy(p1->e_mail,alter_e_mail);
continue;
}
printf("\n\t修改後用戶的基本信息為:\n\t");
printf("\t序號: %s 姓名:%s 電話號碼:%s 地址:%s 郵編:%s e_mail\n"
,p1->number,p1->name,p1->phone,p1->adress,p1->postcode,p1->e_mail);
}
else
{
printf("\n\t修改成功!!\n");
break;
}
}
return(head);
}

⑼ 急求一個300——400行的C語言代碼,哪個大神能給個啊什麼程序都成。。。。。謝啦!!!

源代碼——C語言實現列印楊輝三角(源代碼)

楊輝三角形是形如:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
的三角形,其實質是二項式(a+b)的n次方展開後各項的系數排成的三角形,它的特點是左右兩邊全是1,從第二行起,中間的每一個數是上一行里相鄰兩個數之和。這個題目常用於程序設計的練習。
下面給出六種不同的解法。
解法一
#include <stdio.h>
main()
{ int i,j,n=0,a[17][17]={0};
while(n<1 || n>16)
{ printf("請輸入楊輝三角形的行數:");
scanf("%d",&n);
}
for(i=0;i<n;i++)
a[i][0]=1;
for(i=1;i<n;i++)
for(j=1;j<=i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=0;i<n;i++)
{ for(j=0;j<=i;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
點評:解法一是一般最容易想到的解法,各部分功能獨立,程序淺顯易懂。
解法二
#include <stdio.h>
main()
{ int i,j,n=0,a[17][17]={1};
while(n<1 || n>16)
{ printf("請輸入楊輝三角形的行數:");
scanf("%d",&n);
}
for(i=1;i<n;i++)
{ a[i][0]=1;
for(j=1;j<=i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i<n;i++)
{ for(j=0;j<=i;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
點評:解窢二是在解法一的基礎上,把第一列置為1的命令移到下面的雙重循環中,減少了一個循環。注意初始化數組的變化。
解法三
#include <stdio.h>
main()
{ int i,j,n=0,a[17][17]={0,1};
while(n<1 || n>16)
{ printf("請輸入楊輝三角形的行數:");
scanf("%d",&n);
}
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=1;i<=n;i++)
{ for(j=1;j<=i;j++) printf("%5d",a[i][j]);
printf("\n");
}
}
點評:解法三是在解法一、二的基礎上,把第一列置為1的命令去掉了,注意初始化數組的變化。
解法四
#include <stdio.h>
main()
{ int i,j,n=0,a[17][17]={0,1};
while(n<1 || n>16)
{ printf("請輸入楊輝三角形的行數:");
scanf("%d",&n);
}
for(i=1;i<=n;i++)
{ for(j=1;j<=i;j++)
{ a[i][j]=a[i-1][j-1]+a[i-1][j];
printf("%5d",a[i][j]);
}
printf("\n");
}
}
點評:解法四是在解法三的基礎上,把計算和列印合並在一個雙重循環中。
解法五
#include <stdio.h>
main()
{ int i,j,n=0,a[17]={1},b[17];
while(n<1 || n>16)
{ printf("請輸入楊輝三角形的行數:");
scanf("%d",&n);
}
for(i=0;i<n;i++)
{ b[0]=a[0];
for(j=1;j<=i;j++)
b[j]=a[j-1]+a[j];
for(j=0;j<=i;j++)
{ a[j]=b[j];
printf("%5d",a[j]);
}
printf("\n");
}
}
點評:解法一到解法四都用了二維數組,佔用的空間較多。而解法五隻使用了兩個一維數組。
解法六
#include <stdio.h>
main()
{ int i,j,n=0,a[17]={0,1},l,r;
while(n<1 || n>16)
{ printf("請輸入楊輝三角形的行數:");
scanf("%d",&n);
}
for(i=1;i<=n;i++)
{ l=0;
for(j=1;j<=i;j++)
{ r=a[j];
a[j]=l+r;
l=r;
printf("%5d",a[j]);
}
printf("\n");
}
}
點評:解法六隻使用了一個一維數組和兩個臨時變數。

⑽ 幫我找個c語言程序 300行左右的 要每行後面都有很全的注釋,謝謝了

#include "stdio.h" /*I/O函數*/
#include "stdlib.h" /*其它說明*/
#include "string.h" /*字元串函數*/
#include "conio.h" /*屏幕操作函數*/
#include "mem.h" /*內存操作函數*/
#include "ctype.h" /*字元操作函數*/
#include "alloc.h" /*動態地址分配函數*/
#define N 3 /*定義常數*/
typedef struct z1 /*定義數據結構*/
{
char no[11];
char name[15];
int score[N];
float sum;
float average;
int order;
struct z1 *next;
}STUDENT;
/*以下是函數原型*/
STUDENT *init(); /*初始化函數*/
STUDENT *create(); /*創建鏈表*/
STUDENT *delete(STUDENT *h); /*刪除記錄*/
void print(STUDENT *h); /* 顯示所有記錄*/
void search(STUDENT *h); /*查找*/
void save(STUDENT *h); /*保存*/
STUDENT *load(); /*讀入記錄*/
void computer(STUDENT *h); /*計算總分和均分*/
STUDENT *insert(STUDENT *h); /*插入記錄*/
void append(); /*追加記錄*/
void (); /*復制文件*/
STUDENT *sort(STUDENT *h); /*排序*/
STUDENT *index(STUDENT *h); /*索引*/
void total(STUDENT *h); /*分類合計*/
int menu_select(); /*菜單函數*/
/******主函數開始*******/
main()
{
int i;
STUDENT *head; /*鏈表定義頭指針*/
head=init(); /*初始化鏈表*/
clrscr(); /*清屏*/
for(;;) /*無限循環*/
{
switch(menu_select()) /*調用主菜單函數,返回值整數作開關語句的條件*/
{ /*值不同,執行的函數不同,break 不能省略*/
case 0:head=init();break; /*執行初始化*/
case 1:head=create();break; /*創建鏈表*/
case 2:head=delete(head);break; /*刪除記錄*/
case 3:print(head);break; /*顯示全部記錄*/
case 4:search(head);break; /*查找記錄*/
case 5:save(head);break; /*保存文件*/
case 6:head=load(); break; /*讀文件*/
case 7:computer(head);break; /*計算總分和均分*/
case 8:head=insert(head); break; /*插入記錄*/
case 9:();break; /*復制文件*/
case 10:head=sort(head);break; /*排序*/
case 11:append();break; /*追加記錄*/
case 12:head=index(head);break; /*索引*/
case 13:total(head);break; /*分類合計*/
case 14:exit(0); /*如菜單返回值為14程序結束*/
}
}
}
/*菜單函數,返回值為整數*/
menu_select()
{
char *menu[]={"***************MENU***************", /*定義菜單字元串數組*/
" 0. init list", /*初始化*/
" 1. Enter list", /*輸入記錄*/
" 2. Delete a record from list", /*從表中刪除記錄*/
" 3. print list ", /*顯示單鏈表中所有記錄*/
" 4. Search record on name", /*按照姓名查找記錄*/
" 5. Save the file", /*將單鏈表中記錄保存到文件中*/
" 6. Load the file", /*從文件中讀入記錄*/
" 7. compute the score", /*計算所有學生的總分和均分*/
" 8. insert record to list ", /*插入記錄到表中*/
" 9. the file to new file", /*復制文件*/
" 10. sort to make new file", /*排序*/
" 11. append record to file", /*追加記錄到文件中*/
" 12. index on nomber", /*索引*/
" 13. total on nomber", /*分類合計*/
" 14. Quit"}; /*退出*/
char s[3]; /*以字元形式保存選擇號*/
int c,i; /*定義整形變數*/
gotoxy(1,25); /*移動游標*/
printf("press any key enter menu......\n"); /*壓任一鍵進入主菜單*/
getch(); /*輸入任一鍵*/
clrscr(); /*清屏幕*/
gotoxy(1,1); /*移動游標*/
textcolor(YELLOW); /*設置文本顯示顏色為黃色*/
textbackground(BLUE); /*設置背景顏色為藍色*/
gotoxy(10,2); /*移動游標*/
putch(0xc9); /*輸出左上角邊框┏*/
for(i=1;i<44;i++)
putch(0xcd); /*輸出上邊框水平線*/
putch(0xbb); /*輸出右上角邊框 ┓*/
for(i=3;i<20;i++)
{
gotoxy(10,i);putch(0xba); /*輸出左垂直線*/
gotoxy(54,i);putch(0xba);
} /*輸出右垂直線*/
gotoxy(10,20);putch(0xc8); /*輸出左上角邊框┗*/
for(i=1;i<44;i++)
putch(0xcd); /*輸出下邊框水平線*/
putch(0xbc); /*輸出右下角邊框┛*/
window(11,3,53,19); /* 製作顯示菜單的窗口,大小根據菜單條數設計*/
clrscr(); /*清屏*/
for(i=0;i<16;i++) /*輸出主菜單數組*/
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK); /*設置背景顏色為黑色*/
window(1,1,80,25); /*恢復原窗口大小*/
gotoxy(10,21); /*移動游標*/
do{
printf("\n Enter you choice(0~14):"); /*在菜單窗口外顯示提示信息*/
scanf("%s",s); /*輸入選擇項*/
c=atoi(s); /*將輸入的字元串轉化為整形數*/
}while(c<0||c>14); /*選擇項不在0~14之間重輸*/
return c; /*返回選擇項,主程序根據該數調用相應的函數*/
}
STUDENT *init()
{
return NULL;
}

/*創建鏈表*/
STUDENT *create()
{
int i; int s;
STUDENT *h=NULL,*info; /* STUDENT指向結構體的指針*/
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info) /*如果指針info為空*/
{
printf("\nout of memory"); /*輸出內存溢出*/
return NULL; /*返回空指針*/
}
inputs("enter no:",info->no,11); /*輸入學號並校驗*/
if(info->no[0]=='@') break; /*如果學號首字元為@則結束輸入*/
inputs("enter name:",info->name,15); /*輸入姓名,並進行校驗*/
printf("please input %d score \n",N); /*提示開始輸入成績*/
s=0; /*計算每個學生的總分,初值為0*/
for(i=0;i<N;i++) /*N門課程循環N次*/
{
do{
printf("score%d:",i+1); /*提示輸入第幾門課程*/
scanf("%d",&info->score[i]); /*輸入成績*/
if(info->score[i]>100||info->score[i]<0) /*確保成績在0~100之間*/
printf("bad data,repeat input\n"); /*出錯提示信息*/
}while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i]; /*累加各門課程成績*/
}
info->sum=s; /*將總分保存*/
info->average=(float)s/N; /*求出平均值*/
info->order=0; /*未排序前此值為0*/
info->next=h; /*將頭結點做為新輸入結點的後繼結點*/
h=info; /*新輸入結點為新的頭結點*/
}
return(h); /*返回頭指針*/
}
/*輸入字元串,並進行長度驗證*/
inputs(char *prompt, char *s, int count)
{
char p[255];
do{
printf(prompt); /*允鞠提示信息*/
scanf("%s",p); /*輸入字元串*/
if(strlen(p)>count)printf("\n too long! \n"); /*進行長度校驗,超過count值重輸入*/
}while(strlen(p)>count);
strcpy(s,p); /*將輸入的字元串拷貝到字元串s中*/
}
/*輸出鏈表中結點信息*/
void print(STUDENT *h)
{
int i=0; /* 統計記錄條數*/
STUDENT *p; /*移動指針*/
clrscr(); /*清屏*/
p=h; /*初值為頭指針*/
printf("\n\n\n****************************STUDENT********************************\n");
printf("|rec|nO | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|---|----------|---------------|----|----|----|--------|-------|-----|\n");
while(p!=NULL)
{
i++;
printf("|%3d |%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", i, p->no,p->name,p->score[0],p->score[1],
p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("**********************************end*********************************\n");
}

/*刪除記錄*/
STUDENT *delete(STUDENT *h)
{
STUDENT *p,*q; /*p為查找到要刪除的結點指針,q為其前驅指針*/
char s[11]; /*存放學號*/
clrscr(); /*清屏*/
printf("please deleted no\n"); /*顯示提示信息*/
scanf("%s",s); /*輸入要刪除記錄的學號*/
q=p=h; /*給q和p賦初值頭指針*/
while(strcmp(p->no,s)&&p!=NULL) /*當記錄的學號不是要找的,或指針不為空時*/
{
q=p; /*將p指針值賦給q作為p的前驅指針*/
p=p->next; /*將p指針指向下一條記錄*/
}
if(p==NULL) /*如果p為空,說明鏈表中沒有該結點*/
printf("\nlist no %s student\n",s);
else /*p不為空,顯示找到的記錄信息*/
{
printf("*****************************have found***************************\n");
printf("|no | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|----------|---------------|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,
p->average,p->order);
printf("********************************end*******************************\n");
getch(); /*壓任一鍵後,開始刪除*/
if(p==h) /*如果p==h,說明被刪結點是頭結點*/
h=p->next; /*修改頭指針指向下一條記錄*/
else
q->next=p->next; /*不是頭指針,將p的後繼結點作為q的後繼結點*/
free(p); /*釋放p所指結點空間*/
printf("\n have deleted No %s student\n",s);
printf("Don't forget save\n");/*提示刪除後不要忘記保存文件*/
}
return(h); /*返回頭指針*/
}
/*查找記錄*/
void search(STUDENT/*當記錄的姓名不是要找的,或指針不為空時*/
{ p=p->next; /*移動指針,指向下一結點*/
if(p==NULL) /*如果指針為空*/
printf("\nlist no %s student\n",s); /*顯示沒有該學生*/
else /*顯示找到的記錄信息*/
{
printf (*h)
{
STUDENT *p; /* 移動指針*/
char s[15]; /*存放姓名的字元數組*/
clrscr(); /*清屏幕*/
printf("please enter name for search\n");
scanf("%s",s); /*輸入姓名*/
p=h; /*將頭指針賦給p*/
while(strcmp(p->name,s)&&p!=NULL) ("\n\n*****************************havefound***************************\n");
printf("|nO | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|----------|---------------|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("********************************end*******************************\n");
}
}
}
/*插入記錄*/
STUDENT *insert(STUDENT *h)
{
STUDENT *p,*q,*info; /*p指向插入位置,q是其前驅,info指新插入記錄*/
char s[11]; /*保存插入點位置的學號*/
int s1,i;
printf("please enter location before the no\n");
scanf("%s",s); /*輸入插入點學號*/
printf("\nplease new record\n"); /*提示輸入記錄信息*/
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info)
{
printf("\nout of memory"); /*如沒有申請到,內存溢出*/
return NULL; /*返回空指針*/
}
inputs("enter no:",info->no,11); /*輸入學號*/
inputs("enter name:",info->name,15); /*輸入姓名*/
printf("please input %d score \n",N); /*提示輸入分數*/
s1=0; /*保存新記錄的總分,初值為0*/
for(i=0;i<N;i++) /*N門課程循環N次輸入成績*/
{
do{ /*對數據進行驗證,保證在0~100之間*/

printf("score%d:",i+1);
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)
printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0);
s1=s1+info->score[i]; /*計算總分*/
}
info->sum=s1; /*將總分存入新記錄中*/
info->average=(float)s1/N; /*計算均分*/
info->order=0; /*名次賦值0*/
info->next=NULL; /*設後繼指針為空*/
p=h; /*將指針賦值給p*/
q=h; /*將指針賦值給q*/
while(strcmp(p->no,s)&&p!=NULL) /*查找插入位置*/
{
q=p; /*保存指針p,作為下一個p的前驅*/
p=p->next; /*將指針p後移*/
}
if(p==NULL) /*如果p指針為空,說明沒有指定結點*/
if(p==h) /*同時p等於h,說明鏈表為空*/
h=info; /*新記錄則為頭結點*/
else
q->next=info; /*p為空,但p不等於h,將新結點插在表尾*/
else
if(p==h) /*p不為空,則找到了指定結點*/
{
info->next=p; /*如果p等於h,則新結點插入在第一個結點之前*/
h=info; /*新結點為新的頭結點*/
}
else
{
info->next=p; /*不是頭結點,則是中間某個位置,新結點的後繼為p*/
q->next=info; /*新結點作為q的後繼結點*/
}
printf("\n ----have inserted %s student----\n",info->name); printf("---Don't forget save---\n"); /*提示存檔*/
return(h); /*返回頭指針*/
}
/*保存數據到文件*/
void save(STUDENT *h)
{
FILE *fp; /*定義指向文件的指針*/
STUDENT *p; /* 定義移動指針*/
char outfile[10]; /*保存輸出文件名*/
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示文件名格式信息*/
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*為輸出打開一個二進制文件,如沒有則建立*/
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file......\n"); /*打開文件,提示正在保存*/
p=h; /*移動指針從頭指針開始*/
while(p!=NULL) /*如p不為空*/
{
fwrite(p,sizeof(STUDENT),1,fp);/*寫入一條記錄*/
p=p->next; /*指針後移*/
}
fclose(fp); /*關閉文件*/
printf("-----save success!!-----\n"); /*顯示保存成功*/
}
/* 從文件讀數據*/
STUDENT *load()
{
STUDENT *p,*q,*h=NULL; /*定義記錄指針變數*/
FILE *fp; /* 定義指向文件的指針*/
char infile[10]; /*保存文件名*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n"); scanf("%s",infile); /*輸入文件名*/
if((fp=fopen(infile,"rb"))==NULL) /*打開一個二進制文件,為讀方式*/
{
printf("can not open file\n"); /*如不能打開,則結束程序*/
exit(1);
}
printf("\n -----Loading file!-----\n");
p=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!p)
{
printf("out of memory!\n"); /*如沒有申請到,則內存溢出*/
return h; /*返回空頭指針*/
}
h=p; /*申請到空間,將其作為頭指針*/
while(!feof(fp)) /*循環讀數據直到文件尾結束*/
{
if(1!=fread(p,sizeof(STUDENT),1,fp))
break; /*如果沒讀到數據,跳出循環*/
p->next=(STUDENT *)malloc(sizeof(STUDENT)); /*為下一個結點申請空間*/
if(!p->next)
{
printf("out of memory!\n"); /*如沒有申請到,則內存溢出*/
return h;
}
q=p; /*保存當前結點的指針,作為下一結點的前驅*/
p=p->next; /*指針後移,新讀入數據鏈到當前表尾*/
}
q->next=NULL; /*最後一個結點的後繼指針為空*/
fclose(fp); /*關閉文件*/
printf("---You have success read data from file!!!---\n");
return h; /*返回頭指針*/
}
/*追加記錄到文件*/
void append()

{
FILE *fp; /*定義指向文件的指針*/
STUDENT *info; /*新記錄指針*/
int s1,i;
char infile[10]; /*保存文件名*/
printf("\nplease new record\n");
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info)
{
printf("\nout of memory"); /*沒有申請到,內存溢出本函數結束*/
return ;
}
inputs("enter no:",info->no,11); /*調用inputs輸入學號*/
inputs("enter name:",info->name,15); /*調用inputs輸入姓名*/
printf("please input %d score \n",N); /*提示輸入成績*/
s1=0;
for(i=0;i<N;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&info->score[i]); /*輸入成績*/
if(info->score[i]>100||info->score[i]<0)printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0); /*成績數據驗證*/
s1=s1+info->score[i]; /*求總分*/
}
info->sum=s1; /*保存總分*/
info->average=(float)s1/N; /*求均分*/
info->order=0; /*名次初始值為0*/
info->next=NULL; /*將新記錄後繼指針賦值為空*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n"); scanf("%s",infile); /*輸入文件名*/
if((fp=fopen(infile,"ab"))==NULL) /*向二進制文件尾增加數據方式打開文件*/
{
printf("can not open file\n"); /*顯示不能打開*/
exit(1); /*退出程序*/
}
printf("\n -----Appending record!-----\n");
if(1!=fwrite(info,sizeof(STUDENT),1,fp)) /*寫文件操作*/
{
printf("-----file write error!-----\n");
return; /*返回*/
}
printf("-----append sucess!!----\n");
fclose(fp); /*關閉文件*/
}
/*文件拷貝*/
void ()
{
char outfile[10],infile[10];
FILE *sfp,*tfp; /*源和目標文件指針*/
STUDENT *p=NULL; /*移動指針*/
clrscr(); /*清屏*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n");
scanf("%s",infile); /*輸入源文件名*/
if((sfp=fopen(infile,"rb"))==NULL) /*二進制讀方式打開源文件*/
{
printf("can not open input file\n");
exit(0);
}
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示輸入目標文件名*/
scanf("%s",outfile); /*輸入目標文件名*/
if((tfp=fopen(outfile,"wb"))==NULL) /*二進制寫方式打開目標文件*/
{
printf("can not open output file \n");
exit(0);
}
while(!feof(sfp)) /*讀文件直到文件尾*/
{
if(1!=fread(p,sizeof(STUDENT),1,sfp))
break; /*塊讀*/
fwrite(p,sizeof(STUDENT),1,tfp); /*塊寫*/
}
fclose(sfp); /*關閉源文件*/
fclose(tfp); /*關閉目標文件*/
printf("you have success file!!!\n"); /*顯示成功拷貝*/
}
/*排序*/
STUDENT *sort(STUDENT *h)
{
int i=0; /*保存名次*/
STUDENT *p,*q,*t,*h1; /*定義臨時指針*/
h1=h->next; /*將原表的頭指針所指的下一個結點作頭指針*/
h->next=NULL; /*第一個結點為新表的頭結點*/
while(h1!=NULL) /*當原表不為空時,進行排序*/
{
t=h1; /*取原表的頭結點*/
h1=h1->next; /*原表頭結點指針後移*/
p=h; /*設定移動指針p,從頭指針開始*/
q=h; /*設定移動指針q做為p的前驅,初值為頭指針*/
while(t->sum<p->sum&&p!=NULL) /*作總分比較*/
{
q=p; /*待排序點值小,則新表指針後移*/
p=p->next;
}
if(p==q) /*p==q,說明待排序點值大,應排在首位*/
{
t->next=p; /*待排序點的後繼為p*/
h=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置q和p之間,如p為空則是尾部*/
{
t->next=p; /*t的後繼是p*/
q->next=t; /*q的後繼是t*/

}
}
p=h; /*已排好序的頭指針賦給p,准備填寫名次*/
while(p!=NULL) /*當p不為空時,進行下列操作*/
{
i++; /*結點序號*/
p->order=i; /*將名次賦值*/
p=p->next; /*指針後移*/
}
printf("sort sucess!!!\n"); /*排序成功*/
return h; /*返回頭指針*/
}
/*計算總分和均值*/
void computer(STUDENT *h)
{
STUDENT *p; /*定義移動指針*/
int i=0; /*保存記錄條數初值為0*/
long s=0; /*總分初值為0*/
float average=0; /*均分初值為0*/
p=h; /*從頭指針開始*/
while(p!=NULL) /*當p不為空時處理*/
{
s+=p->sum; /*累加總分*/
i++; /*統計記錄條數*/
p=p->next; /*指針後移*/
}
average=(float)s/i;/* 求均分,均分為浮點數,總分為整數,所以做類型轉換*/
printf("\n--All students sum score is:%ld average is %5.2f\n",s,average);
}
/*索引*/
STUDENT *index(STUDENT *h)
{
STUDENT *p,*q,*t,*h1; /*定義臨時指針*/
h1=h->next; /*將原表的頭指針所指的下一個結點作頭指針*/
h->next=NULL; /*第一個結點為新表的頭結點*/
while(h1!=NULL) /*當原表不為空時,進行排序*/
{
t=h1; /*取原表的頭結點*/
h1=h1->next; /*原表頭結點指針後移*/
p=h; /*設定移動指針p,從頭指針開始*/
q=h; /*設定移動指針q做為p的前驅,初值為頭指針*/
while(strcmp(t->no,p->no)>0&&p!=NULL) /*作學號比較*/
{
q=p; /*待排序點值大,應往後插,所以新表指針後移*/
p=p->next;
}
if(p==q) /*p==q,說明待排序點值小,應排在首位*/
{
t->next=p; /*待排序點的後繼為p*/
h=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置q和p之間,如p為空則是尾部*/
{
t->next=p; /*t的後繼是p*/
q->next=t; /*q的後繼是t*/
}
}
printf("index sucess!!!\n"); /*索引排序成功*/
return h; /*返回頭指針*/
}
/*分類合計*/
void total(STUDENT *h)
{
STUDENT *p,*q; /*定義臨時指針變數*/
char sno[9],qno[9],*ptr; /*保存班級號的*/
float s1,ave; /*保存總分和均分*/
int i; /*保存班級人數*/
clrscr(); /*清屏*/
printf("\n\n *******************Total*****************\n");
printf("---class---------sum--------------average----\n");
p=h; /*從頭指針開始*/
while(p!=NULL) /*當p不為空時做下面的處理*/
{
memcpy(sno,p->no,8); /*從學號中取出班級號*/
sno[8]='\0'; /*做字元串結束標記*/
q=p->next; /*將指針指向待比較的記錄*/
s1=p->sum; /*當前班級的總分初值為該班級的第一條記錄總分*/
ave=p->average; /*當前班級的均分初值為該班級的第一條記錄均分*/
i=1; /*統計當前班級人數*/
while(q!=NULL) /*內循環開始*/
{
memcpy(qno,q->no,8); /*讀取班級號*/
qno[8]='\0'; /*做字元串結束標記*/
if(strcmp(qno,sno)==0) /*比較班級號*/
{
s1+=q->sum; /*累加總分*/
ave+=q->average; /*累加均分*/
i++; /*累加班級人數*/
q=q->next; /*指針指向下一條記錄*/
}
else
break; /*不是一個班級的結束本次內循環*/
}
printf("%s %10.2f %5.2f\n",sno,s1,ave/i);
if(q==NULL)
break; /*如果當前指針為空,外循環結束,程序結束*/
else
p=q; /*否則,將當前記錄作為新的班級的第一條記錄開始新的比較*/
}
printf("---------------------------------------------\n");
}