#包含“stdio.h”
#包含“conio.h”
#包含" string.h "
#包含" stdlib.h "
# define CMD _ START printf(" \ n \ n # # # # # # # # # #啟動壹個命令# # # # # # # # # \ n ");
/*用於標記命令執行的開始*/
# define CMD _ END printf(" \ n # # # # # # # # # # # END a command # # # # # # # # # \ n \ n ");
/*用於標記命令執行的結束,編寫這兩條語句是為了提供更好的用戶界面*/
#定義數據文件" data.dat "
/*這是數據文件名*/
#定義臨時文件“temp.dat”
/*這是臨時文件的名稱,在刪除記錄的功能中使用。
請參考Delete()函數*/
typedef結構標記學生
{
char ID[30];/*學生ID */
char Name[30];/*姓名*/
char類[255];/*類*/
迷人的性愛;/*性別,值為f或f或m或m */
int數學;/*數學成績*/
int英語;/*英語成績*/
int計算;/*計算機分數*/
int哲學;/*哲學成就*/
int PE/*體育成績*/
}學生;
/*這是學生信息結構*/
int ShowMenu();/*在屏幕上打印主菜單的函數,其返回值是所選菜單的項目編號*/
int ReadData(FILE*,Student *);/*函數從打開的數據文件中讀取記錄。錯誤返回0 */
int WriteData(FILE*、Student *);/*將記錄寫入數據文件的函數,錯誤返回0 */
void Output_Rec(學生*);/*在屏幕上打印記錄*/
void Input_Rec(學生*);/*允許用戶輸入記錄中每壹項的值,在添加記錄時使用*/
void CopyRec(Student*,Student *);/*將壹條記錄復制到另壹條記錄*/
/*標題中需要的函數*/
void打印();/*實現查看數據文件內容的功能*/
void Add();/*添加記錄的函數*/
void Delete();/*刪除記錄的功能*/
void統計();/*數據統計分析功能*/
void Find(int);/*用於查找函數的函數。參數確定是按ID查找還是按名稱查找*/
int退出;/*壹個全局變量,在下面的main()函數中用於決定何時退出主循環*/
void main()
{
int cmd/*用戶選擇的菜單項的標簽*/
退出= 0;/*初始化為不退出*/
/*這是程序的主循環,每次都打印出主菜單。
供用戶選擇相應的序列號來執行相應的功能*/
而(!退出)
{
cmd = ShowMenu();/*顯示主菜單並返回用戶選擇的菜單項的編號*/
CMD_START /*在屏幕上打印壹個行分隔符,告訴用戶這是壹個子函數的開始*/
Switch( cmd) /*根據用戶的選擇調用相應的函數有多個分支*/
{
案例1:
print();打破;/*用戶選擇菜單1,程序執行查看數據文件的功能*/
案例二:
add();打破;/*用戶選擇菜單2,程序執行添加記錄功能*/
案例三:
delete();打破;/*用戶選擇3號菜單,程序執行刪除記錄功能*/
案例4:
統計();打破;/*用戶選擇菜單4,程序執行統計數據功能*/
案例5:
查找(5);打破;/* Find_ID,菜單5執行按ID(學號)搜索的功能)*/
案例6:
查找(6);打破;/* Find_Name,菜單6執行按名稱搜索的功能*/
案例7:
退出= 1;/*用戶選擇退出菜單*/
printf("感謝您的使用。\n\n快樂每壹天!!\n\n拜拜....\ n ");
打破;
默認值:
printf("請輸入壹個介於\t1\tto\t7之間的數字。\ n ");
/*用戶輸入的序列號不在處理範圍內*/
}
CMD_END /*打印壹個行分隔符,告訴用戶所選菜單的功能已經完成*/
如果(退出!= 1) /*檢查用戶是否請求退出*/
{
printf("按任意鍵返回主菜單....\ n ");
getch();/*使用不帶回顯的字符輸入功能暫停執行,按任意鍵繼續*/
}
}
}
int ShowMenu()
{
int cmd = 0;/*保存用戶的選擇*/
/*定義程序支持的菜單項*/
char Menu_SeeData[] = "\t1。\ t查看數據文件中的記錄\ n ";/*查看數據文件*/
char Menu_Add[] = "\t2。\ t添加新記錄\ n ";/*添加記錄*/
char Menu_Delete[] = "\t3。\ t刪除舊記錄\ n ";/*刪除記錄*/
char Menu_Statistics[] = "\t4。\ t進行統計\ n ";/*統計分析*/
char Menu_Find_ID[] = "\t5。\ t從ID中查找記錄\ n ";/*按學號搜索*/
char Menu_Find_Name[] = "\t6。\ t從名稱中查找記錄\ n ";/*按姓名搜索*/
char Menu_Quit[] = "\t7。\ t退出\ n ";/*退出*/
/*打印屏幕上的主菜單*/
printf(" \ n \ n # # # # # # # # # # # # #主菜單# # # # # # # # # # # # \ n ");
printf(" ## # # # # # # # # # # # # # # # # # # # # # # # # # # n \ n ");
printf(Menu _ SeeData);
printf(Menu _ Add);
printf(Menu _ Delete);
printf(菜單_統計);
printf(Menu _ Find _ ID);
printf(菜單_查找_名稱);
printf(Menu _ Quit);
printf(" \ n # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ");
printf("\n\n輸入您選擇的索引:");
scanf("%d ",& ampcmd);/*接受用戶選擇*/
printf(" \ n ");
返回cmd/*將用戶輸入返回到主循環進行處理*/
}
Void Print() /*打印數據文件的記錄內容*/
{
FILE * fp = NULL/*文件指針*/
學生記錄;/*存儲從文件中讀取的記錄*/
int I = 0;/*實現計數和分屏打印功能*/
fp = fopen(DATA_FILE," Rb ");/*以二進制讀取模式打開數據文件*/
If(fp == NULL) /*打開文件時出錯*/
{
printf("無法打開數據文件:%s\n ",DATA _ FILE);
返回;
}
while(ReadData(fp,& ampRec))/*當readdata()函數失敗或在文件末尾返回0時,可以做壹個循環條件*/
{
output _ Rec(& amp;rec);/*正確讀取並輸出記錄*/
printf("-");
/*打印壹個行分隔符以創建壹個良好的用戶界面*/
i++;/*計數器加1 */
If( i% 4 == 0) /*顯示四次暫停*/
{
printf("\n按任意鍵繼續...\ n ");
getch();
}
}
printf("\n當前數據文件有\ t % d \記錄。\n”,I);
fclose(FP);/*關閉文件*/
}
Voideadd ()/*添加記錄*/
{
學生記錄;
FILE * fp = NULL
input _ Rec(& amp;rec);/*讓用戶輸入新記錄的內容*/
fp = fopen(DATA_FILE," ab ");/*通過添加*/打開數據文件
if( fp == NULL)
{
printf("無法打開數據文件寫入...\ n ");
返回;
}
if( WriteData(fp,& ampRec) == 1) /*將新記錄寫入文件,並檢查是否寫對*/
printf("\n\n添加新記錄成功\ n \ n ");
其他
printf("\n\n無法將新記錄寫入數據文件\ n ");
fclose(FP);
}
Void Delete() /*刪除記錄*/
{
學生記錄;
FILE* fpr,* fpw/*用於讀寫的兩個文件指針*/
char buf[30];/*接受用戶輸入的ID緩沖區*/
char cmd[255];/*執行的系統命令*/
int del _ count/*實際刪除的記錄數*/
del _ count = 0;
printf("\n請鍵入您要我刪除的記錄的ID。");
printf(" \ n ID:");/*提示用戶輸入*/
scanf("%s ",buf);
fpr = fopen(DATA_FILE," Rb ");/*從原始記錄文件中讀取數據並跳過要刪除的記錄*/
if( fpr == NULL)
{
printf("無法打開數據文件來讀取記錄...\ n ");
返回;
}
fpw = fopen(TEMP_FILE," WB ");/*打開壹個臨時文件來保存不會被刪除的記錄*/
if( fpw == NULL)
{
printf("無法打開數據文件寫入...\ n ");
返回;
}
while(ReadData(fpr,& ampRec)) /*讀取要保存的記錄*/
{
if(strcmp(rec。ID,buf)!= 0)
{
寫數據(fpw & amp;rec);/*寫入臨時文件,然後刪除原始數據文件。
然後將臨時文件命名為原始數據文件的名稱*/
}
其他
{
del _ count++;/*跳過的記錄數,即刪除的記錄數*/
}
}
fclose(FPR);
fclose(fpw);
strcpy(cmd," del ");/*構造壹個命令字符串並執行*/
strcat(cmd,DATA _ FILE);
系統(cmd);
rename(TEMP_FILE,DATA _ FILE);/*直接調用C語言的重命名函數將臨時文件重命名為數據文件的名稱*/
printf("\n我已經刪除了\ t % d \記錄。\n ",del _ count);
}
Void Statistics() /*統計分析函數*/
{
int i50,i60,i70,i80,i90/*平均分小於60,60-69,70-79,80-89,>分數段人數=90 */
浮動平均值;/*平均分數*/
int sum,sum_high,sum _ low/*總分,總分最高,總分最低*/
學生stu_high,stu _ low/*總分最高和最低的學生信息*/
學生stu_math_high,stu _ english _ high/*各科成績最高的學生記錄復印件*/
學生stu_compute_high,stu_philosophy_high,stu _ PE _ high
學生stu_math_low,stu _ english _ low/*每科最低分學生記錄的復印件*/
學生stu_compute_low,stu_philosophy_low,stu _ PE _ low
FILE * fp
學生記錄;
int計數;/*用於判斷是否第壹次讀取數據的計數器*/
count = sum = sum _ high = sum _ low = i50 = i60 = i60 = i70 = i80 = i90 = 0;
fp = NULL/*初始化數據*/
fp = fopen(DATA_FILE," Rb ");
if(fp == NULL)
{
printf(" \ n無法打開要讀取的數據文件...\ n ");
返回;
}
while(ReadData(fp,& ampRec)) /*讀取數據*/
{
count++;/*計數器加1 */
總和=建議。數學+娛樂。英語+ rec。計算+記錄。體育+娛樂。哲學;/*總和*/
/*平均值*/
avg =((float)sum)/5;/*平均分數*/
/*對每個分數段進行以下統計*/
if(avg & lt;60)
i50++;
else if(avg & lt;70)
i60++;
else if(avg & lt;80)
i70++;
else if(avg & lt;90)
i80++;
其他
i90++;
/*最高和最低*/
if(count & lt;= 1) /*初讀,初始化,無比較*/
{
sum _高= sum _低= sum;
CopyRec(& amp;stu _ high & amp;rec);
CopyRec(& amp;stu _ low & amp;rec);
}
其他
{
if(sum & gt;sum_high)
{
sum _ high = sum/*獲得最高總分*/
CopyRec(& amp;stu _ high & amp;rec);/*保存總分最高的學生的信息*/
}
if(sum & lt;sum_low)
{
sum _ low = sum/*獲得最低分*/
CopyRec(& amp;stu _ low & amp;rec);/*保存總分最低的學生的信息*/
}
}
/*科目最高和最低*/
If(count == 1) /*如上初始化*/
{
CopyRec(& amp;stu _ math _ high & amp;rec);
CopyRec(& amp;stu _ English _ high & amp;rec);
CopyRec(& amp;stu _ compute _ high & amp;rec);
CopyRec(& amp;stu _哲學_高& amprec);
CopyRec(& amp;stu _ PE _ high & amp;rec);
CopyRec(& amp;stu _ math _ low & amp;rec);
CopyRec(& amp;stu _ English _ low & amp;rec);
CopyRec(& amp;stu _ compute _ low & amp;rec);
CopyRec(& amp;stu _ philosophy _ low & amp;rec);
CopyRec(& amp;stu _ PE _ low & amp;rec);
}
其他
{
/*高*/
/*保存各科成績最高的學生信息*/
如果(建議。數學& gt斯圖_數學_高。數學)
CopyRec(& amp;stu _ math _ high & amp;rec);
如果(建議。英語& gt斯圖_英語_高。英語)
CopyRec(& amp;stu _ English _ high & amp;rec);
如果(建議。Compute & gt斯圖_計算_高。計算)
CopyRec(& amp;stu _ compute _ high & amp;rec);
如果(建議。哲學& gtstu _哲學_高。哲學)
CopyRec(& amp;stu _哲學_高& amprec);
如果(建議。PE & gtstu_PE_high。PE)
CopyRec(& amp;stu _ PE _ high & amp;rec);
/*低*/
/*保存各科成績最低的學生信息*/
如果(建議。數學& lt斯圖_數學_低。數學)
CopyRec(& amp;stu _ math _ low & amp;rec);
如果(建議。英語& ltstu _英語_low。英語)
CopyRec(& amp;stu _ English _ low & amp;rec);
如果(建議。計算& ltstu_compute_low。計算)
CopyRec(& amp;stu _ compute _ low & amp;rec);
如果(建議。哲學& ltstu _哲學_low。哲學)
CopyRec(& amp;stu _ philosophy _ low & amp;rec);
如果(建議。PE & ltstu_PE_low。PE)
CopyRec(& amp;stu _ PE _ low & amp;rec);
}
}/*而End */
if(count & lt;1)
{
printf("\n數據文件中沒有記錄。\ n ");
}
其他
{
/*平均值*/
/*輸出平均分割統計*/
printf("\n每個段中的計數:\ n ");
printf(" \ t & lt;60\t:\t%d\n ",i50);
printf("\t60 - 69\t:\t%d\n ",i60);
printf("\t70 - 79\t:\t%d\n ",i70);
printf("\t80 - 90\t:\t%d\n ",i80);
printf(" \ t & gt;= 90\t:\t%d\n ",i90);
printf("-");
getch();
/*最高和最低*/
/*輸出總分最高的學生的信息*/
printf("\n最高分學生:\ n ");
printf("標記為:%d\n ",sum _ high);
printf("學生是:\ n ");
output _ Rec(& amp;stu _ high);
/*輸出總分最高的學生的信息*/
printf("\n最低分學生:\ n ");
printf("標記為:%d\n ",sum _ low);
printf("學生是:\ n ");
output _ Rec(& amp;stu _ low);
printf("-\ n ");
getch();
/*科目最高和最低*/
/*輸出各科最高分和最低分的統計*/
printf("最高\ t math:\ n ");
output _ Rec(& amp;斯圖_數學_高);
printf("最低級的數學:\ n ");
output _ Rec(& amp;stu _ math _ low);
printf("-\ n ");
getch();/*暫停並按任意鍵繼續*/
printf("最高英語:\ n ");
output _ Rec(& amp;stu _英語_高);
printf("最低級的英語:\ n ");
output _ Rec(& amp;stu _ English _ low);
printf("-\ n ");
getch();
printf("最高計算:\ n ");
output _ Rec(& amp;斯圖_計算_高);
printf("最低計算:\ n ");
output _ Rec(& amp;stu _ compute _ low);
printf("-\ n ");
getch();
printf("最高哲學:\ n ");
output _ Rec(& amp;stu _哲學_高);
printf("最低級的哲學:\ n ");
output _ Rec(& amp;stu _哲學_ low);
printf("-\ n ");
getch();
printf("最高PE:\ n ");
output _ Rec(& amp;stu _ PE _ high);
printf("最低PE:\ n ");
output _ Rec(& amp;stu _ PE _ low);
printf("-\ n ");
}
fclose(FP);
}
void Find(int is Find _ From _ ID)/* Find函數*/
{
char buf[30];/*用於接受用戶輸入條件的緩沖區*/
學生記錄;
int find _ count/*找到的記錄數*/
FILE * fp
find _ count = 0;
fp = fopen(DATA_FILE," Rb ");
if( fp == NULL)
{
printf("\n無法打開要搜索的數據文件...\ n ");
返回;
}
switch(isFind_From_ID)
{
案例5: /*ID,按學號搜索*/
printf("\n請輸入ID:");
scanf("%s ",buf);/*提示用戶輸入*/
while(ReadData(fp,& ampRec)) /*讀取數據*/
{
If(strcmp(rec。ID,buf) == 0) /*比較*/
{
find _ count++;
output _ Rec(& amp;rec);/*輸出符合條件的記錄*/
printf("-\ n ");
}
}
打破;
案例6:/*姓名,按姓名搜索*/
printf("\n請輸入名稱:");
scanf("%s ",buf);
while(ReadData(fp,& amprec))
{
if(strcmp(rec。Name,buf) == 0)
{
find _ count++;
output _ Rec(& amp;rec);
printf("-\ n ");
}
}
打破;
默認值:
printf(" \ n請鍵入右索引...\ n ");/*處理isFind_From_ID既不是5也不是6的情況*/
}
if(find _ count & gt;0) /*輸出找到的記錄數*/
{
printf(" \ n Have find out \ t % d \ t record \ n ",find _ count);
}
其他
{
printf("\n非常抱歉。\n我找不到妳想要的。\ n ");
printf("\n我建議妳改壹些其他的關鍵詞。\ n ");
}
fclose(FP);
}
Int讀取數據(file * FP,student * dest _ rec)/*讀取數據記錄*/
{
int r;
if(( fp == NULL ) || ( Dest_Rec == NULL))
返回0;/*錯誤*/
r = fread(Dest_Rec,sizeof(Student),1,FP);
if(r!= 1)
返回0;
返回1;
}
Intwritedata (file * FP,student * src _ rec)/*寫數據記錄*/
{
int r;
if((fp == NULL) || (Src_Rec == NULL))
返回0;/*錯誤*/
r = fwrite(Src_Rec,sizeof(Student),1,FP);
if(r!= 1)
返回0;
返回1;
}
Void Output_Rec( Student* stu) /*在屏幕上輸出壹個學生的信息*/
{
printf(" \ n ");
printf(" Name : %s ",stu-& gt;姓名);
printf(" \ t \ tSex:");
如果(stu-& gt;sex = = ' M ' | | stu-& gt;Sex == 'm ')
printf(“男”);
其他
printf(“女”);
printf("\n ID : %s\t\tClass : %s\n ",stu-& gt;ID,stu-& gt;類);
printf(" Math = % d \ t English = % d \ t computer = % d \ n ",stu-& gt;數學,斯圖-& gt;英語,斯圖-& gt;計算);
printf(" Philosophy = % d \ t \ tPE = % d \ n ",stu-& gt;哲學,斯圖-& gt;PE);
printf(" \ n ");
}
Void Input_Rec( Student* stu) /*讓用戶輸入學生的各種信息*/
{
if(stu == NULL)
返回;
printf(" \ n Name = ");
scanf("%s ",stu-& gt;姓名);
/*下面的代碼只能輸入F,F,M,M,M的函數*/
printf(" \ tSex =(F | M)");
while(1)
{
斯圖-& gt;sex = getch();/*無回聲輸入*/
如果(stu-& gt;sex = = ' F ' | | stu-& gt;sex = = ' f ' | | stu-& gt;sex = = ' M ' | | stu-& gt;Sex == 'm ')
{
printf("%c ",stu-& gt;性);/*輸出用戶輸入的字符,模擬正常數據輸入時的回聲*/
打破;
}
}
/*以下提示供用戶輸入結構內容*/
printf(" \ n ID = ");
scanf("%s ",stu-& gt;ID);
printf(" \ n Class = ");
scanf("%s ",stu-& gt;類);
printf(" \ n Math = ");
scanf("%d ",& amp(斯圖-& gt;數學));
printf(" \ n English = ");
scanf("%d ",& amp(斯圖-& gt;英語));
printf(" \ n Compute = ");
scanf("%d ",& amp(斯圖-& gt;計算));
printf("\n哲學= ");
scanf("%d ",& amp(斯圖-& gt;哲學));
printf(" \ n PE = ");
scanf("%d ",& amp(斯圖-& gt;PE));
printf(" \ n ");
}
/*因為結構不能直接用等號(=)賦值,所以寫壹個賦值函數*/
void CopyRec(Student* dest_stu,Student* src_stu)
{
/*將項目從源記錄復制到目標記錄*/
strcpy(dest _ stu-& gt;名稱,src _ stu-& gt;姓名);
strcpy(dest _ stu-& gt;ID,src _ stu-& gt;ID);
strcpy(dest _ stu-& gt;Class,src _ stu-& gt;類);
dest _ stu-& gt;sex = src _ stu-& gt;性;
dest _ stu-& gt;math = src _ stu-& gt;數學;
dest _ stu-& gt;English = src _ stu-& gt;英語;
dest _ stu-& gt;compute = src _ stu-& gt;計算;
dest _ stu-& gt;哲學= src _ stu-& gt;哲學;
dest _ stu-& gt;PE = src _ stu-& gt;PE;
}
雖然程序和妳的要求不太壹樣,但是很像。其他石頭可以用來攻玉。。。。希望能理解和幫助。