功能要求
(1)能够从外部数据文件读入学生信息,并以数据文件的形式存储学生信息;
(2)能够增加学生信息,按学号修改、删除学生的信息;
(3)能够按学号、姓名等方式查询学生信息;
(4)能够根据条件对学生信息进行排序浏览;
(5)能够根据条件统计学生人数;
(6)查看所有学生信息。
函数结构体宏定义和声明
#include #include #include #include #include//头文件的引用//#define LEN sizeof(struct student)#define FORMAT "%-8d%-15s%-12.1lf%-12.1lf%-12.1lf%-12.1lf\n"#define DATA stu[i].num,stu[i].name,stu[i].elec,stu[i].expe,stu[i].requ,stu[i].sumvoid in();void show();void order();void del();void modify();void menu();void insert();void total();void search(); //函数的声明// struct student //声明定义结构体//{int num;char name[15];double elec;double expe;double requ;double sum; };struct student stu[50];
主函数的设计
//实现主函数的设计// int main(int argc,const char *argv[],const char *env[]){system("color f0\n"); /*白底黑字*/ int n;menu();scanf("%d",&n);while(n){switch(n){case 1:in();break;case 2:search();break;case 3:del();break;case 4:modify();break;case 5:insert();break;case 6:order();break;case 7:total();break;case 8:show(); break;default:break;}getch();/*执行完功能再次显示菜单界面*/ menu();scanf("%d",&n);} }
菜单界面的实现
自定义函数实现菜单功能*/void menu() {system("cls");printf("\n\n\n\n");printf("\t\t|---------------学生信息管理系统---------------|\n");printf("\t\t|\t\t\t\t\t |\n");printf("\t\t|\t\t 1.录入学生信息|\n");printf("\t\t|\t\t 2.查找学生信息\t |\n"); printf("\t\t|\t\t 3.删除学生信息\t |\n");printf("\t\t|\t\t 4.修改学生信息\t |\n");printf("\t\t|\t\t 5.插入学生信息\t |\n");printf("\t\t|\t\t 6.排序\t |\n");printf("\t\t|\t\t 7.统计学生总数\t |\n");printf("\t\t|\t\t 8.显示所有学生信息\t |\n");printf("\t\t|\t\t 0.退出系统\t |\n");printf("\t\t|\t\t\t |\n");printf("\t\t|----------------------------------------------|\n\n");printf("\t\t\t请选择(0-8):");}
实现录入学生信息
void in(){int i,m=0;char ch[2];FILE*fp;if((fp=fopen("data.txt","a+"))==NULL){printf("文件不存在!\n");return;}while(!feof(fp)){if(fread(&stu[m],LEN,1,fp)==1){m++;}}fclose(fp);if(m==0){printf("文件中没有内容!\n"); }else { show(); } if((fp=fopen("data.txt","wb"))==NULL) { printf("文件不存在!\n"); return; } printf("输入学生信息(y/n):"); scanf("%s",ch); while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) {printf("number:"); scanf("%d",&stu[m].num); for(i=0;i<m;i++)if(stu[i].num==stu[m].num){printf("number已经存在了,按任意键继续!");getch();fclose(fp);return;}printf("name:");scanf("%s",stu[m].name);printf("elective:");scanf("%lf",&stu[m].elec);printf("experiment:");scanf("%lf",&stu[m].expe);printf("required course:");scanf("%lf",&stu[m].requ);stu[m].sum=stu[m].elec+stu[m].expe+stu[m].requ;if(fwrite(&stu[m],LEN,1,fp)!=1){printf("不能保存!");getch(); }else { printf("%s被保存!\n",stu[m].name); m++; }printf("继续?(y/n):");scanf("%s",ch); } fclose(fp); printf("OK!\n");}
实现查询学生信息
//查询没有记录的文件//void search(){FILE*fp;int snum,i,m=0;if((fp=fopen("data.txt","rb"))==NULL){printf("文件不存在!\n");return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1)m++;fclose(fp);if(m==0){printf("文件没有记录!\n");return;}printf("请输入number:");scanf("%d",&snum);for(i=0;i<m;i++) if(snum==stu[i].num) {printf("number nameelectiveexperiment required sum\t\n");printf(FORMAT,DATA);break;}if(i==m)printf("没有找到这名学生!\n");}
实现删除学生信息
void del(){FILE*fp;int snum,i,j,m=0;char ch[2];if((fp=fopen("data.txt","r+"))==NULL)/*data.txt文件不存在*/{printf("文件不存在!\n");return;} while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1)m++; fclose(fp); if(m==0){ printf("文件夹中没有记录!\n"); /*data.txt文件存在,但里面没有内容*/return; }printf("请输入学生学号:");scanf("%d",&snum);for(i=0;i<m;i++)if(snum==stu[i].num){printf("找到了这条记录,是否删除?(y/n)");scanf("%s",ch);if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)//判断是否要进行删除{for(j=i;j<m;j++)stu[j]=stu[j+1];/*将后一个记录移到前一个记录的位置*/m--;if((fp=fopen("data.txt","wb"))==NULL){printf("文件不存在\n");return; } for(j=0;j<m;j++) /*将更改后的记录重新写入指定的磁盘文件中*/ if(fwrite(&stu[j],LEN,1,fp)!=1) { printf("can not save!\n"); getch();}fclose(fp);printf("删除成功!\n"); }else{printf("找到了记录,选择不删除!"); }break;}else{printf("没有找到这名学生!\n"); /*未找到要查找的信息*/}}
实现修改学生信息
void modify()/*自定义修改函数*///修正:要修改文件中没有记录的学号时,还是说“找到了。。。” {struct student t;FILE *fp;int i=0,j=0,m=0,snum; if((fp=fopen("data.txt","r+"))==NULL){ printf("文件不存在!\n");return;}while(!feof(fp)) if(fread(&stu[m] ,LEN,1,fp)==1) m++;if(m==0) {printf("文件中没有记录!\n");fclose(fp);return;}show();printf("请输入要修改的学生number: ");scanf("%d",&snum);for(i=0;i<m;i++)if(snum==stu[i].num)/*检索记录中是否有要修改的信息*/{printf("找到了这名学生,可以修改他的信息!\n"); printf("name:");scanf("%s",stu[i].name);/*输入名字*/printf("elective:");scanf("%lf",&stu[i].elec);/*输入选修课成绩*/printf("experiment:");scanf("%lf",&stu[i].expe);/*输入实验课成绩*/printf("required course:");scanf("%lf",&stu[i].requ);/*输入必修课成绩*/printf("修改成功!");stu[i].sum=stu[i].elec+stu[i].expe+stu[i].requ;if((fp=fopen("data.txt","wb"))==NULL) {printf("can not open\n"); return;}for(j=0;j<m;j++)/*将新修改的信息写入指定的磁盘文件中*/if(fwrite(&stu[j] ,LEN,1,fp)!=1) {printf("can not save!");getch(); } fclose(fp); break;}if(i==m) printf("没有找到这名学生!\n");/*未找到要查找的信息*/}
实现插入学生信息
//实现插入学生信息// void insert()/*自定义插入函数*/{ FILE *fp;int i,j,k,m=0,snum;if((fp=fopen("data.txt","r+"))==NULL){ printf("文件不存在!\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1)m++;if(m==0) {printf("文件中没有记录!\n");fclose(fp);return;}printf("请输入要插入的位置(number):\n");scanf("%d",&snum);/*输入要插入的位置*/for(i=0;ii;j--) stu[j+1]=stu[j];/*从最后一条记录开始均向后移一位*/printf("现在请输入要插入的学生信息.\n");printf("number:");scanf("%d",&stu[i+1].num);for(k=0;k<m;k++)if(stu[k].num==stu[m].num){printf("number已经存在,按任意键继续!");getch();fclose(fp);return;}printf("name:");scanf("%s",stu[i+1].name);printf("elective:");scanf("%lf",&stu[i+1].elec);printf("experiment:");scanf("%lf",&stu[i+1].expe);printf("required course:");scanf("%lf",&stu[i+1].requ);stu[i+1].sum=stu[i+1].elec+stu[i+1].expe+stu[i+1].requ;printf("插入成功!按任意键返回主界面!"); if((fp=fopen("data.txt","wb"))==NULL){printf("不能打开!\n"); return;}for(k=0;k<=m;k++)if(fwrite(&stu[k] ,LEN,1,fp)!=1)/*将修改后的记录写入磁盘文件中*/ {printf("不能保存!");getch(); }fclose(fp);}
实现学生排名
//实现学生排名//void order(){FILE *fp;struct student t;int i = 0,j = 0,m = 0;if((fp=fopen("data.txt","r+"))==NULL){printf("文件不存在!\n");return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1)m++;fclose(fp);if(m==0){printf("文件中没有记录!\n");return;}if((fp=fopen("data.txt","wb"))==NULL){printf("文件不存在!\n");return;}for(i=0;i<m-1;i++)for(j=i+1;j<m;j++)if(stu[i].sum<stu[j].sum){t=stu[i];stu[i]=stu[j];stu[j]=t;} if((fp = fopen("data.txt","wb"))==NULL) { printf("文件不存在!\n"); return; }for(i=0;i<m;i++)if(fwrite(&stu[i],LEN,1,fp)!=1){printf("%s 不能保存文件!\n");getch();}fclose(fp);printf("保存成功!\n");}
实现统计学生个数
//实现统计学生个数// void total(){ FILE *fp;int m=0;if((fp=fopen("data.txt","r+"))==NULL) {printf("文件不存在!\n"); return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1) m++;/*统计记录个数即学生个数*/if(m==0) {printf("no record!\n");fclose(fp);return;}printf("这个班级一共有 %d 名学生!\n",m);/*将统计的个数输出*/fclose(fp);}
实现显示学生信息表
//实现统计学生个数// void total(){ FILE *fp;int m=0;if((fp=fopen("data.txt","r+"))==NULL) {printf("文件不存在!\n"); return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1) m++;/*统计记录个数即学生个数*/if(m==0) {printf("no record!\n");fclose(fp);return;}printf("这个班级一共有 %d 名学生!\n",m);/*将统计的个数输出*/fclose(fp);}
源代码展示
#include #include #include #include #include//头文件的引用//#define LEN sizeof(struct student)#define FORMAT "%-8d%-15s%-12.1lf%-12.1lf%-12.1lf%-12.1lf\n"#define DATA stu[i].num,stu[i].name,stu[i].elec,stu[i].expe,stu[i].requ,stu[i].sumvoid in();void show();void order();void del();void modify();void menu();void insert();void total();void search(); //函数的声明// struct student //声明定义结构体//{int num;char name[15];double elec;double expe;double requ;double sum; };struct student stu[50]; //实现主函数的设计// int main(int argc,const char *argv[],const char *env[]){system("color f0\n"); /*白底黑字*/ int n;menu();scanf("%d",&n);while(n){switch(n){case 1:in();break;case 2:search();break;case 3:del();break;case 4:modify();break;case 5:insert();break;case 6:order();break;case 7:total();break;case 8:show(); break;default:break;}getch(); /*执行完功能再次显示菜单界面*/ menu();scanf("%d",&n);} }/*** 自定义函数实现菜单功能*/void menu() {system("cls");printf("\n\n\n\n");printf("\t\t|---------------学生信息管理系统---------------|\n");printf("\t\t|\t\t\t\t\t |\n");printf("\t\t|\t\t 1.录入学生信息|\n");printf("\t\t|\t\t 2.查找学生信息\t |\n"); printf("\t\t|\t\t 3.删除学生信息\t |\n");printf("\t\t|\t\t 4.修改学生信息\t |\n");printf("\t\t|\t\t 5.插入学生信息\t |\n");printf("\t\t|\t\t 6.排序\t |\n");printf("\t\t|\t\t 7.统计学生总数\t |\n");printf("\t\t|\t\t 8.显示所有学生信息\t |\n");printf("\t\t|\t\t 0.退出系统\t |\n");printf("\t\t|\t\t\t |\n");printf("\t\t|----------------------------------------------|\n\n");printf("\t\t\t请选择(0-8):");}//录入学生信息//void in(){int i,m=0;char ch[2];FILE*fp;if((fp=fopen("data.txt","a+"))==NULL){printf("文件不存在!\n");return;}while(!feof(fp)){if(fread(&stu[m],LEN,1,fp)==1){m++;}}fclose(fp);if(m==0){printf("文件中没有内容!\n"); }else { show(); } if((fp=fopen("data.txt","wb"))==NULL) { printf("文件不存在!\n"); return; } printf("输入学生信息(y/n):"); scanf("%s",ch); while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) {printf("number:"); scanf("%d",&stu[m].num); for(i=0;i<m;i++)if(stu[i].num==stu[m].num){printf("number已经存在了,按任意键继续!");getch();fclose(fp);return;}printf("name:");scanf("%s",stu[m].name);printf("elective:");scanf("%lf",&stu[m].elec);printf("experiment:");scanf("%lf",&stu[m].expe);printf("required course:");scanf("%lf",&stu[m].requ);stu[m].sum=stu[m].elec+stu[m].expe+stu[m].requ;if(fwrite(&stu[m],LEN,1,fp)!=1){printf("不能保存!");getch(); }else { printf("%s被保存!\n",stu[m].name); m++; }printf("继续?(y/n):");scanf("%s",ch); } fclose(fp); printf("OK!\n");}//查询没有记录的文件//void search(){FILE*fp;int snum,i,m=0;if((fp=fopen("data.txt","rb"))==NULL){printf("文件不存在!\n");return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1)m++;fclose(fp);if(m==0){printf("文件没有记录!\n");return;}printf("请输入number:");scanf("%d",&snum);for(i=0;i<m;i++) if(snum==stu[i].num) {printf("number nameelectiveexperiment required sum\t\n");printf(FORMAT,DATA);break;}if(i==m)printf("没有找到这名学生!\n");} //实现删除学生信息//void del(){FILE*fp;int snum,i,j,m=0;char ch[2];if((fp=fopen("data.txt","r+"))==NULL)/*data.txt文件不存在*/{printf("文件不存在!\n");return;} while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1)m++; fclose(fp); if(m==0){ printf("文件夹中没有记录!\n"); /*data.txt文件存在,但里面没有内容*/return; }printf("请输入学生学号:");scanf("%d",&snum);for(i=0;i<m;i++)if(snum==stu[i].num){printf("找到了这条记录,是否删除?(y/n)");scanf("%s",ch);if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)//判断是否要进行删除{for(j=i;j<m;j++)stu[j]=stu[j+1];/*将后一个记录移到前一个记录的位置*/m--;if((fp=fopen("data.txt","wb"))==NULL){printf("文件不存在\n");return; } for(j=0;j<m;j++) /*将更改后的记录重新写入指定的磁盘文件中*/ if(fwrite(&stu[j],LEN,1,fp)!=1) { printf("can not save!\n"); getch();}fclose(fp);printf("删除成功!\n"); }else{printf("找到了记录,选择不删除!"); }break;}else{printf("没有找到这名学生!\n"); /*未找到要查找的信息*/}} //实现修改学生信息 //void modify()/*自定义修改函数*///修正:要修改文件中没有记录的学号时,还是说“找到了。。。” {FILE *fp;struct student t;int i=0,j=0,m=0,snum; if((fp=fopen("data.txt","r+"))==NULL){ printf("文件不存在!\n");return;}while(!feof(fp)) if(fread(&stu[m] ,LEN,1,fp)==1) m++;if(m==0) {printf("文件中没有记录!\n");fclose(fp);return;}show();printf("请输入要修改的学生number: ");scanf("%d",&snum);for(i=0;i<m;i++)if(snum==stu[i].num)/*检索记录中是否有要修改的信息*/{printf("找到了这名学生,可以修改他的信息!\n"); printf("name:");scanf("%s",stu[i].name);/*输入名字*/printf("elective:");scanf("%lf",&stu[i].elec);/*输入选修课成绩*/printf("experiment:");scanf("%lf",&stu[i].expe);/*输入实验课成绩*/printf("required course:");scanf("%lf",&stu[i].requ);/*输入必修课成绩*/printf("修改成功!");stu[i].sum=stu[i].elec+stu[i].expe+stu[i].requ;if((fp=fopen("data.txt","wb"))==NULL) {printf("can not open\n"); return;}for(j=0;j<m;j++)/*将新修改的信息写入指定的磁盘文件中*/if(fwrite(&stu[j] ,LEN,1,fp)!=1) {printf("can not save!");getch(); } fclose(fp); break;}if(i==m) printf("没有找到这名学生!\n");/*未找到要查找的信息*/}//实现插入学生信息// void insert()/*自定义插入函数*/{ FILE *fp;int i,j,k,m=0,snum;if((fp=fopen("data.txt","r+"))==NULL){ printf("文件不存在!\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1)m++;if(m==0) {printf("文件中没有记录!\n");fclose(fp);return;}printf("请输入要插入的位置(number):\n");scanf("%d",&snum);/*输入要插入的位置*/for(i=0;ii;j--) stu[j+1]=stu[j];/*从最后一条记录开始均向后移一位*/printf("现在请输入要插入的学生信息.\n");printf("number:");scanf("%d",&stu[i+1].num);for(k=0;k<m;k++)if(stu[k].num==stu[m].num){printf("number已经存在,按任意键继续!");getch();fclose(fp);return;}printf("name:");scanf("%s",stu[i+1].name);printf("elective:");scanf("%lf",&stu[i+1].elec);printf("experiment:");scanf("%lf",&stu[i+1].expe);printf("required course:");scanf("%lf",&stu[i+1].requ);stu[i+1].sum=stu[i+1].elec+stu[i+1].expe+stu[i+1].requ;printf("插入成功!按任意键返回主界面!"); if((fp=fopen("data.txt","wb"))==NULL){printf("不能打开!\n"); return;}for(k=0;k<=m;k++)if(fwrite(&stu[k] ,LEN,1,fp)!=1)/*将修改后的记录写入磁盘文件中*/ {printf("不能保存!");getch(); }fclose(fp);}//实现学生排名//void order(){FILE *fp;struct student t;int i = 0,j = 0,m = 0;if((fp=fopen("data.txt","r+"))==NULL){printf("文件不存在!\n");return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1)m++;fclose(fp);if(m==0){printf("文件中没有记录!\n");return;}if((fp=fopen("data.txt","wb"))==NULL){printf("文件不存在!\n");return;}for(i=0;i<m-1;i++)for(j=i+1;j<m;j++)if(stu[i].sum<stu[j].sum){t=stu[i];stu[i]=stu[j];stu[j]=t;} if((fp = fopen("data.txt","wb"))==NULL) { printf("文件不存在!\n"); return; }for(i=0;i<m;i++)if(fwrite(&stu[i],LEN,1,fp)!=1){printf("%s 不能保存文件!\n");getch();}fclose(fp);printf("保存成功!\n");}//实现统计学生个数// void total(){ FILE *fp;int m=0;if((fp=fopen("data.txt","r+"))==NULL) {printf("文件不存在!\n"); return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1) m++;/*统计记录个数即学生个数*/if(m==0) {printf("no record!\n");fclose(fp);return;}printf("这个班级一共有 %d 名学生!\n",m);/*将统计的个数输出*/fclose(fp);}//读取显示所以学生信息// void show(){FILE*fp;int i,m=0;fp=fopen("data.txt","rb");while(!feof(fp)){if(fread(&stu[m],LEN,1,fp)==1)m++;}fclose(fp);printf("number name elective experiment required sum\t\n");for(i=0;i<m;i++){printf(FORMAT,DATA);}}
结语及经验总结
问题:1.运行程序时变量名称输入错误;
2.运行程序时部分功能实现的异常;
3.功能实现时思路的问题;
总结:
开发程序真正需要的是团队协作,协作提升效率。问题往往发现于他人,切莫眼高手低,相信自己,更要相信他人相信团队,庞大的问题细分每个成员,会变得游刃有余。合理分工,共克难题,这便是开发设计之道。