本文目錄一覽:
- 1、C++語言編寫一個大概300~600行的程序解決一個實際問題。(必須使用類)
- 2、求C語言600行的學生管理系統源程序
- 3、用c語言編寫的一個小程序,200行以上,急!!!
- 4、找人幫我編一個600行的代碼
- 5、求一個用C語言編寫的小遊戲代碼
- 6、求一個c語言製作的小遊戲或者小軟件,行數不小於600,挺急的
C++語言編寫一個大概300~600行的程序解決一個實際問題。(必須使用類)
從今天凌晨3點多,一直寫代碼寫到現在,除了吃飯沒休息過。希望這個程序對你有所幫助。如果有任何疑惑的地方,可以繼續提問、交流。附件里有源代碼文件。
代碼已經超過600行,這裡只有部分代碼,因為已經超過字數限制。其餘代碼可在源文件里查看。
#include iostream
#include conio.h
#include math.h
using namespace std;
#define Mc_MaxByte_MenuItem 61 //菜單項字符串長度最多61字節
#define Mc_MaxBufferSize 17 //緩存最大字節數。因為該程序只支持輸入小於等於2字節的整型,而2字節整型的二進制位數有16位,所以需要17個字節保存二進制字符串,最後一個為結束符。
#define Mc_Back (-1) //返回上一級菜單
#define Mc_Exit (-2) //退出程序
#define Mc_Error (-3) //輸入錯誤
#define Mc_Binary 0 //二進制
#define Mc_Octonary 1 //八進制
#define Mc_Decimalism 2 //十進制
#define Mc_Hexadecimal 3 //十六進制
class Calculator //計算器類
{
protected:
char chr1Buffer[Mc_MaxBufferSize]; //用於接收用戶輸入的數學算數運算式,如:“23+6*5-400/20=”
bool Chr1SameAsInt(char* chrPt,int number); //判斷2個整數是否相等。前一個是用字符串表示的整數,後一個就是int型的整數。
int Chr1ToInt(const char* chrPt); //將用字符串表示的整數轉換為int型整數
int Menu(char* chrPtMenuTitle,char chr2MenuItem[][Mc_MaxByte_MenuItem], int intItemCount,bool bCreateBackItem); //顯示一個菜單。最後一個參數表示:是否創建“返回”選項
void ClearScreen(); //清屏
void ShowInf(const char* chrInf); //顯示一條信息
void NextLine(int count); //換行
int NumberFormTransitionUI(char* title); //“整數的進制轉換”功能界面
void InputToBuffer(const char* title);
const char* GetBuffer();
void (Calculator::*funPt)(const char* chrSource,char* chrResult); //函數指針
int NumberFormTransition(int source,int target); //進制轉換函數。source源數據進制形式,target目標進制形式
void BinaryToOctonary(const char* chrPtBinary,char* chrPtOctonaryResult); //二進制轉八進制。得出的結果將用字符串表示,且用chrPtOctonaryResult接收轉換後的結果
void BinaryToDecimalism(const char* chrPtBinary,char* chrPtDecimalismResult); //二進制轉十進制
void BinaryToHexadecimal(const char* chrPtBinary,char* chrPtHexadecimalResult); //二進制轉十六進制
void OctonaryToDecimalism(const char* chrPtOctonary,char* chrPtDecimalismResult); //八進制轉十進制
void OctonaryToBinary(const char* chrPtOctonary,char* chrPtBinaryResult); //八進制轉二進制
void OctonaryToHexadecimal(const char* chrPtOctonary,char* chrPtHexadecimalResult); //八進制轉十六進制
void DecimalismToBinary(const char* chrPtDecimalism,char* chrPtBinaryResult); //十進制轉二進制
void DecimalismToOctonary(const char* chrPtDecimalism,char* chrPtOctonaryResult); //十進制轉八進制
void DecimalismToHexadecimal(const char* chrPtDecimalism,char* chrPtHexadecimalResult); //十進制轉十六進制
void HexadecimalToDecimalism(const char* chrPtHexadecimal,char* chrPtDecimalismResult); //十六進制轉十進制
void HexadecimalToOctonary(const char* chrPtHexadecimal,char* chrPtOctonaryResult); //十六進制轉八進制
void HexadecimalToBinary(const char* chrPtHexadecimal,char* chrPtBinaryResult); //十六進制轉二進制
public:
int UI(); //計算器界面
};
int Calculator::UI()
{
int code = !Mc_Exit, CodeTmp = 1;
char chr2Menu[][Mc_MaxByte_MenuItem] = { “數的進制轉換” };
while (code != Mc_Exit)
{
code = Menu(“計算器”, chr2Menu, 1, false);
switch (code)
{
case 1:
ClearScreen();
CodeTmp = NumberFormTransitionUI(chr2Menu[0]);
break;
case Mc_Error:
ShowInf(“\n 輸入錯誤!請重新輸入。 \n 按任意鍵繼續…… “);
getch();
ClearScreen();
}
if (CodeTmp == Mc_Exit)
code = Mc_Exit;
}
return code;
}
bool Calculator::Chr1SameAsInt(char* chrPt,int number)
{
int len = strlen(chrPt) – 1;
while(number len = 0)
{
if (chrPt[len] – 48 != number % 10)
break;
len –, number /= 10;
}
if (len 0 !number)
return true;
return false;
}
int Calculator::Chr1ToInt(const char* chrPt)
{
int len = strlen(chrPt), num = 0, i;
for (i = 0; len 0; len –, i ++) //將用字符串表示的數字轉換成int整數
num += (chrPt[i] – 48) * (int)pow(10, len – 1);
return num;
}
int Calculator::Menu(char* chrPtMenuTitle,char chr2MenuItem[][Mc_MaxByte_MenuItem],int intItemCount,bool bCreateBackItem)
{
int index, choice = 0, len, i;
cout”————– “chrPtMenuTitle” ————–“endlendl;
for (index = 1; index = intItemCount; index ++)
cout” “index”.”chr2MenuItem[index – 1]endl;
if (bCreateBackItem)
cout” “index ++”.””返回”endl;
cout” “index”.””退出”endl;
InputToBuffer(“請輸入相應選項的序號:”);
if (bCreateBackItem)
{
if (Chr1SameAsInt(chr1Buffer, index – 1))
return Mc_Back; //用戶選擇了返回選項
}
if (Chr1SameAsInt(chr1Buffer, index))
return Mc_Exit; //用戶選擇了退出選項
len = strlen(chr1Buffer);
for (i = 0; len 0; len –, i ++) //將用字符串表示的數字轉換成int整數
choice += (chr1Buffer[i] – 48) * (int)pow(10, len – 1);
if (choice 1 || choice index)
return Mc_Error;
return choice;
}
void Calculator::ClearScreen()
{
system(“cls”);
}
void Calculator::ShowInf(const char* chrInf)
{
coutchrInf;
}
void Calculator::NextLine(int count)
{
while (count– 0)
{
cout’\n’;
}
}
void Calculator::InputToBuffer(const char* title)
{
couttitle;
cinchr1Buffer;
}
const char* Calculator::GetBuffer()
{
return chr1Buffer;
}
void Calculator::BinaryToOctonary(const char* chrPtBinary,char* chrPtOctonaryResult)
{
int index, x, numTmp = 0;
bool bBreak;
const char* chrPt;
char chr1Tmp[6]; //十進制65535等於八進制177777,這個最大值八進制數有6位數,所以至少需要6個字節。這個臨時數組所保存的字符串不需要結束符。
chrPt = chrPtBinary + strlen(chrPtBinary) – 1; //讓chrPt指向最後一個字符
index = 0, bBreak = false;
while (bBreak == false)
{
for (x = 0, numTmp = 0; x 3; x ++, chrPt –)
{ // 01011011 –每3位二進制數對應一個八進制數。從最低位,右邊開始:011=3,011=3,01=1。則該二進制數的八進制形式為133(最後結果需要反序)
if (chrPt chrPtBinary)
numTmp += (*chrPt – 48) * (int)pow(2, x);
else
{
numTmp += (*chrPt – 48) * (int)pow(2, x);
bBreak = true;
break;
}
}
chr1Tmp[index ++] = numTmp + 48;
}
if (index 1)
{
for (– index; index 0; index –)
if (chr1Tmp[index] != ‘0’)
break;
}
else
{
chr1Tmp[0] = ‘0’;
index = 0;
}
for (; index = 0; chrPtOctonaryResult ++, index –)
*chrPtOctonaryResult = chr1Tmp[index];
*chrPtOctonaryResult = ‘\0’; //給返回參數加上結束符
}
void Calculator::DecimalismToBinary(const char* chrPtDecimalism,char* chrPtBinaryResult) //十進制轉二進制
{
int num = Chr1ToInt(chrPtDecimalism), index = 0;
char chr1Tmp[16];
while (num)
{
chr1Tmp[index ++] = num % 2 + 48;
num /= 2;
}
if (!index)
{
chr1Tmp[0] = ‘0’;
index = 1;
}
for(– index; index = 0; chrPtBinaryResult ++, index –)
*chrPtBinaryResult = chr1Tmp[index];
*chrPtBinaryResult = ‘\0’;
}
void Calculator::DecimalismToHexadecimal(const char* chrPtDecimalism,char* chrPtHexadecimalResult) //十進制轉十六進制
{
int num = Chr1ToInt(chrPtDecimalism), index = 0, tmp;
char chr1Tmp[16];
while (num)
{
tmp = num % 16;
num /= 16;
switch (tmp)
{
case 10:
chr1Tmp[index ++] = ‘A’;
break;
case 11:
chr1Tmp[index ++] = ‘B’;
break;
case 12:
chr1Tmp[index ++] = ‘C’;
break;
case 13:
chr1Tmp[index ++] = ‘D’;
break;
case 14:
chr1Tmp[index ++] = ‘E’;
break;
case 15:
chr1Tmp[index ++] = ‘F’;
break;
default:
chr1Tmp[index ++] = tmp + 48;
}
}
if (!index)
{
chr1Tmp[0] = ‘0’;
index = 1;
}
for(– index; index = 0; chrPtHexadecimalResult ++, index –)
*chrPtHexadecimalResult = chr1Tmp[index];
*chrPtHexadecimalResult = ‘\0’;
}
void Calculator::HexadecimalToDecimalism(const char* chrPtHexadecimal,char* chrPtDecimalismResult) //十六進制轉十進制
{
int x, index, NumTmp = 0;
const char* chrPt = chrPtHexadecimal + strlen(chrPtHexadecimal) – 1;
char chr1Tmp[4];
for ( x = 0; chrPt chrPtHexadecimal; chrPt –)
{
switch (*chrPt)
{
case ‘a’:
case ‘A’:
NumTmp += 10 * (int)pow(16, x ++);
break;
case ‘b’:
case ‘B’:
NumTmp += 11 * (int)pow(16, x ++);
break;
case ‘c’:
case ‘C’:
NumTmp += 12 * (int)pow(16, x ++);
break;
case ‘d’:
case ‘D’:
NumTmp += 13 * (int)pow(16, x ++);
break;
case ‘e’:
case ‘E’:
NumTmp += 14 * (int)pow(16, x ++);
break;
case ‘f’:
case ‘F’:
NumTmp += 15 * (int)pow(16, x ++);
break;
default:
NumTmp += (*chrPt – 48) * (int)pow(16, x ++);
}
}
switch (*chrPt)
{
case ‘a’:
case ‘A’:
NumTmp += 10 * (int)pow(16, x);
break;
case ‘b’:
case ‘B’:
NumTmp += 11 * (int)pow(16, x);
break;
case ‘c’:
case ‘C’:
NumTmp += 12 * (int)pow(16, x);
break;
case ‘d’:
case ‘D’:
NumTmp += 13 * (int)pow(16, x);
break;
case ‘e’:
case ‘E’:
NumTmp += 14 * (int)pow(16, x);
break;
case ‘f’:
case ‘F’:
NumTmp += 15 * (int)pow(16, x);
break;
default:
NumTmp += (*chrPt – 48) * (int)pow(16, x ++);
}
for (index = 0; NumTmp; index ++)
{
chr1Tmp[index] = NumTmp % 10 + 48;
NumTmp /= 10;
}
if (!index)
{
chr1Tmp[0] = ‘0’;
index = 1;
}
for (– index; index = 0; chrPtDecimalismResult ++, index –)
*chrPtDecimalismResult = chr1Tmp[index];
*chrPtDecimalismResult = ‘\0’;
}
void Calculator::HexadecimalToOctonary(const char* chrPtHexadecimal,char* chrPtOctonaryResult) //十六進制轉八進制
{
char chr1Tmp[6];
HexadecimalToDecimalism(chrPtHexadecimal, chr1Tmp); //先將十六進制轉化為十進制
DecimalismToOctonary(chr1Tmp, chrPtOctonaryResult); //再將十進制轉化為八進制
}
void Calculator::HexadecimalToBinary(const char* chrPtHexadecimal,char* chrPtBinaryResult) //十六進制轉二進制
{
char chr1Tmp[6];
HexadecimalToDecimalism(chrPtHexadecimal, chr1Tmp); //先將十六進制轉化為十進制
DecimalismToBinary(chr1Tmp, chrPtBinaryResult); //再將十進制轉化為二進制
}
int main()
{
Calculator cal;
cal.UI();
return 0;
}
求C語言600行的學生管理系統源程序
#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 copy(); /*複製文件*/
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:copy();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. copy 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;i44;i++)
putch(0xcd); /*輸出上邊框水平線*/
putch(0xbb); /*輸出右上角邊框 ┓*/
for(i=3;i20;i++)
{
gotoxy(10,i);putch(0xba); /*輸出左垂直線*/
gotoxy(54,i);putch(0xba);
} /*輸出右垂直線*/
gotoxy(10,20);putch(0xc8); /*輸出左上角邊框┗*/
for(i=1;i44;i++)
putch(0xcd); /*輸出下邊框水平線*/
putch(0xbc); /*輸出右下角邊框┛*/
window(11,3,53,19); /* 製作顯示菜單的窗口,大小根據菜單條數設計*/
clrscr(); /*清屏*/
for(i=0;i16;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(c0||c14); /*選擇項不在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;iN;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); /*返回頭指針*/
}
用c語言編寫的一個小程序,200行以上,急!!!
C語言課程設計報告——-學生成績簡單管理程序一、系統菜單的主要功能(1)輸入若干條記錄(2)顯示所有記錄(3)按學號排序(4)插入一條記錄(5)按姓名查找,刪除一條記錄(6)查找並顯示一條記錄(7)輸出統計信息 (新增)(8)從正文中添加數據到結構體數組中(9)將所有數據寫入文件中(0)退出程序二、題目分析該題主要考察學生對結構體,指針,文件的操作,以及C語言算法的掌握,所以完成此道題目要求較強的設計能力,尤其是要有一種大局觀的意識。如何調程序也非常重要,通過這個程序可以學習到以前調試短程序沒有的的經驗。菜單中的每一個選項都對應一個子程序,子程序的算法幾乎囊獲了所有C語言學過的技巧,下面就各個子程序中的功能進行說明:功能1和4的算法相似,輸入一條記錄到結構體中去,其中有一部很關鍵,就是通過gets將所有的多餘的字符,回車讀去,否則就會出錯。功能2是顯示所有的記錄,通過循環輸出,格式也比較重要。功能3為按學號排序,因為學號定義成了字符數組的形式,因此在運用冒泡法進行排序的時候,要用到strcmp,strcpy等函數。功能5為按姓名刪除記錄,先輸入姓名,再一一比較,如果沒有則返回失敗信息,如果找到就將此記錄都向前移一位,返回n-1。功能6的算法在5中就已經體現了,輸入姓名,一一比較。功能7為新增的功能,因為考慮到原來給出的函數中竟然沒有對學生成績的統計功能,因此新增此功能,可以得出所有的記錄個數,最高、最低、平均分,並輸出相關的學生信息等。功能8和9是對文件的操作,提前準備好數據。三、程序正文部分#includestdio.h /*引用庫函數*/#includestdlib.h#includectype.h#includestring.htypedef struct /*定義結構體數組*/{char num[10]; /*學號*/char name[20]; /*姓名*/int score; /*成績*/}Student;Student stu[80]; /*結構體數組變量*/int menu_select() /*菜單函數*/{char c;do{system(“cls”); /*運行前清屏*/printf(“\t\t****Students’ Grade Management System****\n”); /*菜單選擇*/printf(“\t\t | 1. Input Records |\n”);printf(“\t\t | 2. Display All Records |\n”);printf(“\t\t | 3. Sort |\n”);printf(“\t\t | 4. Insert a Record |\n”);printf(“\t\t | 5. Delete a Record |\n”);printf(“\t\t | 6. Query |\n”);printf(“\t\t | 7. Statistic |\n”);printf(“\t\t | 8. Add Records from a Text File|\n”);printf(“\t\t | 9. Write to a Text file |\n”);printf(“\t\t | 0. Quit |\n”);printf(“\t\t*****************************************\n”);printf(“\t\t\tGive your Choice(0-9):”);c=getchar(); /*讀入選擇*/}while(c’0’||c’9′);return(c-‘0′); /*返回選擇*/}int Input(Student stud[],int n) /*輸入若干條記錄*/{int i=0;char sign,x[10]; /*x[10]為清除多餘的數據所用*/while(sign!=’n’sign!=’N’) /*判斷*/{ printf(“\t\t\tstudent’s num:”); /*交互輸入*/scanf(“\t\t\t%s”,stud[n+i].num);printf(“\t\t\tstudent’s name:”);scanf(“\t\t\t%s”,stud[n+i].name);printf(“\t\t\tstudent’s score:”);scanf(“\t\t\t%d”,stud[n+i].score);gets(x); /*清除多餘的輸入*/printf(“\t\t\tany more records?(Y/N)”);scanf(“\t\t\t%c”,sign); /*輸入判斷*/i++;}return(n+i);}void Display(Student stud[],int n) /*顯示所有記錄*/{int i;printf(“\t\t\t———————————–\n”); /*格式頭*/printf(“\t\t\tnumber name score\n”);printf(“\t\t\t———————————–\n”);for(i=1;in+1;i++) /*循環輸入*/{printf(“\t\t\t%-16s%-15s%d\n”,stud[i-1].num,stud[i-1].name,stud[i-1].score);if(i1i%10==0) /*每十個暫停*/{printf(“\t\t\t———————————–\n”); /*格式*/printf(“\t\t\t”);system(“pause”);printf(“\t\t\t———————————–\n”);}}printf(“\t\t\t”);system(“pause”);}void Sort_by_num(Student stud[],int n) /*按學號排序*/{ int i,j,*p,*q,s;char t[10];for(i=0;in-1;i++) /*冒泡法排序*/for(j=0;jn-1-i;j++)if(strcmp(stud[j].num,stud[j+1].num)0){strcpy(t,stud[j+1].num);strcpy(stud[j+1].num,stud[j].num);strcpy(stud[j].num,t);strcpy(t,stud[j+1].name);strcpy(stud[j+1].name,stud[j].name);strcpy(stud[j].name,t);p=stud[j+1].score;q=stud[j].score;s=*p;*p=*q;*q=s;}}int Insert_a_record(Student stud[],int n) /*插入一條記錄*/{char x[10]; /*清除多餘輸入所用*/printf(“\t\t\tstudent’s num:”); /*交互式輸入*/scanf(“\t\t\t%s”,stud[n].num);printf(“\t\t\tstudent’s name:”);scanf(“\t\t\t%s”,stud[n].name);printf(“\t\t\tstudent’s score:”);scanf(“\t\t\t%d”,stud[n].score);gets(x);n++;Sort_by_num(stud,n); /*調用排序函數*/printf(“\t\t\tInsert Successed!\n”); /*返回成功信息*/return(n);}int Delete_a_record(Student stud[],int n) /*按姓名查找,刪除一條記錄*/{ char s[20];int i=0,j;printf(“\t\t\ttell me his(her) name:”); /*交互式問尋*/scanf(“%s”,s);while(strcmp(stud[i].name,s)!=0in) i++; /*查找判斷*/if(i==n){ printf(“\t\t\tnot find!\n”); /*返回失敗信息*/return(n);}for(j=i;jn-1;j++) /*刪除操作*/{strcpy(stud[j].num,stud[j+1].num);strcpy(stud[j].name,stud[j+1].name);stud[j].score=stud[j+1].score;}printf(“\t\t\tDelete Successed!\n”); /*返回成功信息*/return(n-1);}void Query_a_record(Student stud[],int n) /*查找並顯示一個記錄*/{ char s[20];int i=0;printf(“\t\t\tinput his(her) name:”); /*交互式輸入*/scanf(“\t\t\t%s”,s);while(strcmp(stud[i].name,s)!=0in) i++; /*查找判斷*/if(i==n){ printf(“\t\t\tnot find!\n”); /*輸入失敗信息*/return;}printf(“\t\t\this(her) number:%s\n”,stud[i].num); /*輸出該學生信息*/printf(“\t\t\this(her) score:%d\n”,stud[i].score);}void Statistic(Student stud[],int n) /*新增功能,輸出統計信息*/{ int i,j=0,k=0,sum=0;float aver; /*成績平均值*/for(i=0;in;i++) /*循環輸入判斷*/{sum+=stud[i].score;if(stud[j].scorestud[i].score) j=i;if(stud[k].scorestud[i].score) k=i;}aver=1.0*sum/n;printf(“\t\t\tthere are %d records.\n”,n); /*總共記錄數*/printf(“\t\t\tthe hignest score:\n”); /*最高分*/printf(“\t\t\tnumber:%s name:%s score:%d\n”,stud[j].num,stud[j].name,stud[j].score);printf(“\t\t\tthe lowest score:\n”); /*最低分*/printf(“\t\t\tnumber:%s name:%s score:%d\n”,stud[k].num,stud[k].name,stud[k].score);printf(“\t\t\tthe average score is %5.2f\n”,aver); /*平均分*/}int AddfromText(Student stud[],int n) /*從文件中讀入數據*/{ int i=0,num;FILE *fp; /*定義文件指針*/char filename[20]; /*定義文件名*/printf(“\t\t\tInput the filename:”);scanf(“\t\t\t%s”,filename); /*輸入文件名*/if((fp=fopen(filename,”rb”))==NULL) /*打開文件*/{ printf(“\t\t\tcann’t open the file\n”); /*打開失敗信息*/printf(“\t\t\t”);system(“pause”);return(n);}fscanf(fp,”%d”,num); /*讀入總記錄量*/while(inum) /*循環讀入數據*/{fscanf(fp,”%s%s%d”,stud[n+i].num,stud[n+i].name,stud[n+i].score);i++;}n+=num;fclose(fp); /*關閉文件*/printf(“\t\t\tSuccessed!\n”);printf(“\t\t\t”);system(“pause”);return(n);}void WritetoText(Student stud[],int n) /*將所有記錄寫入文件*/{int i=0;FILE *fp; /*定義文件指針*/char filename[20]; /*定義文件名*/printf(“\t\t\tWrite Records to a Text File\n”); /*輸入文件名*/printf(“\t\t\tInput the filename:”);scanf(“\t\t\t%s”,filename);if((fp=fopen(filename,”w”))==NULL) /*打開文件*/{printf(“\t\t\tcann’t open the file\n”);system(“pause”);return;}fprintf(fp,”%d\n”,n); /*循環寫入數據*/while(in){fprintf(fp,”%-16s%-15s%d\n”,stud[i].num,stud[i].name,stud[i].score);i++;}fclose(fp); /*關閉文件*/printf(“Successed!\n”); /*返回成功信息*/}void main() /*主函數*/{int n=0;for(;;){switch(menu_select()) /*選擇判斷*/{case 1:printf(“\t\t\tInput Records\n”); /*輸入若干條記錄*/n=Input(stu,n);break;case 2:printf(“\t\t\tDisplay All Records\n”); /*顯示所有記錄*/Display(stu,n);break;case 3:printf(“\t\t\tSort\n”);Sort_by_num(stu,n); /*按學號排序*/printf(“\t\t\tSort Suceessed!\n”);printf(“\t\t\t”);system(“pause”);break;case 4:printf(“\t\t\tInsert a Record\n”);n=Insert_a_record(stu,n); /*插入一條記錄*/printf(“\t\t\t”);system(“pause”);break;case 5:printf(“\t\t\tDelete a Record\n”);n=Delete_a_record(stu,n); /*按姓名查找,刪除一條記錄*/printf(“\t\t\t”);system(“pause”);break;case 6:printf(“\t\t\tQuery\n”);Query_a_record(stu,n); /*查找並顯示一個記錄*/printf(“\t\t\t”);system(“pause”);break;case 7:printf(“\t\t\tStatistic\n”);Statistic(stu,n); /*新增功能,輸出統計信息*/printf(“\t\t\t”);system(“pause”);break;case 8:printf(“\t\t\tAdd Records from a Text File\n”);n=AddfromText(stu,n); /*新增功能,輸出統計信息*/break;case 9:printf(“\t\t\tWrite to a Text file\n”);WritetoText(stu,n); /*循環寫入數據*/printf(“\t\t\t”);system(“pause”);break;case 0:printf(“\t\t\tHave a Good Luck,Bye-bye!\n”); /*結束程序*/printf(“\t\t\t”);system(“pause”);exit(0);}}}四、函數調用關係圖註:“→”代表調用Input函數打印鏈表記錄Display函數輸入若干條記錄menu_select()函數選擇菜單Sort_by_num函數顯示所有記錄Delete_a_record函數按姓名查找,刪除一條記錄Query_a_record查找並顯示一條記錄Statistic函數輸出統計信息 (新增)AddfromText函數從正文中添加數據到結構體數組中Main函數Insert_a_record插入一條記錄WritetoText函數 將所有數據寫入文件中退出程序Reverse(head)函數按學號排序五、設計測試流程1、進入界面2、輸入選項1,回車;按提示輸入數據:3、回到主菜單;輸入選項7,回車;輸入文件名:data.txt,回車;出現成功提示,則讀入文件操作成功。4、回到主菜單,輸入2,回車每10個暫停顯示數據5、回到主菜單,輸入3,回車出現排序成功信息。6、回到主菜單,輸入4,回車按提示插入一組數據7、回到主菜單,輸入5,回車按提示輸入姓名,刪除數據出現刪除成功的信息8、回到主菜單,輸入6,回車輸入姓名進行查詢9、回到主菜單,輸入7,回車出現統計信息10、回到主菜單,輸入9,回車輸入result.txt,回車出現成功寫入文件的信息11、回到主菜單,輸入0,回車退出系統
找人幫我編一個600行的代碼
給你一個,不過比你要求的少個一兩百行把
也很簡單的,你也有這能力完成這樣的程序
老師會相信你的,下面是程序代碼:
#include stdio.h
#include string.h
#define STU_NUM 3//學生數
#define SCORE_NUM 5//每個學生的科目數
typedef struct /*定義結構體數組*/
{
char num[20]; /*學號*/
int age; /*年齡*/
char name[20]; /*姓名*/
char sex[5]; /*性別*/
float score[SCORE_NUM]; /*成績*/
float total;//總分
float average;//平均分
} Student;
Student stu[STU_NUM];
//輸入學生信息
void input()
{
int i,j;
printf(“請輸入%d個學生的信息:\n”,STU_NUM);
for(i=0;iSTU_NUM;i++)
{
printf(“學號:”);
scanf(“%s”,stu[i].num);
printf(“姓名:”);
scanf(“%s”,stu[i].name);
printf(“年齡:”);
scanf(“%d”,stu[i].age);
printf(“性別:”,stu[i].sex);
scanf(“%s”,stu[i].sex);
for(j=0;jSCORE_NUM;j++)
{
printf(“科目%d的成績:”,j+1);
scanf(“%f”,stu[i].score[j]);
}
}
}
//輸出學生信息
void output(int i)
{
int j;
printf(“學生的信息如下:\n”);
printf(“學號:%s”,stu[i].num);
printf(“姓名:%s”,stu[i].name);
printf(“年齡:%d”,stu[i].age);
printf(“性別:%s\n”,stu[i].sex);
for(j=0;jSCORE_NUM;j++)
{
printf(“科目%d:%f\n”,j+1,stu[i].score[j]);
}
printf(“總分:%f\n”,stu[i].total);
printf(“平均分:%f\n”,stu[i].average);
}
//計算總分和平均分
void process()
{
int i,j;
for(i=0;iSTU_NUM;i++)
{
stu[i].total=0;
for(j=0;jSCORE_NUM;j++)
{
stu[i].total+=stu[i].score[j];
}
stu[i].average=stu[i].total/SCORE_NUM;
}
}
//排序並輸出
void sort()
{
Student tStu;
int i,j;
for(i=0;iSTU_NUM;i++)
{
for(j=STU_NUM-1;ji;j–)
{
if(stu[j].totalstu[j-1].total)
{
tStu=stu[j];
stu[j]=stu[i];
stu[i]=tStu;
}
}
}
for(i=0;iSTU_NUM;i++)
{
output(i);
}
}
//根據學號查找
void find(char *no)
{
int i;
for(i=0;iSTU_NUM;i++)
{
if(strcmp(stu[i].num,no)==0)
{
output(i);
return;
}
}
printf(“沒有找到!\n”);
}
void main()
{
input();
process();
sort();
find(“1”);
}
程序2:
/**********************
作者:***
日期:2007年7月9日
描述:學員成績管理。
**********************/
#include stdio.h
#define N 50
int NUM;
int x; //全局變量
struct student //定義並聲明結構變量
{
int number;
char name[20];
float score[3];
float ave;
}stu[N];
void suan(int );
void insert(int ); //錄入學員信息
void display(struct student *p,int ); //顯示信息
void paixu(struct student stu[],int ); //冒泡排序
void add(int ); //增加學員信息
void del(struct student *p,int ); //刪除學員信息
void tui(int ); //退出系統
void main()
{
int i=0;
printf(“\n\n=======================歡迎進行學員成績管理系統=======================\n\n\n”);
suan(x);
//增加
//調用函數
}
void suan(int i)
{
char x;
printf(” \n\n 1、輸入學員信息 2、增加學員信息 3、刪除學員信息 4、退出\n”);
printf(“\n請輸入選項:”);
scanf(“%d”,x);
switch (x)
{
case 1:
{
insert(i); //調用函數
break;
}
case 2:
{
add(NUM);
break;
}
case 3:
{
del(stu,NUM);
paixu(stu,NUM);
break;
}
case 4:
{
tui(NUM);
break;
}
default:
printf(“\n輸入有誤\n\n”);
}
}
void insert(int i)
{
int j;
float sum;
char ch;
do
{
sum=0;
printf(“\n請輸入學員信息:\n”);
printf(“\n學號:”);
scanf(“%d”,stu[i].number);
printf(“\n姓名:”);
scanf(“%s”,stu[i].name);
printf(“\n三門成績:\n”);
for (j=0;j3;j++)
{
printf(“\n成績%d:”,j+1);
scanf(“%f”,stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].ave=sum/3;
i++;
printf(“\n是否繼續輸入學員信息?(y/n)”);
fflush(stdin);
ch=getchar();
}while(ch==’y’ || ch==’Y’);
printf(“\n排序前學員信息:\n”);
display(stu,i);
printf(“\n排序後的學員信息如下:\n”);
paixu(stu,i);
NUM=i;
suan(x);
}
void display(struct student *p,int n)
{
int i;
printf(“\n學號\t姓名\t成績:\n”);
for (i=0;in;i++,p++)
{
printf(“%d\t%s\t%lf\n”,p-number,p-name,p-ave);
}
}
void paixu(struct student stu[],int n)
{
float temp;
int i,j;
for (i=0;in;i++)
{
for (j=0;jn-i-1;j++)
{
if (stu[j].avestu[j+1].ave)
{
temp=stu[j+1].ave;
stu[j+1].ave=stu[j].ave;
stu[j].ave=temp;
}
}
}
display(stu,n);
}
void add(int i)
{
char ch;
printf(“\n是否需要插入新學員信息?(y/n)”);
fflush(stdin);
ch=getchar();
if (ch==’y’ || ch==’Y’)
{
insert(i);
}
suan(x);
}
void del(struct student stu[],int n)
{
int num,i=0,j=0;
char ch;
printf(“\n是否需要刪除學員信息?(y/n)”);
fflush(stdin);
ch=getchar();
while (ch==’y’ ||ch==’Y’)
{
printf(“\n請輸入刪除學員的學號:”);
scanf(“%d”,num);
for (i=0;in;i++)
{
if (num==stu[i].number)
break;
}
if (in)
{
for (j=i;jn;j++)
{
stu[j]=stu[j+1];
}
n–;
printf(“\n刪除後的學員信息如下:\n”);
display(stu,n);
}
else
printf(“\n對不起,沒有這個學號!\n”);
if (n==0)
break;
printf(“\n是否要繼續刪除學員信息?(y/n)”);
fflush(stdin);
ch=getchar();
}
suan(x);
}
void tui(int i)
{
char ch;
printf(“\n退出?(y/n):”);
fflush(stdin);
ch=getchar();
if (ch==’y’ || ch==’Y’)
{
printf(“\n”);
}
else
{
suan(x);
}
}
求一個用C語言編寫的小遊戲代碼
#include graphics.h
#include conio.h
#include time.h
/////////////////////////////////////////////
// 定義常量、枚舉量、結構體、全局變量
/////////////////////////////////////////////
#define WIDTH 10 // 遊戲區寬度
#define HEIGHT 22 // 遊戲區高度
#define SIZE 20 // 每個遊戲區單位的實際像素
// 定義操作類型
enum CMD
{
CMD_ROTATE, // 方塊旋轉
CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方塊左、右、下移動
CMD_SINK, // 方塊沉底
CMD_QUIT // 退出遊戲
};
// 定義繪製方塊的方法
enum DRAW
{
SHOW, // 顯示方塊
HIDE, // 隱藏方塊
FIX // 固定方塊
};
// 定義七種俄羅斯方塊
struct BLOCK
{
WORD dir[4]; // 方塊的四個旋轉狀態
COLORREF color; // 方塊的顏色
} g_Blocks[7] = { {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, // I
{0x0660, 0x0660, 0x0660, 0x0660, BLUE}, // 口
{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, // L
{0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, // 反L
{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, // Z
{0x0360, 0x4620, 0x0360, 0x4620, GREEN}, // 反Z
{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T
// 定義當前方塊、下一個方塊的信息
struct BLOCKINFO
{
byte id; // 方塊 ID
char x, y; // 方塊在遊戲區中的坐標
byte dir:2; // 方向
} g_CurBlock, g_NextBlock;
// 定義遊戲區
BYTE g_World[WIDTH][HEIGHT] = {0};
/////////////////////////////////////////////
// 函數聲明
/////////////////////////////////////////////
void Init(); // 初始化遊戲
void Quit(); // 退出遊戲
void NewGame(); // 開始新遊戲
void GameOver(); // 結束遊戲
CMD GetCmd(); // 獲取控制命令
void DispatchCmd(CMD _cmd); // 分發控制命令
void NewBlock(); // 生成新的方塊
bool CheckBlock(BLOCKINFO _block); // 檢測指定方塊是否可以放下
void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 畫方塊
void OnRotate(); // 旋轉方塊
void OnLeft(); // 左移方塊
void OnRight(); // 右移方塊
void OnDown(); // 下移方塊
void OnSink(); // 沉底方塊
/////////////////////////////////////////////
// 函數定義
/////////////////////////////////////////////
// 主函數
void main()
{
Init();
CMD c;
while(true)
{
c = GetCmd();
DispatchCmd(c);
// 按退出時,顯示對話框諮詢用戶是否退出
if (c == CMD_QUIT)
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T(“您要退出遊戲嗎?”), _T(“提醒”), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
Quit();
}
}
}
// 初始化遊戲
void Init()
{
initgraph(640, 480);
srand((unsigned)time(NULL));
// 顯示操作說明
setfont(14, 0, _T(“宋體”));
outtextxy(20, 330, _T(“操作說明”));
outtextxy(20, 350, _T(“上:旋轉”));
outtextxy(20, 370, _T(“左:左移”));
outtextxy(20, 390, _T(“右:右移”));
outtextxy(20, 410, _T(“下:下移”));
outtextxy(20, 430, _T(“空格:沉底”));
outtextxy(20, 450, _T(“ESC:退出”));
// 設置坐標原點
setorigin(220, 20);
// 繪製遊戲區邊界
rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);
rectangle((WIDTH + 1) * SIZE – 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);
// 開始新遊戲
NewGame();
}
// 退出遊戲
void Quit()
{
closegraph();
exit(0);
}
// 開始新遊戲
void NewGame()
{
// 清空遊戲區
setfillstyle(BLACK);
bar(0, 0, WIDTH * SIZE – 1, HEIGHT * SIZE – 1);
ZeroMemory(g_World, WIDTH * HEIGHT);
// 生成下一個方塊
g_NextBlock.id = rand() % 7;
g_NextBlock.dir = rand() % 4;
g_NextBlock.x = WIDTH + 1;
g_NextBlock.y = HEIGHT – 1;
// 獲取新方塊
NewBlock();
}
// 結束遊戲
void GameOver()
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T(“遊戲結束。\n您想重新來一局嗎?”), _T(“遊戲結束”), MB_YESNO | MB_ICONQUESTION) == IDYES)
NewGame();
else
Quit();
}
// 獲取控制命令
DWORD m_oldtime;
CMD GetCmd()
{
// 獲取控制值
while(true)
{
// 如果超時,自動下落一格
DWORD newtime = GetTickCount();
if (newtime – m_oldtime = 500)
{
m_oldtime = newtime;
return CMD_DOWN;
}
// 如果有按鍵,返回按鍵對應的功能
if (kbhit())
{
switch(getch())
{
case ‘w’:
case ‘W’: return CMD_ROTATE;
case ‘a’:
case ‘A’: return CMD_LEFT;
case ‘d’:
case ‘D’: return CMD_RIGHT;
case ‘s’:
case ‘S’: return CMD_DOWN;
case 27: return CMD_QUIT;
case ‘ ‘: return CMD_SINK;
case 0:
case 0xE0:
switch(getch())
{
case 72: return CMD_ROTATE;
case 75: return CMD_LEFT;
case 77: return CMD_RIGHT;
case 80: return CMD_DOWN;
}
}
}
// 延時 (降低 CPU 佔用率)
Sleep(20);
}
}
// 分發控制命令
void DispatchCmd(CMD _cmd)
{
switch(_cmd)
{
case CMD_ROTATE: OnRotate(); break;
case CMD_LEFT: OnLeft(); break;
case CMD_RIGHT: OnRight(); break;
case CMD_DOWN: OnDown(); break;
case CMD_SINK: OnSink(); break;
case CMD_QUIT: break;
}
}
// 生成新的方塊
void NewBlock()
{
g_CurBlock.id = g_NextBlock.id, g_NextBlock.id = rand() % 7;
g_CurBlock.dir = g_NextBlock.dir, g_NextBlock.dir = rand() % 4;
g_CurBlock.x = (WIDTH – 4) / 2;
g_CurBlock.y = HEIGHT + 2;
// 下移新方塊直到有局部顯示
WORD c = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
while((c 0xF) == 0)
{
g_CurBlock.y–;
c = 4;
}
// 繪製新方塊
DrawBlock(g_CurBlock);
// 繪製下一個方塊
setfillstyle(BLACK);
bar((WIDTH + 1) * SIZE, 0, (WIDTH + 5) * SIZE – 1, 4 * SIZE – 1);
DrawBlock(g_NextBlock);
// 設置計時器,用於判斷自動下落
m_oldtime = GetTickCount();
}
// 畫方塊
void DrawBlock(BLOCKINFO _block, DRAW _draw)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;
int color = BLACK;
switch(_draw)
{
case SHOW: color = g_Blocks[_block.id].color; break;
case HIDE: color = BLACK; break;
case FIX: color = g_Blocks[_block.id].color / 3; break;
}
setfillstyle(color);
for(int i=0; i16; i++)
{
if (b 0x8000)
{
x = _block.x + i % 4;
y = _block.y – i / 4;
if (y HEIGHT)
{
if (_draw != HIDE)
bar3d(x * SIZE + 2, (HEIGHT – y – 1) * SIZE + 2, (x + 1) * SIZE – 4, (HEIGHT – y) * SIZE – 4, 3, true);
else
bar(x * SIZE, (HEIGHT – y – 1) * SIZE, (x + 1) * SIZE – 1, (HEIGHT – y) * SIZE – 1);
}
}
b = 1;
}
}
// 檢測指定方塊是否可以放下
bool CheckBlock(BLOCKINFO _block)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;
for(int i=0; i16; i++)
{
if (b 0x8000)
{
x = _block.x + i % 4;
y = _block.y – i / 4;
if ((x 0) || (x = WIDTH) || (y 0))
return false;
if ((y HEIGHT) (g_World[x][y]))
return false;
}
b = 1;
}
return true;
}
// 旋轉方塊
void OnRotate()
{
// 獲取可以旋轉的 x 偏移量
int dx;
BLOCKINFO tmp = g_CurBlock;
tmp.dir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; }
tmp.x = g_CurBlock.x – 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; }
tmp.x = g_CurBlock.x + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; }
tmp.x = g_CurBlock.x – 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; }
tmp.x = g_CurBlock.x + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; }
return;
rotate:
// 旋轉
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.dir++;
g_CurBlock.x += dx;
DrawBlock(g_CurBlock);
}
// 左移方塊
void OnLeft()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x–;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x–;
DrawBlock(g_CurBlock);
}
}
// 右移方塊
void OnRight()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x++;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x++;
DrawBlock(g_CurBlock);
}
}
// 下移方塊
void OnDown()
{
BLOCKINFO tmp = g_CurBlock;
tmp.y–;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.y–;
DrawBlock(g_CurBlock);
}
else
OnSink(); // 不可下移時,執行“沉底方塊”操作
}
// 沉底方塊
void OnSink()
{
int i, x, y;
// 連續下移方塊
DrawBlock(g_CurBlock, HIDE);
BLOCKINFO tmp = g_CurBlock;
tmp.y–;
while (CheckBlock(tmp))
{
g_CurBlock.y–;
tmp.y–;
}
DrawBlock(g_CurBlock, FIX);
// 固定方塊在遊戲區
WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
for(i = 0; i 16; i++)
{
if (b 0x8000)
{
if (g_CurBlock.y – i / 4 = HEIGHT)
{ // 如果方塊的固定位置超出高度,結束遊戲
GameOver();
return;
}
else
g_World[g_CurBlock.x + i % 4][g_CurBlock.y – i / 4] = 1;
}
b = 1;
}
// 檢查是否需要消掉行,並標記
int row[4] = {0};
bool bRow = false;
for(y = g_CurBlock.y; y = max(g_CurBlock.y – 3, 0); y–)
{
i = 0;
for(x = 0; x WIDTH; x++)
if (g_World[x][y] == 1)
i++;
if (i == WIDTH)
{
bRow = true;
row[g_CurBlock.y – y] = 1;
setfillstyle(WHITE, DIAGCROSS2_FILL);
bar(0, (HEIGHT – y – 1) * SIZE + SIZE / 2 – 2, WIDTH * SIZE – 1, (HEIGHT – y – 1) * SIZE + SIZE / 2 + 2);
}
}
if (bRow)
{
// 延時 200 毫秒
Sleep(200);
// 擦掉剛才標記的行
IMAGE img;
for(i = 0; i 4; i++)
{
if (row[i])
{
for(y = g_CurBlock.y – i + 1; y HEIGHT; y++)
for(x = 0; x WIDTH; x++)
{
g_World[x][y – 1] = g_World[x][y];
g_World[x][y] = 0;
}
getimage(img, 0, 0, WIDTH * SIZE, (HEIGHT – (g_CurBlock.y – i + 1)) * SIZE);
putimage(0, SIZE, img);
}
}
}
// 產生新方塊
NewBlock();
}
求一個c語言製作的小遊戲或者小軟件,行數不小於600,挺急的
貪吃蛇代碼
#include stdio.h
#include graphics.h
#include stdlib.h
#include dos.h /*引用的庫函數*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b/*宏定義鍵名*/
#define N 200
int i,key;
int level;/*遊戲等級*/
int score=0;/*得分*/
int gamespeed;/*遊戲速度*/
struct Food
{
int x;/*食物的橫坐標*/
int y;/*食物的縱坐標*/
int yes;/*判斷是否要出現食物的變量*/
}food;/*食物的結構體*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的節數*/
int direction;/*蛇移動方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;/*蛇的結構體*/
void Choicelevle(void);/*選擇遊戲等級*/
void Init(void);/*圖形驅動*/
void Close(void);/*圖形結束*/
void DRAW(void);/*遊戲區域*/
void GameOver(void);/*結束遊戲*/
void GamePlay(void);/*玩遊戲具體過程*/
void PrScore(void);/*輸出成績*/
/*主函數*/
void main(void)
{
Init();/*圖形驅動*/
Choicelevle();/*選擇遊戲等級*/
DRAW();/*遊戲區域*/
GamePlay();/*玩遊戲具體過程*/
Close();/*圖形結束*/
}
/*圖形驅動*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(gd,gm,”\\turboc2″); /*初始化圖形系統*/
cleardevice(); /*清除圖形界面*/
}
/*選擇遊戲等級*/
void Choicelevle(void)
{char name[20];
setcolor(YELLOW);
settextstyle(0,0,6);
outtextxy(150,150,”Snake”);
setcolor(GREEN);
settextstyle(0,0,1);
outtextxy(200,250,”please put in your English name:”);
outtextxy(200,270,”Choice levle from 1-9.”);
outtextxy(300,320,”name:yangzilong”);/*製作人姓名*/
outtextxy(300,350,”number:0902060226″);/*製作人學號*/
outtextxy(300,380,”class:computer science 0602″);/*製作人班級*/
getch();
printf(“please putin your name:”);
gets(name);
printf(“please choice levle:”);
scanf(“%d”,level);
gamespeed=100000-400*level-300*level*level;
if(level9||level1)
{cleardevice(); /*清除圖形界面*/
setcolor(YELLOW); /*設置字體顏色*/
settextstyle(0,0,2); /*設置字體類型*/
outtextxy(150,200,”level input error”); /*顯示文本*/
getch();
level=1;
}
}
void DRAW(void)
{cleardevice(); /*清屏*/
setcolor(2);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設置線型*/
rectangle(45,45,465,325);
}
/*玩遊戲具體過程*/
void GamePlay(void)
{setcolor(5);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設置線型*/
randomize();/*隨機數發生器*/
food.yes=1;/*1表示需要出現新食物,0表示已經存在食物*/
snake.life=0;/*活着*/
snake.direction=1;/*方向往右*/
snake.x[0]=320;snake.y[0]=240;/*蛇頭*/
snake.x[1]=330;snake.y[1]=240; /*蛇的第二節位置*/
snake.node=3;/*節數*/
PrScore();/*輸出得分*/
while(1)/*可以重複玩遊戲,壓ESC鍵結束*/
{
while(!kbhit())/*在沒有按鍵的情況下,蛇自己移動身體*/
{
if(food.yes==1)/*需要出現新食物*/
{
food.x=rand()%360+70;
food.y=rand()%250+60;
while(food.x%10!=0)/*食物隨機出現後必須讓食物能夠在整格內,這樣才可以讓蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*畫面上有食物了*/
}
if(food.yes==0)/*畫面上有食物了就要顯示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i0;i–)/*蛇的每個環節往前移動*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個方向,通過這個控制來移動蛇頭*/
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;isnake.node;i++)/*從蛇的第四節開始判斷是否撞到自己了,因為蛇頭為兩節,第三節不可能拐過來*/
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
GameOver();/*顯示失敗*/
snake.life=1; /*蛇死*/
break;
}
}
/*如果蛇頭碰到牆壁,蛇頭從對面牆出來*/
if(snake.x[0]50)
{snake.x[0]=450;/*如果蛇頭越過左邊界,則從右邊界進入*/
snake.y[0]=snake.y[0];/*縱坐標不變*/
for(i=snake.node-1;i0;i–)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節數向前推進*/
}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的範圍的函數*/
}
}
else
if(snake.x[0]450)
{snake.x[0]=50;/*如果蛇頭越過右邊界,則蛇頭從左邊界進入*/
snake.y[0]=snake.y[0];/*縱坐標不變*/
for(i=snake.node-1;i0;i–)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節數向前推進*/
}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的範圍的函數*/
}
}
else
if(snake.y[0]60)
{snake.y[0]=320;/*如果蛇頭越過上邊界,則從下邊界進入*/
snake.x[0]=snake.x[0];/*橫坐標不變*/
for(i=snake.node-1;i0;i–)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節數向前推進*/
}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的範圍的函數*/
}
}
else
if(snake.y[0]320)
{snake.y[0]=60;/*如果蛇頭越過下邊界,則從上邊界進入*/
snake.x[0]=snake.x[0];/*橫坐標不變*/
for(i=snake.node-1;i0;i–)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節數向前推進*/
}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的範圍的函數*/
}
}
if(snake.life==1)/*如果蛇死就跳出內循環,重新開始*/
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以後*/
{
setcolor(0);/*把畫面上的食物東西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10); /*用當前線型和顏色畫一矩形*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一節先放在看不見的位置,下次循環就取前一節的位置*/
snake.node++;/*蛇的身體長一節*/
food.yes=1;/*畫面上需要出現新的食物*/
score+=10; /*每吃掉一食物,得分累加10分*/
if(score%100==0)
{level++;gamespeed=100000-400*level-300*level*level;/*每吃掉10食物提升一級,速度加快*/
PrScore();/*輸出新得分*/
setcolor(YELLOW); /*設置字體顏色*/
settextstyle(0,0,4); /*設置字體類型*/
outtextxy(150,200,”LEVEL UP”); /*顯示文本*/
if(level==10)
{level=1,gamespeed=100000-400*level-300*level*level;}
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
bar(50,55,455,315);/*bar是表示填充的範圍的函數*/
}
PrScore();/*輸出新得分*/
}
setcolor(4);/*畫出蛇*/
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed); /*控制遊戲速度*/
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/ /*用黑色去除蛇的的最後一節*/
if(snake.life==1)/*如果蛇死就跳出循環*/
break;
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*按ESC鍵退出*/
break;
else
if(key==UPsnake.direction!=4)
/*判斷是否往相反的方向移動*/
snake.direction=3;
else
if(key==RIGHTsnake.direction!=2)
snake.direction=1;
else
if(key==LEFTsnake.direction!=1)
snake.direction=2;
else
if(key==DOWNsnake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*遊戲結束*/
void GameOver(void)
{
cleardevice(); /*清屏*/
PrScore();
setcolor(RED); /*設置字體顏色*/
settextstyle(0,0,4); /*設置字體類型*/
outtextxy(200,200,”GAME OVER”); /*顯示文本*/
getch();
}
/*輸出成績及遊戲等級*/
void PrScore(void)
{
char str1[20];/*設置字符型數組*/
setfillstyle(SOLID_FILL,0);
bar(50,15,390,35); /*填充矩形框*/
setcolor(6); /*設置文本顏色*/
settextstyle(0,0,2); /*設置數組顯示位置*/
sprintf(str1,”score %d level %d”,score,level);/*顯示數組內容*/
outtextxy(55,20,str1);
setcolor(YELLOW); /*設置字體顏色*/
settextstyle(0,0,2); /*設置字體類型*/
outtextxy(250,400,”EXIT=ESC “);/*顯示文本*/
}
void Close(void)
{
closegraph();
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/284649.html