當前位置:首頁 » 編程語言 » c語言設計項目

c語言設計項目

發布時間: 2023-08-31 02:01:01

c語言課程設計

//我以前的作業

游戲說明
1.開始游戲
1)系統隨機產生一位不重復的N位數字。N的大小與等級有關。
初級:N=3 中級:N=4 高級:N=5
2)游戲者輸入所猜的不重復數字,並按回車提交。提交的數據
數應與N的大小一致,當數據位數大於N時,只取前N位。
3)系統將提交的數與它自動產生的數進行比較,輸出結果「*A*B」形式。其中A代表數字正確相應位置也正確,B代表數字正確但位置不正確。如:1A3B表示游戲者有1個數字的位置正確且數值也正確,除此以外,游戲者還猜對了3個數字,但位置不對,即游戲者已經猜出4位數據,但有3個數據位置不對!
4)游戲者有限定次數的猜測機會,在規定次數內完成,則游戲成功,否則,游戲失敗。其中,猜測次數與等級有關:
初級:10次 中級:9次 高級:8次。
5)按完成游戲所用的時間和次數計算游戲者所得分數,游戲等級越高,猜測所用的次數越少,得分越高!若游戲者得分比系統已經保存的分數的高,將提示要求輸入游戲者信息,並且保存在最佳排行之中。
2.等級設置
6)游戲者可以自行設置游戲等級:初級,中級和高級。
3.最佳排行
在最佳排行中,游戲者可以查看當前游戲的最佳排行。
4.游戲說明
在游戲說明中,游戲者可以查看游戲規則。

三.總體設計
本課程設計對模塊設計的要求如下:
(1)要求使用多文件方式實現設計;
(2)要求在各個文件內實現結構化設計;
(3)每個模塊作為一個單獨的C文件,每個文件內的函數如表所示,表中給出了各個函數的功能說明。
(4)宏和數據結構等放在頭文件中,並使用條件編譯
(1)文件及函數組成
源文件 函數名或其他成分 功能
record.h
ASK 宏定義
結構聲明 結構聲明
庫函數及函數原型聲明
game_ control.c Game_rank 設置游戲等級
Game_explain 輸出遊戲說明
game_ register.c Tiptop_record 記錄最高分
Tiptop_output 輸出最佳排行
game.c Game_main 開始游戲中心
Build_number 獲取系統隨機數字
Game_in_out 獲取游戲者數據和輸出結果
Time_command 時間控制
Game_result 游戲結果處理輸出
record.c main 主函數
Menu_select 選擇菜單
manage_menu 處理菜單

(2)函數設計
1.頭部文件
文件名稱:record.h
功 能:聲明函數原型,包含文件及自定義宏和數據結構。
要 求:報告是否能正常打開文件執行操作。
2. 設置游戲等級函數
函數原型:void Game_rank(void)
功 能:可由游戲者自行設置游戲等級
參 數:void
返 回 值:void
要 求:如果游戲者沒有進行等級設定,則默認初級進行游戲。
3. 輸出遊戲說明函數
函數原型:void Game_ explain()
功 能:輸出遊戲規則說明
參 數:void
返 回 值:void
要 求:說明游戲相應的按鍵,保證游戲者按照說明。
4. 記錄最高分函數:
函數原型:void Tiptop_register(int)
功 能:把從Get_player函數得到的游戲者信息和游戲的積分信息一同保存到指定路徑的Tiptop_register.dat文件中
參 數:int S;S為游戲積分
返 回 值:void
要 求:要求把Tiptop1.txt(初級最高分) Tiptop2.txt(中級最高分) Tiptop3.txt(高
級最高分)原來有的信息替換為現在的信息,並且只有積分大於原有信息的第
十名,才會被以替換的方式記錄;並且是降序的方式排列的。
5. 輸出最佳排行函數:
函數原型:void Tiptop_out()
功 能:輸出最佳排行信息
參 數:void
返 回 值:void
要 求:沒有信息的時候輸出提示,有信息時按降序排列輸出全部信息。
6. 開始游戲中心函數
函數原型:void Game_main()
功 能:調用Time_ control 、Build_number等函數,進行游戲的整體控制
參 數:void
返 回 值:void
要 求:注意函數的完整性,保證游戲順利進行,注意沒有設置等級時候,默認C=1!
7. 獲取系統隨機數字函數
函數原型:int *Build_number()
功 能:系統調用<time.h>文件,利用srand((unsigned) time(&)產生1個N位隨機數字,供游戲者猜測
參 數:void
返 回 值:int 8=*i:指向產生的隨機數字構成的數組的指針
要 求:產生的數字是隨機的,並且是不重復的,總共產生N(依據游戲等級而定)位數字,組成數組[初級為3個數字]。
8. 獲取游戲者游戲數據和輸出遊戲進程結果函數
函數原型:void Course_in_out()
功 能:把游戲者猜的數據讀入內存,並且分析得出結果,並輸出結果
參 數:void
返 回 值:void
要 求:用A或B的代碼表示本次的結果,其中A表示數值對位置也對,B表示數值對,但位置錯了!每次得到游戲者的猜測數據都要分析得出結果並輸出結果,以便游戲者的下一次猜測。
9. 時間控制函數
函數原型:int Time_ control()
功 能:利用<dos.h>和<time.h>文件,得到玩游戲的總時間,計算游戲積分。
參 數:void
返 回 值:int Time:游戲前後的時間差異
要 求:得到的時間是以秒為單位,數據是整型數據。
10. 游戲結果處理輸出函數
函數原型:void Game_result(int m )
功 能:計算,得出最後游戲積分
參 數:int m:整個游戲的時間
返 回 值:void
要 求:按照游戲等級,設置不同的游戲積分方式。
11. 主函數:
函數原型:void main()
功 能:調用各個子函數,實現整體程序的運行
參 數:void
返 回 值: void
要 求:控制整個游戲的順利進行。
12. 選擇菜單函數:
函數原型:void Menu_select()
功 能:得到Handle_menu的返回值,由一個switch語句選擇開始,等級,最佳排行,游戲說明,結束等菜單操作。
參 數:void
返 回 值:void
要 求:由Handle_menu得到數據可循環得到,直到得到數字5時,選擇退出菜單,方可退出遊戲,否則一直循環程序!
13. 處理菜單函數:
函數原型:int Handle_menu()
功 能:輸出菜單選擇提示,並由cin語句實現游戲者的輸入,輸入選擇相應菜單對應的菜單序號,並把相應的值傳給Menu_select函數
參 數:void
返 回 值:int n:選擇相應菜單的菜單序號
要 求:輸入的值不是相應的菜單的序號時返回0值,否則返回遊戲者的輸入值,以選擇相應菜單。

四.程序代碼
1. record.h
#include"time.h"
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//結構體
#define MAXSIZE 20
typedef long int ElemType; //定義結構體tiptop,存放游戲者的最高分等信息
typedef struct tiptop
{
ElemType data;
char name[MAXSIZE];
char grade[MAXSIZE];
int year;
int month;
int day;
char week[MAXSIZE];
int hour;
int minute;
int second;
}tiptop;
//全局變數
int C; //C為等級設置變數
//函數聲明
void Menu_select(); //選擇菜單函數
int manage_menu(); //處理菜單函數
void Tiptop_record(int); //記錄最高分函數
void Tiptop_output(); //輸出最佳排行函數
void Game_rank(); //設置游戲等級函數
void Game_explain(); //輸出遊戲說明函數
void Game_main(); //開始游戲中心函數
int *Build_number(); //獲取系統隨機數字函數
void Game_in_out(); //獲取游戲數據和輸出遊戲進程結果函數
int Time_command(); //時間控制函數
void Game_result(int); //游戲結果處理輸出函數

2. game_ control.c
#include"record.h"
//**********************
// 設置游戲等級函數
//**********************
void Game_rank()
{
FILE *fp;
printf("\t******************************\n");
printf("\t************1:初級************\n");
printf("\t************2:中級************\n");
printf("\t************3:高級************\n");
printf("\t******************************\n");
printf("\t請選擇游戲等級:\t");
loop:
scanf("%d",&C);
if(C<1||C>3)
{
printf("\t輸入有誤!請重新輸入:\t");
goto loop;
}
if((fp=fopen("C.txt","w"))==NULL)
{
printf("打開C.txt文件失敗!");
exit(1);
}
fwrite(&C,sizeof(int),1,fp);
fclose(fp);
if(C==1)
printf("\t等級設置為初級!\n");
if(C==2)
printf("\t等級設置為中級!\n");
if(C==3)
printf("\t等級設置為高級!\n");
printf("\n");

}
//****************
//輸出遊戲說明函數
//****************
void Game_explain()
{
printf("游戲說明:\n");
printf("\t**********************************************************\n");
printf("\t**********************************************************\n");
printf("\t①:游戲開始,系統隨機產生一位不重復的N位數字。N的大小與等級\n");
printf("\t有關:\n");
printf("\t初級:N=3\t中級:N=4\t高級:N=5\t\n");
printf("\t②:游戲者輸入所猜的不重復數字,並按回車提交。\n");
printf("\t提交的數據位數應與N的大小一致,當數據位數大於N時,只取前N位;\n");
printf("\t③:系統將提交的數與它自動產生的數進行比較,輸出結果\n");
printf("\t「*A*B」形式。其中A代表數字正確相應位置也正確,B代\n");
printf("\t表數字正確但位置不正確。如:1A3B表示游戲者有1個數字的位置\n");
printf("\t正確且數值也正確,除此以外,游戲者還猜對了3個數字,但位置不\n");
printf("\t對,即游戲者已經猜出4位數據,但有3個數據位置不對!\n");
printf("\t④:游戲者有限定次數的猜測機會,在規定次數內完成,\n");
printf("\t則游戲成功。否則,游戲失敗。其中,猜測次\n");
printf("\t數與等級有關:\n");
printf("\t初級:10次\t中級:9次\t高級:8次。\t\n");
printf("\t⑤:按完成游戲所用的時間和次數計算游戲者所得分數,\n");
printf("\t游戲等級越高,猜測所用的次數越少,得分越高!\n");
printf("\t若游戲者得分比系統已經保存的分數的高,將提示要求\n");
printf("\t輸入游戲者信息,並且保存在最佳排行之中。\n");
printf("\t⑥:游戲者可以自行設置等級!\n");
printf("\t**********************************************************\n");
printf("\t**********************************************************\n");
printf("\n");
}
3. game_ register.c
#include"record.h"
//**************
//記錄最高分函數
//**************
void Tiptop_record(int S)
{
FILE *fp;
tiptop n;
char *week[]={"星期天","星期一","星期二","星期三","星期四","星期五",
"星期六"};
time_t timep;
struct tm *t;
printf("\t恭喜您!\n");
printf("\t您獲得本游戲的最高分!\n");
//獲取名字
printf("\t請輸入您的姓名:");
printf("\t");
scanf("%s",n.name);
//獲取時間
time(&timep);
t=gmtime(&timep);
t->tm_year=1900+t->tm_year;
t->tm_mon=1+t->tm_mon;
t->tm_hour=8+t->tm_hour;
n.year=t->tm_year;
n.month=t->tm_mon;
n.day=t->tm_mday;
strcpy(n.week,week[t->tm_wday]);
n.hour=t->tm_hour;
n.minute=t->tm_min;
n.second=t->tm_sec;
//獲取等級
if(C==1)
strcpy(n.grade,"初級");
if(C==2)
strcpy(n.grade,"中級");
if(C==3)
strcpy(n.grade,"高級");
//獲取分數
n.data=S;
if(C==1)
{
if((fp=fopen("tiptop1.txt","w"))==NULL)
{
printf("\t打開tiptop1.txt文件失敗!");
exit(1);
}
fwrite(&n,sizeof(struct tiptop),1,fp);
fclose(fp);
}
if(C==2)
{
if((fp=fopen("tiptop2.txt","w"))==NULL)
{
printf("\t打開tiptop2.txt文件失敗!");
exit(1);
}
fwrite(&n,sizeof(struct tiptop),1,fp);
fclose(fp);
}
if(C==3)
{
if((fp=fopen("tiptop3.txt","w"))==NULL)
{
printf("\t打開tiptop3.txt文件失敗!");
exit(1);
}
fwrite(&n,sizeof(struct tiptop),1,fp);
fclose(fp);
}
}
//****************
//輸出最佳排行函數
//****************
void Tiptop_output()
{
int i;
FILE *fp;
tiptop p,q,r;
if((fp=fopen("tiptop1.txt","r"))==NULL)
{
printf("還沒有最佳排行!");
exit(1);
}
fread(&p,sizeof(struct tiptop),1,fp);
fclose(fp);
if((fp=fopen("tiptop2.txt","r"))==NULL)
{
printf("還沒有最佳排行!");
exit(1);
}
fread(&q,sizeof(struct tiptop),1,fp);
fclose(fp);
if((fp=fopen("tiptop3.txt","r"))==NULL)
{
printf("還沒有最佳排行!");
exit(1);
}
fread(&r,sizeof(struct tiptop),1,fp);
fclose(fp);
if(p.data==0&&q.data==0&&r.data==0)
{
printf("\t暫無最高分信息!");
printf("\n");
printf("\t是否直接進入游戲?刷新最佳排行信息?\n");
printf("\t1:是\t2:否\n");
printf("\t");
i=0;
scanf("%d",&i);
if(i==1)
{
printf("\t請設置游戲等級: \n");
Game_rank();
Game_main();
}
else
Menu_select();
}
else
{
printf("\t最佳排行:\n");
printf("\t等級\t姓名\t分數\t游戲時間\n");
if(p.data!=0)
{
printf("\t%s\t%s\t%d\t",p.grade,p.name,p.data);
printf("%d—%d—%d\t",p.year,p.month,p.day);
printf("%s\t%d:%d:%d\n",p.week,p.hour,p.minute,p.second);
}
if(q.data!=0)
{
printf("\t%s\t%s\t%d\t",q.grade,q.name,q.data);
printf("%d—%d—%d\t",q.year,q.month,q.day);
printf("%s\t%d:%d:%d\n",q.week,q.hour,q.minute,q.second);
}
if(r.data!=0)
{
printf("\t%s\t%s\t%d\t",r.grade,r.name,r.data);
printf("%d—%d—%d\t",r.year,r.month,r.day);
printf("%s\t%d:%d:%d\n",r.week,r.hour,r.minute,r.second);
}
if(p.data==0)
printf("\t初級暫無最高分信息!\n");
if(q.data==0)
printf("\t中級暫無最高分信息!\n");
if(r.data==0)
printf("\t高級暫無最高分信息!\n");

//重置最佳排行榜
if(p.data!=0&&q.data!=0&&r.data!=0)
{
printf("\t是否重置最佳排行?\n");
printf("\t1:是\t2:否\t");
i=0;
scanf("%d",&i);
if(i==1)
{
p.data=0;
q.data=0;
r.data=0;
if((fp=fopen("tiptop1.txt","w"))==NULL)
{
printf("打開tiptop1.txt文件失敗!");
exit(1);
}
fwrite(&p,sizeof(struct tiptop),1,fp);
fclose(fp);
if((fp=fopen("tiptop2.txt","w"))==NULL)
{
printf("打開tiptop2.txt文件失敗!");
exit(1);
}
fwrite(&q,sizeof(struct tiptop),1,fp);
fclose(fp);
if((fp=fopen("tiptop3.txt","w"))==NULL)
{
printf("打開tiptop3.txt文件失敗!");
exit(1);
}
fwrite(&r,sizeof(struct tiptop),1,fp);
fclose(fp);
}
}
}
}

4.game.c
#include"record.h"

//全局變數
int *c1; //產生的數保存在指針c中
int A,B; // AB均為游戲重要參數
int N; //NUM為需要猜測的數據個數
int TIME; //TIME為猜數字共有的次數
long int S; //S為游戲積分
//****************
//開始游戲中心函數
//****************
void Game_main()
{
FILE *fp;
int time;
if((fp=fopen("C.txt","r"))==NULL)
{
printf("打開C.txt文件失敗!");
printf("\n");
exit(1);
}
fread(&C,sizeof(int),1,fp);
N=C+2;
TIME=11-C;
if(C==1)
{
printf("\t游戲等級為:初級!\n");
}
if(C==2)
{
printf("\t游戲等級為:中級!\n");
}
if(C==3)
{
printf("\t游戲等級為:高級!\n");
}
printf("\t需要猜測的數字位數數為:%d,共有%d次猜測機會!",N,TIME);
printf("\n");
time=Time_command();
Game_result(time);
}
//********************
//獲取系統隨機數字函數
//********************
int *Build_number()
{
int i,j,m;
time_t t;
c1=(int *)malloc(N*sizeof(int));
if(c1==NULL)
{
printf("分配內存錯誤!\n");
printf("\n");
exit(1);
}
srand((unsigned) time(&t));
for(i=0;i<N;i++)
{
m=rand()%10; //產生一個數字
if(i==0)
*c1=m;
else
{
for(j=0;j<i;j++)
{
if(m==*(c1+j)//若原有數據之中已經有了與m相同的數字,則重新產生數字
{
i--;
break;
}
*(c1+i)=m;
}
}
}
printf("\t系統產生的數據為:");
for(i=0;i<N;i++)
printf("*");
printf("\n");
printf("\t您可以開始游戲! 計時開始...\n");
printf("\t不能有重復的數字!\n");
printf("\t數字中間用空格隔開\n");
return c1;
}
//****************************************
//獲取游戲者游戲數據和輸出遊戲進程結果函數
//****************************************
void Game_in_out()
{
int i,j,m;
int n; //游戲者猜測的數字
int a[5]; //游戲者猜測的數據構成的數組
int c[5];
int *p;
p=Build_number();
for(i=0;i<N;i++)
{
c[i]=*(p+i);
}
for(m=0;m<TIME;m++)
{
if(m==(TIME-1))
{
printf("\t**請注意:**\n");
printf("\t**僅剩下一次機會了!**\n");
printf("\t**努力!加油!**\n");
}
printf("\t請輸入第%d次猜測的數字:\t",m+1);
for(j=0;j<N;j++)
{
scanf("%d",&n);
a[j]=n;
}
A=0;
B=0;
for(i=0;i<N;i++)
{
if(a[i]==c[i])
A++;
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(a[i]==c[j]&&i!=j)
B++;
}
}
printf("\t您本次猜測的結果為:\t");
printf("%d A %d B",A,B);
printf("\n");
//游戲成功控制退出猜測程序
if(A==N)
{
m=TIME;
}
}

//TIME改變,記錄游戲總次數的數據
TIME=i;
free(c1);
}
//************
//時間控制函數
//************
int Time_command()
{
int a; //游戲時間
time_t first, second;
first=time(NULL); //獲取系統時間
Game_in_out();
second=time(NULL); //再次獲取系統時間
a=(int)difftime(second,first);
return a;
}
//********************
//游戲結果處理輸出函數
//********************
void Game_result(int m)
{
FILE *fp;
tiptop p;
time_t t;
int S;
int n;
int i=0;
int minute,second;
if(A!=N)
{
S=0;
printf("\tGame over!\n");
printf("\t您本次游戲的積分為:\t%d",S);
printf("\t繼續努力!\n");
printf("\t是否繼續游戲?\n");
printf("\t1:是\t\t\t2:否");
printf("\n");
scanf("%d",&i);
if(i==1)
Game_main();
else
Menu_select();
}
else
{
printf("\t恭喜您游戲成功!");
printf("\n");
minute=m/60;
second=m%60;
printf("\t您本次游戲所用時間為:");
printf("%d分鍾%d秒\n",minute,second);
printf("\n");

//積分
if(C==1)
{
n=11-TIME;
S=n*n*n*10-m;
}
if(C==2)
{
n=10-TIME;
S=n*n*n*n*10-m;
}
if(C==3)
{
n=9-TIME;
S=n*n*n*n*n*10-m;
}
if(S<10)
{
srand((unsigned) time(&t));
S=rand()%100;
}
printf("\t您本次游戲的積分為:\t%d",S);
printf("\n");
}
//判斷是否符合最高分條件
if(C==1)
{
if((fp=fopen("tiptop1.txt","r"))==NULL)
{
printf("打開tiptop1.txt文件失敗!");
printf("\n");
exit(1);
}
fread(&p,sizeof(struct tiptop),1,fp);
fclose(fp);
}
if(C==2)
{
if((fp=fopen("tiptop2.txt","r"))==NULL)
{
printf("打開tiptop2.txt文件失敗!");
printf("\n");
exit(1);
}
fread(&p,sizeof(struct tiptop),1,fp);
fclose(fp);
}
if(C==3)
{
if((fp=fopen("tiptop3.txt","r"))==NULL)
{
printf("打開tiptop3.txt文件失敗!");
printf("\n");
exit(1);
}
fread(&p,sizeof(struct tiptop),1,fp);
fclose(fp);
}
if(S>=(p.data))
Tiptop_record(S);
//提示跳轉
if(C==1)
{
printf("\t您本次游戲等級是初級,是否要進入中級?\n");
printf("\t1:是\t\t\t2:否");
printf("\n\t");
i=0;
scanf("%d",&i);
if(i==1)
{
C=2;
if((fp=fopen("C.txt","w"))==NULL)
{
printf("打開C.txt文件失敗!");
exit(1);
}
fwrite(&C,sizeof(int),1,fp);
fclose(fp);
Game_main();
}
else
Menu_select();
}
if(C==2)
{
printf("\t您本次是中級游戲,是否要進入高級游戲呢?\n");
printf("\t1:是\t\t\t2:否");
printf("\n\t");
i=0;
scanf("%d",&i);
if(i==1)
{
C=3;
if((fp=fopen("C.txt","w"))==NULL)
{
printf("打開C.txt文件失敗!");
exit(1);
}
fwrite(&C,sizeof(int),1,fp);
fclose(fp);
Game_main();
}
else
Menu_select();
}
}

5.record.c
#include"record.h"
//******
//主函數
//******
void main()
{
printf("\n");
printf("********************************************\n");
printf("********************************************\n");
printf(" 歡迎進入猜數字游戲 \n");
printf("\n");
printf("\t設計者: \n");
printf("********************************************\n");
printf("********************************************\n");
printf("\n");
Menu_select();
}
//************
//選擇菜單函數
//************
void Menu_select()
{
while(1)
{
switch(manage_menu())
{
case 0:
printf("輸入有誤,請重新輸入!\n");
break;
case 1:
Game_main();
break;
case 2:
Game_rank();
break;
case 3:
Tiptop_output();
break;
case 4:
Game_explain();
break;
case 5:
printf("\n");
printf("\n");
printf("*************** ** ** **** ** ** ** ** *** \n");
printf("*************** ** ** ** ** **** ** ** ** ***** \n");
printf(" ** ** ** ** ** ** ** ** ** ** ******* \n");
printf(" ** ** ** ** ** ** ** ** ** ** ******* \n");
printf(" ** *********** ************* ** ** ** **** ***** \n");
printf(" ** *********** ************* ** ** ** **** ***** \n");
printf(" ** ** ** ** ** ** ** ** ** ** ***** \n");
printf(" ** ** ** ** ** ** ** ** ** ** *** \n");
printf(" ** ** ** ** ** ** **** ** ** *** \n");
printf(" ** ** ** ** ** ** ** ** ** *** \n");
printf("\n");
printf("Goodbye!\n");
exit(1);
}
}
}
//************
//處理菜單函數
//************
int manage_menu()
{
int n;
printf("\t**************************\n");
printf("\t**\t1:開始游戲\t**\n");
printf("\t**\t2:等級設置\t**\n");
printf("\t**\t3:最佳排行\t**\n");
printf("\t**\t4:游戲說明\t**\n");
printf("\t**\t5:退出遊戲\t**\n");
printf("\t**************************\n");
printf("\t請輸入相應序號選擇相應菜單:");
printf("\t");
scanf("%d",&n);
printf("\n");
if(n<1||n>5)
n=0;
return n;
}

㈡ 《C語言程序設計》項目設計

這種東西很多了,隨便搜索一大堆:這是個學生成績的!
#include <iostream.h>
#include <iomanip.h>
#include <fstream>
#include <vector>
#include <malloc.h>
#include <stdlib.h>
#include <string>
#include <process.h>
#include <stdio.h>
//#define NULL 0
int const Q=20;
#define LEN sizeof(struct student)
using namespace std;
int n=0; //定義一個全局變數統計學生人數
//——--------->定義一個學生考試信息的結構體
struct student
{
char name[Q]; //用來存放姓名的
char sex[Q]; //用來存放性別的
long int id; //用來存放准考證號的
int score[4]; //用來存放分數的
int total; //用來存放總分數的
struct student *next;
};
//student向量容器
vector <student> stu;
//-------------->學生類
class Information
{
public:
Information() ; //構造函數.
~Information() ; //析構函數.
student *creat();//建立鏈表函數。
void output(student *head);
int count(student *head);//定義函數count()統計考生總數
student *insert(student*head);//指針函數*insert()用來添加考生信息.
student *cancel(student *head,long int num);//指針函數*cancel()用來刪除考生信息.
student *find(student *head,long int num); //指針函數*find()用來查找考生信息.
void inorder(student *head);//定義inorder()函數將考生的總分從大到小排列並輸出
void average( student *head);//求學生成績的平均分的函數
void save(student *head);//保存函數
student *Read();//讀取函數
private:
student *p1,*p2,*p3,*head,st;
};
Information::Information()
{
cout<<" ******************************************************************************\n";
cout<<" ------------------------<<歡迎您使用學生成績管理系統>>------------------------\n";
cout<<" ******************************************************************************\n\n";
}
Information::~Information()
{
cout<<" ******************************************************************************\n";
cout<<" ------------------------<<謝謝您使用學生成績管理系統>>------------------------\n";
cout<<" ******************************************************************************\n";
}
student *Information::creat(void)
{//定義一個指向struct student的結構體指針函數*creat()用來增加考生信息.
char ch[Q];n=0; //用來存放姓名的
p1=p2=(student *)malloc(LEN);//調用malloc()函數用來開辟一個新的存儲單元
cout<<" -------------<<請建立學生考試信息表,在姓名處鍵以 ! 結束輸入。>>--------------"<<endl;
cout<<" 姓名:";
cin>>ch;
head=NULL; //給指針head賦初值
while (strcmp(ch,"!")!=0)
{//調用字元比較函數strcmp()用來判斷是否繼續輸入
char str[10];
int flag=0;
p1=(student *)malloc(LEN);//調用malloc()函數用來開辟一個新的存儲單元
strcpy(p1->name,ch); //將循環結構前面輸入的姓名復制到結構體名為p1的數組name中
cout<<" 性別:";
cin>>p1->sex;

cout<<" 准考證號(8位):";
do{
cin>>str;
if(atol(str)>99999999 || atol(str)<1)
cout<<"對不起,請正確輸入!!!\n";
else
{
p1->id=atol(str); flag=1;
}
}while(flag==0);

flag=0;

cout<<" 計算機組成原理成績:";
do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{
p1->score[0]=atoi(str); flag=1;
}
}while(flag==0);

flag=0;

cout<<" 概率統計成績:";

do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{ p1->score[1]=atoi(str); flag=1;}
}while(flag==0);

flag=0;

cout<<" 英語成績:";

do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{ p1->score[2]=atoi(str); flag=1;}
}while(flag==0);
flag=0;

cout<<" C++成績:";

do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{ p1->score[3]=atoi(str); flag=1;}
}while(flag==0);

flag=0;

p1->total=p1->score[0]+p1->score[1]+p1->score[2]+p1->score[3];//計算總分
if(n==0)head=p1;//如果是輸入第一組學生考試信息就將指針p1賦給指針head
else p2->next=p1;//否則將p1賦給p2所指結構體的next指針
p2=p1;//將指針p1賦給指針p2
n++; //將n的值加1
cout<<" 姓名:";
cin>>ch;//將輸入的姓名存放到字元數組ch中
}
p2->next=NULL;//將p2所指結構體的next指針重新賦空值
return (head);//將輸入的第一組學生考試信息返回
}
//--------------->定義output()函數將考生的信息從頭指針所指內容開始輸出
void Information::output(student *head)
{

if(head==NULL) cout<<" 這是一個空表,請先輸入考生成績.\n";
else{
cout<<"-------------------------------------------------------------------------------\n";
cout<<" *學生考試成績信息表*\n";
cout<<"-------------------------------------------------------------------------------\n";
cout<<"准考證號 姓 名 性別 計算機組成原理 概率統計 英語 C++ 平均分 總分\n";
cout<<"-------------------------------------------------------------------------------\n";
p1=head;//將頭指針賦給p
do
{
cout<<setw(8)<<p1->id
<<setw(9)<<p1->name
<<setw(8)<<p1->sex
<<setw(13)<<p1->score[0]
<<setw(16)<<p1->score[1]
<<setw(10)<<p1->score[2]
<<setw(9)<<p1->score[3]
<<setw(6)<<p1->total/4.0
<<setw(11)<<p1->total<<endl;
cout<<"-------------------------------------------------------------------------------\n";
p1=p1->next;//將下一組考生信息的next指針賦給p
}while(p1!=NULL);//若指針p非空則繼續,目的是把所有的考生信息都傳給指針p然後輸出.
}
}
//------------>統計學生人數的函數
int Information::count(struct student *head)//定義函數count()統計考生總數
{
if(head==NULL)
return(0);//若指針head為空返回值為0
else return(1+count(head->next));//函數的遞歸調用
}
//----------->插入學生的成績的函數
student *Information::insert( student *head)
//插入新結點定義一個指向struct student的結構體指針函數*insert()用來添加考生信息.
{
char str[10];
int flag=0;
cout<<"\t----------------<<請輸入新增學生成績信息>>----------------\n"<<endl;
p1=(student *)malloc(LEN); //使p1指向插入的新結點
cout<<" 姓名:";
cin>>p1->name; //將輸入的姓名存放到結構體名為p1的數組name中
cout<<" 性別:";
cin>>p1->sex;
cout<<" 准考證號(8位):";
do{
cin>>str;
if(atol(str)>99999999 || atol(str)<1)
cout<<"對不起,請請正確輸入!!!\n";
else
{p1->id=atol(str); flag=1; }
}while(flag==0);

flag=0;

cout<<" 計算機組成原理成績:";
do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{ p1->score[0]=atoi(str); flag=1;}
}while(flag==0);

flag=0;

cout<<" 概率統計成績:";

do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{ p1->score[1]=atoi(str); flag=1;}
}while(flag==0);

flag=0;

cout<<" 英語成績:";

do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{ p1->score[2]=atoi(str); flag=1;}
}while(flag==0);
flag=0;

cout<<" C++成績:";

do{
cin>>str;
if(atoi(str)>100 || atoi(str)<1)
cout<<"對不起,請輸入1-100之間的數字!!\n";
else
{ p1->score[3]=atoi(str); flag=1;}
}while(flag==0);

flag=0;

p1->total=p1->score[0]+p1->score[1]+p1->score[2]+p1->score[3];//計算總分
p2=head;//將頭指針賦給p2
if(head==NULL) //若沒調用次函數以前的頭指針head為空
{
head=p1;p1->next=NULL;
}//則將p1賦給頭指針head並將p1所指結構體成員指針next賦空值
else
{
while((p1->id>p2->id)&&(p2->next!=NULL))
{
p3=p2;//p3指向原p2指向的結點
p2=p2->next;
}//p2後移一個結點
if(p1->id<=p2->id)
{
if(head==p2)
{
p1->next=head;
head=p1;
} //插入到第一個結點之前
else
{
p3->next=p1;
p1->next=p2;
} //插入到p3所指結點之後
}
else
{
p2->next=p1;
p1->next=NULL;
} //插入到尾結點之後
}
n++;//將學生人數加1
cout<<"\t你輸入的學生信息已經成功插入"<<endl;
return (head);
}
//------------>刪除函數
student *Information::cancel(student *head,long int num)//定義一個指向struct student的結構體指針函數*delete()用來刪除考生信息.
{

if(head==NULL)//若調用次函數以前的頭指針head為空
{
return(head);
}
else
{
p1=head;//否則將頭指針賦給p1
while(num!=p1->id&&p1->next!=NULL)//尋找要刪除的結點當p1所指的學生准考證號不是輸入的學生准考證號並且p1所指的next指針不為空
{
p2=p1;
p1=p1->next;
}//p2指向原p1指向的結點p1後移一個結點
if(num==p1->id)//如果輸入的學生准考證號是p1所指的學生准考證號//結點找到後刪除
{
if(p1==head) head=p1->next;//如果head指針和p1指針相等則將下一個結點賦給指針head
else
p2->next=p1->next;//否則將p1所指結點賦給p2所指結點將要刪除的學生信息跳過去
cout<<" 刪除准考證號為"<<num<<"的學生\n";
n--;//將學生人數減1
}
return(head);//將頭指針返回
}
}
//------------>查找函數
student *Information::find(student *head,long int num)
//定義一個指向struct student的結構體指針函數*find()用來查找考生信息.
{

if(head==NULL)//若調用次函數以前的頭指針head為空
{
cout<<" 這是一個空表,請先輸入考生成績.\n";
return(head);
}
else
{
p1=head;//否則將頭指針賦給p1
while(num!=p1->id&&p1->next!=NULL)
//尋找結點當p1所指的學生准考證號不是輸入的學生准考證號並且p1所指的next指針不為空
{
p1=p1->next;
}//p2指向原p1指向的結點p1後移一個結點
if(num==p1->id)//如果要查找的學生准考證號是p1所指的學生准考證號
{
cout<<"------------------------------------------------------------------------------\n";
cout<<"准考證號 姓名 性別 計算機組成原理 概率統計 英語 C++ 平均分 總分 \n";
cout<<"------------------------------------------------------------------------------\n";
cout<<setw(8)<<p1->id
<<setw(9)<<p1->name
<<setw(8)<<p1->sex
<<setw(13)<<p1->score[0]
<<setw(16)<<p1->score[1]
<<setw(10)<<p1->score[2]
<<setw(9)<<p1->score[3]
<<setw(6)<<p1->total/4.0
<<setw(11)<<p1->total<<endl;
cout<<"------------------------------------------------------------------------------\n";
}
else
cout<<" 沒找到准考證號為"<<num<<"的學生.\n"; //結點沒找到
return(head);
}
}
//------------定義inorder()函數將考生的總分從大到小排列並輸出
void Information::inorder(student *head)
{
int i,k,m=0,j;
student *p[Q];//定義一個指向struct student的結構體指針數組p
if(head!=NULL)//如果頭指針是空則繼續
{ m=count(head);
cout<<"------------------------------------------------------------------------------\n";
cout<<"學生考試成績統計表\n";
cout<<"------------------------------------------------------------------------------\n";
cout<<"准考證號 姓 名 性別 計算機組成原理 概率統計 英語 C++ 平均分 總分 名次\n";
cout<<"------------------------------------------------------------------------------\n";
p1=head;
for(k=0;k<m;k++)
{
p[k]=p1;
p1=p1->next;
}
for(k=0;k<m-1;k++) //選擇排序法
for(j=k+1;j<m;j++)
if(p[k]->total<p[j]->total)
{
p2=p[k];
p[k]=p[j];
p[j]=p2;
} //從大到小排列的指針
for(i=0;i<m;i++)
{
cout<<setw(8)<<p1->id
<<setw(9)<<p1->name
<<setw(8)<<p1->sex
<<setw(13)<<p1->score[0]
<<setw(16)<<p1->score[1]
<<setw(10)<<p1->score[2]
<<setw(9)<<p1->score[3]
<<setw(6)<<p1->total/4.0
<<setw(11)<<p1->total<<endl;
cout<<"------------------------------------------------------------------------------\n";
}
}
}
//------------>求各科平均分成績的函數

void Information::average(student *head)
{

int k,m;
float arg1=0,arg2=0,arg3=0,arg4=0;
if(head==NULL)//如果頭指針是空則繼續
{
cout<<" 這是一個空表,請先輸入考生成績.\n";
}
else
{
m=count(head);
p1=head;
for(k=0;k<m;k++)
{
arg1+=p1->score[0];
arg2+=p1->score[1];
arg3+=p1->score[2];
arg4+=p1->score[3];
p1=p1->next;
}
arg1/=m;arg2/=m;arg3/=m;arg4/=m;
cout<<"全班單科成績平均分\n";
cout<<"------------------------------------------------------------------------------\n";
cout<<" 計算機組成原理平均分:"<<setw(7)<<arg1
<<" 概率統計平均分:"<<setw(7)<<arg2
<<" 英語平均分:"<<setw(7)<<arg3
<<" C++平均分:"<<setw(7)<<arg4<<endl;
cout<<"------------------------------------------------------------------------------\n";
}
}
//------------------->保存函數.
void Information::save(student *head)
{
ofstream out("data.txt",ios::out);
out<<count(head)<<endl;
while(head!=NULL)
{ out<<head->name<<"\t"
<<head->id<<"\t"<<"\t"
<<head->sex<<"\t"
<<head->score[0]<<"\t"
<<head->score[1]<<"\t"
<<head->score[2]<<"\t"
<<head->score[3]<<"\t"
<<head->total<<endl;
head=head->next;
}

}
//———————————>讀取函數的實現
student *Information::Read()
{ int i=0;
p1=p2=( student *)malloc(LEN);
head=NULL;
ifstream in("data.txt",ios::out);
in>>i;
if(i==0){cout<<" data.txt 文件中的數據為空,請先輸入數據。"<<endl; return 0;}
else {
cout<<" …………………………………………………………………………………………"<<endl;
for(;i>0;i--)
{ p1=(student *)malloc(LEN);
cin>>st.name>>st.id>>st.sex
>>st.score[0]>>st.score[1]>>st.score[2]>>st.score[3]
>>st.total;
strcpy(p1->name,st.name);
p1->id=st.id;
strcpy(p1->sex,st.sex);
p1->score[0]=st.score[0];
p1->score[1]=st.score[1];
p1->score[2]=st.score[2];
p1->score[3]=st.score[3];
p1->total=st.total;
if(n==0)head=p1;//如果是輸入第一組學生考試信息就將指針p1賦給指針head
else p2->next=p1;//否則將p1賦給p2所指結構體的next指針
p2=p1;//將指針p1賦給指針p2
n++; //將n的值加1
//顯示讀入數據
cout<<" "<<p1->name<<"\t"
<<p1->id<<"\t"<<"\t"
<<p1->sex<<"\t"
<<p1->score[0]<<"\t"
<<p1->score[1]<<"\t"
<<p1->score[2]<<"\t"
<<p1->score[3]<<"\t"
<<p1->total<<endl;
cout<<" …………………………………………………………………………………………"<<endl;
//
}
cout<<" 數據已經成功讀取完畢。"<<endl;
p2->next=NULL;
return (head);
}

}
//------------------------------>主函數.
int main(void)
{
Information person;
student *head=NULL;
char str[10];
int flag=0;
int choice;
long int i;
head=person.Read();
do{

cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓";
cout<<"┃ 學生成績管理系統主菜單界面 ┃";
cout<<"┃ 讀取數據請輸入數字零 ┃";
cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫";
cout<<"┃ ①.輸入學生成績 ┃";
cout<<"┃ ②.顯示學生成績 ┃";
cout<<"┃ ③.排序統計成績 ┃";
cout<<"┃ ④.查找學生成績 ┃";
cout<<"┃ ⑤.增加學生成績 ┃";
cout<<"┃ ⑥.刪除學生成績 ┃";
cout<<"┃ ⑦.保存退出系統 ┃";
cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛";
cout<<"請輸入您的選擇(1--7):( )\b\b";

cin>>str;
if(atoi(str)>7 || atoi(str)<1)
cout<<"對不起,請輸入1-7這幾個數字!!\n";
else
{
choice=atoi(str);
switch(choice)
{
case 1:
head=person.creat();
break;
case 2:
person.output(head);
break;
case 3:
person.inorder(head);
person.average(head);
cout<<" 參加考試的學生人數為:"<<person.count(head)<<"人\n";
break;
case 4:
cout<<" 請輸入要查找的准考證號(8位):";
do{
cin>>str;
if(atol(str)>99999999 || atol(str)<1)
cout<<"對不起,請輸入正確輸入!!!\n";
else
{i=atol(str); flag=1; }
}while(flag==0);

flag=0;
person.find(head,i);
break;
case 5:
head=person.insert(head);
person.output(head);
break;
case 6:
cout<<" 請輸入要刪除的准考證號(8位):";
do{
cin>>str;
if(atol(str)>99999999 || atol(str)<1)
cout<<"對不起,請輸入正確輸入!!!h\n";
else
{i=atol(str); flag=1; }
}while(flag==0);

flag=0;
head=person.cancel(head,i);
person.output(head);
break;
case 7:
person.save(head);
cout<<"文件已保存!可以安全退出!!!"<<endl;
break;
default :cout<<" 對不起,您的輸入有誤,請重新輸入。\n";
break;
}
}
}while(choice!=7);

return 0;
}

㈢ C語言程序設計的項目介紹要寫什麼

1、項目產生的目的,開發一個軟體,肯定是有用,才去開發的,如果這個軟體沒有存在的必要性那就不會去開發了。(學生直接寫,學習軟體開發流程,這個夠完美的了)
2、軟體的功能,能做什麼,不能做什麼。(軟體是給人用的,不是你開發者使用的,開發者制定規則,使用者遵循開發者制定的規則來使用)

㈣ 用C語言設計一個學生信息查詢系統程序

1、首先創建一個c語言項目。然後旁汪者右鍵頭文件,創建一個Stu的頭文件。

㈤ C語言程序設計,做一個簡單計算器。

1、首先,打開Vs 2010,如圖。

㈥ c語言程序設計實例

這是個宏定義,表示後面那個格式化字元串以FORMAT來代替

熱點內容
寶馬3系哪個配置合適 發布:2025-02-04 06:03:10 瀏覽:326
磁碟存儲器的管理課後答案 發布:2025-02-04 05:58:58 瀏覽:598
b級車買哪個配置 發布:2025-02-04 05:56:41 瀏覽:560
我的世界如何看lp伺服器 發布:2025-02-04 05:56:33 瀏覽:482
外賣盒子如何設置密碼 發布:2025-02-04 05:49:33 瀏覽:505
國產安卓編程軟體哪個最好 發布:2025-02-04 05:49:25 瀏覽:388
什麼是身份證密碼 發布:2025-02-04 05:43:41 瀏覽:785
雲伺服器江蘇 發布:2025-02-04 05:38:46 瀏覽:238
演算法及vb 發布:2025-02-04 05:33:37 瀏覽:102
安卓手機怎麼自檢電池 發布:2025-02-04 05:31:31 瀏覽:410