?? students-achieve-grade-management-system.txt
字號:
/*-------------1-------------*/
#include<bios.h>
#include<dos.h> /*頭文件*/
#include<conio.h>
#include<ctype.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
#define NULL 0
#define ESC 0x001b /* 退出 */
#define F2 0x3c00 /*輸入學生成績*/
#define F3 0x3d00 /*按學號查找*/
#define F5 0x3f00 /*列出所有學生成績*/
#define F6 0x4000 /*統計*/
struct stuType /*定義結構體變量*/
{
char NO[11]; /*學號長度為10*/
char XM[10];
float CJ[4]; /*包含4門成績*/
};
/*-------------5-------------*/
int JY_CJ(float stu_CJ) /*學生成績只能在0~100之間*/
{
if(stu_CJ<0||stu_CJ>100)
{ printf("\t\t\t輸入錯誤,成績只能在0~100之間!\n");
return 0;
}
return 1;
}
/*-------------6-------------*/
void CreatFile() /*輸入文件*/
{ FILE *fp;
struct stuType stu,stu0={"","",}; /*對stu0先賦值*/
fp=fopen("stu.dat","wb+"); /*打開或創建一個二進制文件,打開時將原來的內容刪除*/
if(fp==NULL)
{ printf("\t\t\t文件打開失敗!\n\t\t\t按任意鍵返回...");
getch();
return;
}
else
{int i,j;
printf("請輸入學生的個數 qing shu ri xue sheng de ge shu :\n");
scanf("%d",&i);
for(j=1;j<=i;i--)
{ stu=stu0;
printf("\n\t\t\t請輸入學號qing shu ri xue hao:"); /*輸入學號*/
scanf("%10s",stu.NO);
printf("\n\t\t\t請輸入姓名qing shu ri xing ming:"); /*輸入姓名*/
scanf("%10s",stu.XM);
do{ printf("\n\t\t\t請輸入語文成績Chinese:"); /*輸入成績并檢驗其正確性*/
scanf("%f",&stu.CJ[0]);
}while(!JY_CJ(stu.CJ[0]));
do{ printf("\n\t\t\t請輸入數學成績Math:"); /*同上*/
scanf("%f",&stu.CJ[1]);
}while(!JY_CJ(stu.CJ[1]));
do{ printf("\n\t\t\t請輸入英語成績English:");
scanf("%f",&stu.CJ[2]);
}while(!JY_CJ(stu.CJ[2]));
do{ printf("\n\t\t\t請輸入總評成績zeng ping:");
scanf("%f",&stu.CJ[3]);
}while(!JY_CJ(stu.CJ[3]));
fwrite(&stu,sizeof(struct stuType),1,fp); /*寫文件*/
}
}
fclose(fp); /*關閉文件*/
}
/*-------------7-------------*/
void Search_Xuehao() /*按學號查詢*/
{ FILE *fp;
int flag;
struct stuType stu,stud;
fp=fopen("stu.dat","rb");
if(fp==NULL) /*若文件打不開則輸出下面的信息*/
{ printf("\t\t\t文件打開失敗wen jian da kai shi bai!\n\t\t\t按任意鍵返回...");
getch();
return;
}
else
{ do{ puts("\n\t\t\tshu ri’#’end");
printf("\t\t\t請輸入要查詢的學號:");
scanf("%10s",stu.NO);
if(strcmp(stu.NO,"#")==0)break; /*若輸入“#”則結束循環*/
flag=0;
rewind(fp);
while(fread(&stud,sizeof(struct stuType),1,fp)) /*檢查文件指針結束*/
{ if(strcmp(stu.NO,stud.NO)==0) /*比較學號*/
{ puts("\t\t\t該學生成績如下cheng ji ru xia:");
printf("\t\t\t學號NO:%s\n",stud.NO);
printf("\t\t\t姓名name:%s\n",stud.XM);
printf("\t\t\t語文chinese:%.1f\n",stud.CJ[0]);
printf("\t\t\t數學math:%.1f\n",stud.CJ[1]);
printf("\t\t\t英語enlish:%.1f\n",stud.CJ[2]);
printf("\t\t\t總評zen ping:%.1f\n",stud.CJ[3]);
flag=1; /*記錄學號是否查到*/
}
}
if(flag==0)puts("\t\t\t無此學號!");
}while(strcmp(stu.NO,"#")!=0);
}
fclose(fp); /*關閉文件*/
}
/*-------------9-------------*/
int ListFile(void) /*輸出文件,列出所有學生成績*/
{ FILE *fp;
int REC=0; /*記錄學生人數*/
struct stuType stu;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{ printf("\t\t\t文件打開失敗wen jian da kai shi bai!\n\t\t\t按任意鍵返回...");
getch();
return 1;
}
else{ printf("\t\t\t學生成績如下xue sheng cheng ji ru xia:\n");
printf("\t\t\t學號NO\t\t姓名name\t語文chinese\t數學math\t英語enlish\t總評zen ping\n");
rewind(fp);
while(fread(&stu,sizeof(struct stuType),1,fp))
{ /*每讀取一個長度的數據就輸出*/
printf("\t\t\t%s",stu.NO);
printf("\t%s",stu.XM);
printf("\t%.1f",stu.CJ[0]);
printf("\t%.1f",stu.CJ[1]);
printf("\t%.1f",stu.CJ[2]);
printf("\t%.1f",stu.CJ[3]);
printf("\n");
REC++;
if(REC%20==0) /*每輸出20個學生成績,停一下*/
{ printf("\t\t\t請按任意鍵繼續...\n");
getch();
}
}
}
fclose(fp); /*關閉文件*/
printf("\t\t\t請按任意鍵繼續...");
getch();
}
/*-----------10---------------*/
void Statistics() /*統計及格和優秀人數*/
{ FILE *fp;
int REC=0,pass[4]={0},good[4]={0}; /*REC--記錄個數,即人數,pass--及格人數,good--優秀人數*/
float highest[4]={0},score[4]={0}; /*highest--最高分,score--總分*/
struct stuType stu;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{ printf("\t\t\t文件打開失敗wen jian da kai shi bai!\n\t\t\t按任意鍵返回...");
getch();
return;
}
else { rewind(fp);
while(fread(&stu,sizeof(struct stuType),1,fp))
{ REC++;
score[0]=score[0]+stu.CJ[0]; /*語文*/
if(stu.CJ[0]>=60)pass[0]++;
if(stu.CJ[0]>=80)good[0]++;
if(highest[0]<stu.CJ[0])highest[0]=stu.CJ[0];
score[1]=score[1]+stu.CJ[1]; /*數學*/
if(stu.CJ[1]>=60)pass[1]++;
if(stu.CJ[1]>=80)good[1]++;
if(highest[1]<stu.CJ[1])highest[1]=stu.CJ[1];
score[2]=score[2]+stu.CJ[2]; /*英語*/
if(stu.CJ[2]>=60)pass[2]++;
if(stu.CJ[2]>=80)good[2]++;
if(highest[2]<stu.CJ[2])highest[2]=stu.CJ[2];
score[3]=score[3]+stu.CJ[3]; /*總評*/
if(stu.CJ[3]>=60)pass[3]++;
if(stu.CJ[3]>=80)good[3]++;
if(highest[3]<stu.CJ[3])highest[3]=stu.CJ[3];
}
if(REC==0) /*可以防止記錄為0是REC作除數而造成的錯誤*/
{ printf("\t\t\t未輸入學生記錄wei shu ri xue sheng ji lu!按任意鍵返回...");
getch();
return;
}
else{
printf("\t\t\t\t 語文chinese\t 數學math\t 英語enlish\t 總評zen ping\n"); /*輸出統計信息*/
printf("\t\t\t平均分ping jun fen: %.1f\t %.1f\t %.1f\t %.1f\n",score[0]/REC,score[1]/REC,score[2]/REC,score[3]/REC);
printf("\t\t\t最高分zui gao fen: %.1f\t %.1f\t %.1f\t %.1f\n",highest[0],highest[1],highest[2],highest[3]);
printf("\t\t\t優秀人數you xiu ren shu:%d\t %d\t %d\t %d\n",good[0],good[1],good[2],good[3]);
printf("\t\t\t及格人數ji ge ren shu:%d\t %d\t %d\t %d\n",pass[0],pass[1],pass[2],pass[3]);
}
}
fclose(fp);
printf("\n\t\t\t請按任意鍵繼續an any jian...");
getch();
}
/*-------------12-------------*/
int GetKey(void) /*此函數返回一個按鍵的數值*/
{ int key;
key=bioskey(0); /*bioskey為調用BIOS鍵盤接口*/
if(key<<8) /*位移*/
{
key=key&0x00ff;
}
return key; /*返回按鍵*/
}
/*-------------13-------------*/
void main()
{ int key;
struct date d; /*定義時間結構體*/
getdate(&d); /*讀取系統日期并把它放到結構體d中*/
clrscr(); /*清除屏幕*/
printf("\n\n\n\n\n");
printf("\t\t\t****************************\n"); /*版本信息*/
printf("\t\t\t xue sheng cheng ji guanli xi tong \n");
printf("\t\t\t****************************\n");
printf("\t\t\t請按任意鍵繼續an any jian...");
/*while(!kbhit());*/
getch(); /*從鍵盤讀取一個字符,但不顯示于屏幕*/
system("cls"); /*調用DOS的清屏函數,TC中可用clrscr代替*/
while(1) /*主菜單*/
{
textcolor(GREEN);
printf("\n\n\n\n\n");
printf("\t\t\t************************************\n");
printf("\t\t\t**\tF2 –shu ri shu ju bin bao cun輸入數據并存入文件 **\n");
printf("\t\t\t**\tF3 –search_xue hao根據學號查詢成績 **\n");
printf("\t\t\t**\tF5 –shu ri wen jian ri ren輸出文件內容 **\n");
printf("\t\t\t**\tF6 –tong ji jige ren shu he you xiu ren shu統計及格和優秀人數 **\n");
printf("\t\t\t**\tESC--END退出系統 **\n");
printf("\t\t\t************************************\n");
key=GetKey(); /*調用自定義函數,讀取一個鍵*/
switch(key)
{
case F2: CreatFile(); break;
case F3: Search_Xuehao(); break;
case F5: ListFile(); break;
case F6: Statistics(); break;
case ESC:exit(1); break;
/*default: puts("\t\t\terror xuan xiang!!輸入錯誤選項!");
printf("\t\t\t按任意鍵返回 an any jian...");
getch();*/
}
clrscr(); /*每執行完一項功能后,自動清屏*/
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -