/*-------------1-------------*/
#include
#include
#include
#include
#include
#include
#include
#include
#define NULL 0
#define ESC 0x001b /* 退出 */
#define F1 0x3b00 /* 查看帮助信息,调用HelpMassage()函数 */
#define F2 0x3c00 /*输入学生成绩*/
#define F3 0x3d00 /*按学号查找*/
#define F4 0x3e00 /*按姓名查找*/
#define F5 0x3f00 /*列出所有学生成绩*/
#define F6 0x4000 /*统计*/
struct stuType /*定义结构体变量*/
{
char NO[11]; /*学号长度为10*/
char XM[10];
char AGE[2]; /*年龄长度为2*/
float CJ[4]; /*包含4门成绩*/
};
/*-------------2-------------*/
int JY_NO(char *stu_num,FILE *fp) /*检验学号的正确性*/
{ struct stuType stud;
int NO;
char *p=stu_num;
if(strcmp(stu_num,"#")==0) return 1; /*若输入"#"返回真值,不再循环输入*/
while(*p!='\0') /*学号必须是数字,否则返回重新输入*/
{ NO=(int)*p;
if(NO<48||NO>57)
{ puts("\t\tUndefined SN!Please input again!\n");/*非法学号!请重新输入!*/
return 0;
}
else p++; /*指针加1*/
}
if(strlen(stu_num)!=10) /*若学号长度不为10,则返回重新输入*/
{ puts("\t\tLength of SN Error!\n");/*学号长度不对*/
return 0;
}
if(getchar()!='\n') /*若学号后面的字符不是回车符,则学号长度大于10*/
{ printf("\t\tThe length of SN should more than 10!Please input again!\n");/*学号长度大于10个,请重新输入*/
do{}while(getchar()!='\n'); /*用getchar接收多余的字符*/
return 0;
}
else
{
rewind(fp); /*使文件指针指向头*/
while(!feof(fp)) /*若文件指针未到结尾,就继续执行下面的循环,feof遇到文件结束符返回非零值,否则返回0*/
{ fread(&stud,sizeof(struct stuType),1,fp); /*读取一定长度的数据*/
if(strcmp(stu_num,stud.NO)==0) /*学号的唯一性*/
{ printf("\t\t Repeat SN,Please input again!\n");/*学号重复!请重新输入*/
printf("\t\tThe Record of this student:\n");/*该学生成绩如下*/
printf("\t\tChinese:%.1f\n",stud.CJ[0]);/*语文*/
printf("\t\tMath:%.1f\n",stud.CJ[1]);/*数学*/
printf("\t\tEnglish:%.1f\n",stud.CJ[2]);/*英语*/
printf("\t\tTotal:%.1f\n",stud.CJ[3]);/*总评*/
return 0;
}
}
}
return 1;
}
/*-------------3-------------*/
int JY_NO2(char *stu_num) /*检验学号*/
{ int NO;
char *p=stu_num;
if(strcmp(stu_num,"#")==0)return 1; /*若输入“#”,则返回真值结束*/
if(strlen(stu_num)!=10) /*学号长度不为10*/
{ puts("\t\tLength of SN Error!\n");/*学号长度不对*/
return 0;
}
while(*p!='\0') /*学号必须用数字,若包含有字母,或其它字符则返回假值重新输入*/
{ NO=(int)*p;
if(NO<48||NO>57)
{ puts("\t\tUndefined SN!Please input again!\n");/*非法学号!请重新输入*/
return 0;
}
else p++; /*指针加1*/
}
if(getchar()!='\n') /*检验学号长度是否大于10,并把多余的字符去掉*/
{ printf("\t\tThe length of SN should more than 10!Please input again!\n");/*学号长度大于10个,请重新输入*/
do{}while(getchar()!='\n');
return 0;
}
return 1;
}
/*-------------4-------------*/
int JY_XM(char *stu_XM) /*检验姓名*/
{ int PD;
char *p;
p=stu_XM;
while(*p!='\0') /*姓名只能用英文*/
{
PD=(int)*p;
if(PD<0)
{ puts("\t\tName should use English, Please input again!\n");/*姓名只能用英文请重新输入*/
return 0;
}
else p++; /*使指针加1,指向下一汉字*/
}
if(getchar()!='\n') /*姓名长度不得大于5个*/
{ printf("\t\tThe length of Name is more than 5, Please input again!\n");/*姓名长度大于5个请重新输入*/
do{}while(getchar()!='\n');
return 0;
}
return 1; /*字符串全为汉字返回真*/
}
/*-------------5-------------*/
int JY_AGE(float stu_AGE) /*年龄只能在0~100之间*/
{
if(stu_AGE<0||stu_AGE>100)
{ printf("\t\tInput Error! Record should between 0~100!\n");/*输入错误,成绩只能在0~100之间*/
return 0;
}
return 1;
}
/*-------------6-------------*/
int JY_CJ(float stu_CJ) /*学生成绩只能在0~100之间*/
{
if(stu_CJ<0||stu_CJ>100)
{ printf("\t\tInput Error! Record should between 0~100!\n");/*输入错误,成绩只能在0~100之间*/
return 0;
}
return 1;
}
/*-------------7-------------*/
void CreatFile() /*输入文件*/
{ FILE *fp;
struct stuType stu,stu0={"","",}; /*对stu0xianfuzhi*/
fp=fopen("stu.dat","wb+"); /*打开或创建一个二进制文件,打开时将原来的内容删除*/
if(fp==NULL)
{ printf("\t\tFile opens Error!\n\t\t\tPress Anykey back...");/*文件打开失败,按任意键返回*/
getch();
return;
}
else
{ while(1)
{ stu=stu0;
do{ printf("\n\t\tPlease input SN:"); /*输入学号并检验其正确性*/
scanf("%10s",stu.NO);
}while(!JY_NO(stu.NO,fp));
if(strcmp(stu.NO,"#")==0)break;
do{ printf("\n\t\tPlease input Name:"); /*输入姓名并检验其正确性*/
scanf("%10s",stu.XM);
}while(!JY_XM(stu.XM));
do{ printf("\n\t\tPlease input age:"); /*输入年龄绩并检验其正确性*/
scanf("%f",&stu.AGE);
}while(!JY_AGE(stu.AGE[0]));
do{ printf("\n\t\tPlease input record of Chinese:"); /*输入成绩并检验其正确性*/
scanf("%f",&stu.CJ[0]);
}while(!JY_CJ(stu.CJ[0]));
do{ printf("\n\t\tPlease input record of Math:"); /*同上*/
scanf("%f",&stu.CJ[1]);
}while(!JY_CJ(stu.CJ[1]));
do{ printf("\n\t\tPlease input record of English:");
scanf("%f",&stu.CJ[2]);
}while(!JY_CJ(stu.CJ[2]));
do{ printf("\n\t\tPlease input record of Total:");
scanf("%f",&stu.CJ[3]);
}while(!JY_CJ(stu.CJ[3]));
fwrite(&stu,sizeof(struct stuType),1,fp); /*写文件*/
}
}
fclose(fp); /*关闭文件*/
}
/*-------------8-------------*/
void Search_Xuehao() /*按学号查询*/
{ FILE *fp;
int flag;
struct stuType stu,stud;
fp=fopen("stu.dat","rb");
if(fp==NULL) /*若文件打不开则输出下面的信息*/
{ printf("\t\tFile opens Error!\n\t\t\tPress Anykey back...");
getch();
return;
}
else
{ do{ puts("\n\t\tPress\"#\" or search");
do{ printf("\t\tPlease input the SN what you want:");
scanf("%10s",stu.NO);
}while(!JY_NO2(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\tThe Record of this student:");
printf("\t\tSN:%s\n",stud.NO);
printf("\t\tName:%s\n",stud.XM);
printf("\t\tAGE:%.1f\n",stud.AGE[0]);
printf("\t\tChinese:%.1f\n",stud.CJ[0]);
printf("\t\tMath:%.1f\n",stud.CJ[1]);
printf("\t\tEnglish:%.1f\n",stud.CJ[2]);
printf("\t\tTotal:%.1f\n",stud.CJ[3]);
flag=1; /*记录学号是否查到*/
}
}
if(flag==0)puts("\t\tUndefined SN");
}while(strcmp(stu.NO,"#")!=0);
}
fclose(fp); /*关闭文件*/
}
/*-------------9-------------*/
void Search_Xingming() /*按姓名查找*/
{ FILE *fp;
int flag=0;
struct stuType stu,stud;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{ printf("\t\tFile opens Error!\n\t\tPress Anykey back...");
getch();
return;
}
else
{ do{
do{ printf("\t\tPlease input the Name of the Student what you want:");
scanf("%10s",stu.XM);
}while(!JY_XM(stu.XM));
rewind(fp); /*文件指针指向头*/
while(fread(&stud,sizeof(struct stuType),1,fp))
{ if(strcmp(stu.XM,stud.XM)==0) /*比较姓名是否相同*/
{ puts("\t\tThe name of the Student is:");
printf("\t\tSN:%s\n",stud.NO);
printf("\t\tName:%s\n",stud.XM);
printf("\t\tAGE:%.1f\n",stud.AGE[0]);
printf("\t\tChinese:%.1f\n",stud.CJ[0]);
printf("\t\tMath:%.1f\n",stud.CJ[1]);
printf("\t\tEnglish:%.1f\n",stud.CJ[2]);
printf("\t\tTotal:%.1f\n",stud.CJ[3]);
flag=1; /*记录姓名是否被查到*/
}
}
if(flag==0)puts("\n\t\tUndefined Name!");
puts("\t\tContinue?(y--yes,Else key back)?");
}while(getch()=='y');
}
fclose(fp);
/* puts("\t\tPress Anykey to continue...");*/
/* getch();*/
}
/*-------------10-------------*/
int ListFile(void) /*输出文件,列出所有学生成绩*/
{ FILE *fp;
int REC=0; /*记录学生人数*/
struct stuType stu;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{ printf("\t\tFile opens Error!\n\t\tPress Anykey back...");
getch();
return 1;
}
else{ printf("\t\tRecords of the Students:\n");
printf("\t\tSN\t\tName\tChinese\tMath\tEnglish\tTotal\n");
rewind(fp);
while(fread(&stu,sizeof(struct stuType),1,fp))
{ /*每读取一个长度的数据就输出*/
printf("\t\t%s",stu.NO);
printf("\t%s",stu.XM);
printf("\t%.1f",stu.AGE);
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\tPress Anykey to continue...\n");
getch();
}
}
}
fclose(fp); /*关闭文件*/
printf("\t\tContinue...");
getch();
}
/*-------------11-------------*/
void Statistics() /*统计及格和优秀人数*/
{ FILE *fp;
int REC=0,unpass[4]={0},good[4]={0}; /*REC--记录个数,即人数,unpass--重修人数,good--优秀人数*/
float highest[4]={0},score[4]={0}; /*highest--最高分,score--总分*/
struct stuType stu;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{ printf("\t\tFile opens Error!\n\t\tPress Anykey back...");
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)unpass[0]++;
if(stu.CJ[0]>=80)good[0]++;
if(highest[0]
if(stu.CJ[1]<=60)unpass[1]++;
if(stu.CJ[1]>=80)good[1]++;
if(highest[1]
if(stu.CJ[2]<=60)unpass[2]++;
if(stu.CJ[2]>=80)good[2]++;
if(highest[2]
if(stu.CJ[3]<=60)unpass[3]++;
if(stu.CJ[3]<=80)good[3]++;
if(highest[3]
if(REC==0) /*可以防止记录为0是REC作除数而造成的错误*/
{ printf("\t\tYou did't input the Name!Press Anykey back...");
getch();
return;
}
else{
printf("\t Chinese\t Math\t English\tTotal\n"); /*输出统计信息*/
printf("\tAverage: %.1f\t %.1f\t %.1f\t\t%.1f\n",score[0]/REC,score[1]/REC,score[2]/REC,score[3]/REC);
printf("\tTop record: %.1f\t %.1f\t %.1f\t\t%.1f\n",highest[0],highest[1],highest[2],highest[3]);
printf("\tNumber of good students: %d\t\t %d\t %d\t\t %d\n",good[0],good[1],good[2],good[3]);
printf("\tNumber of unpassed students:%d\t\t %d\t %d\t\t %d\n",unpass[0],unpass[1],unpass[2],unpass[3]);
}
}
fclose(fp);
printf("\n\t\t\tPress Anykey to continue...");
getch();
}
/*-------------12-------------*/
void HelpMessage()
{ clrscr();
printf("\n\n\n\n\n chi xi tong shi lao shi suo bu zhi zuoye bianzhi ercheng,gai xitong juyou cunchu xuesheng shuju,an xuehao,xingming chaxun,liechu xuesheng chengji he tongjigongneng.\n \n shiyongfangfa:xitong shulu shuju hou,jiangzai dangqian muluzhong jianli yige mingwei stu.dat wenjian,yongyubaocun shulu de shuju.xuehao shulu zhineng yong shuzi shulu qie xuehao zhineng shi10wei.xingming shulu fuhe zhongguoren de xingming,zhineng yong zhongwen pinying,qie zuichangwei5 ge hanzhi.\n chi chenxu zai Turbo C2.0xia yunxing tongguo \n\n ");
getch();
}
/*-------------13-------------*/
int GetKey(void) /*此函数返回一个按键的数值*/
{ int key;
key=bioskey(0); /*bioskey为调用BIOS键盘接口*/
if(key<<8) /*位移*/
{
key=key&0x00ff;
}
return key; /*返回按键*/
}
/*-------------14-------------*/
void main()
{ int key;
struct date d; /*定义时间结构体*/
getdate(&d); /*读取系统日期并把它放到结构体d中*/
clrscr(); /*清除屏幕*/
printf("\n\n\n\n\n");
printf("\t ========================================================\n"); /*版本信息*/
printf("\n");
printf("\t\t The System of Students Records Management 1.0 \n");
printf("\n");
printf("\n");
printf("\t\t Builder: XG042-WZM YS HYF WQ \n");
printf("\t\t Teacher:Song Zheyuan\n");
printf("\t\t Time:2005\\7\\5 \n");
printf("\n");
printf("\t ========================================================\n");
printf("\t\t\tPress Anykey to continue...");
/*while(!kbhit());*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
system("cls"); /*调用DOS的清屏函数,TC中可用clrscr代替*/
while(1) /*主菜单*/
{
printf("\n\n\n\n\n");
printf("\t **************************************************\n");
printf("\t **\t **\n");
printf("\t **\tF1 --Help **\n");
printf("\t **\t **\n");
printf("\t **\tF2 --Data Input & Save **\n");
printf("\t **\t **\n");
printf("\t **\tF3 --Search by SN **\n");
printf("\t **\t **\n");
printf("\t **\tF4 --Search by Name **\n");
printf("\t **\t **\n");
printf("\t **\tF5 --Contents Input **\n");
printf("\t **\t **\n");
printf("\t **\tF6 --Count Students Number(Passed&Good) **\n");
printf("\t **\t **\n");
printf("\t **\tESC--Quit the System **\n");
printf("\t **\t **\n");
printf("\t **************************************************\n");
printf("\n\t Please select...\t\t%d\\%d\\%d\n\n",d.da_year,d.da_mon,d.da_day); /*提示信息,并显示当前系统日期*/
key=GetKey(); /*调用自定义函数,读取一个键*/
switch(key)
{
case F1: HelpMessage(); break;
case F2: CreatFile(); break;
case F3: Search_Xuehao(); break;
case F4: Search_Xingming();break;
case F5: ListFile(); break;
case F6: Statistics(); break;
case ESC:exit(1); break;
/*default: puts("\t\t\tWrong SN input!");
printf("\t\t\tPress Anykey back...");
getch();*/
}
clrscr(); /*每执行完一项功能后,自动清屏*/
}
}