学生成绩管理系统-vs汇编

  • 格式:docx
  • 大小:22.55 KB
  • 文档页数:10

#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<dos.h>#include<string.h>#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].sum float Ielec,Iexpe,Irequ;float Felec,Fexpe,Frequ;struct student/*定义学生成绩结构体*/{int num;/*学号*/char name[15];/*姓名*/double elec;/*选修课*/double expe;/*实验课*/double requ;/*必修课*/double sum;/*总分*/};struct student stu[50];/*定义结构体数组*/void input();/*录入学生成绩信息*/void show();/*显示学生信息*/void sort();/*按总分排序*/void del();/*删除学生成绩信息*/void modify();/*修改学生成绩信息*/void menu();/*主菜单*/void insert();/*插入学生信息*/void total();/*计算总人数*/void search();/*查找学生信息*/main(){int n;menu();scanf("%d",&n);while(n){switch(n){case 1: input();break;case 2: search();break;case 3: del(); break;case 4: modify();break;case 5: insert();break;case 6: sort();break;case 7: total();break;default: break;}menu();scanf("%d",&n);}}void show(){FILE *fp;int i,m=0;fp=fopen("d:\\data.txt","ab+");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);/*将信息按指定格式打印*/}}void menu(){system("cls");printf("\n");printf("\n");printf("\t|--------------------Student management system--------------------|\n");printf("\t| |\n");printf("\t| 0. exit |\n");printf("\t| |\n");printf("\t| 1. input record |\n");printf("\t| |\n");printf("\t| 2. search record |\n");printf("\t| |\n");printf("\t| 3. delete record |\n");printf("\t| |\n");printf("\t| 4. modify record |\n");printf("\t| |\n");printf("\t| 5. insert record |\n");printf("\t| |\n");printf("\t| 6. order |\n");printf("\t| |\n");printf("\t| 7. number |\n");printf("\t| |\n");printf("\t|-----------------------------------------------------------------|\n"); printf("\n");printf("\n");printf(" choose( 0 ~ 7 ):");}void input(){int i,m=0;char ch[2];FILE *fp;if((fp=fopen("d:\\data.txt","ab+"))==NULL) /*打开指定文件*/{printf("can not open\n");return; /*返回主函数*/}while(!feof(fp)) /*feof()函数是用来判断指针是否已经到达文件尾部的,若指针指向文件末尾,则返回值为“真”,返回。

*/ {if(fread(&stu[m],LEN,1,fp)==1)m++; /*统计当前记录条数*/}fclose(fp);if(m==0)printf("No record!\n");else{system("cls");show(); /*调用show函数,显示原有信息*/}if((fp=fopen("d:\\data.txt","wb"))==NULL){printf("can not open\n");return;}for(i=0;i<m;i++)fwrite(&stu[m],LEN,1,fp); /*向指定磁盘文件写入信息*/printf("please input (y/n):/n");scanf("%s",&ch);if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0){printf("please input per centum:");printf("\nelective:");scanf("%f",&Ielec);printf("/nexperiment:");scanf("%f",&Iexpe);printf("/nrequired course:");scanf("%f",&Irequ);}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("the number is existing,press any to continue!");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*Ielec+stu[m].expe*Iexpe+stu[m].requ*Irequ; /*计算出总成绩*/if(fwrite(&stu[m],LEN,1,fp)!=1) /*将新录入的信息写入指定的磁盘文件*/{printf("can not save!");getch();}else{printf("%s saved!\n",stu[m].name);m++;}printf("continue?(y/n):"); /*询问是否继续*/scanf("%s",ch);}fclose(fp);printf("OK!\n");}void search() /*自定义查找函数*/{FILE *fp;int snum,i,m=0;char ch[2];if((fp=fopen("d:\\data.txt","ab+"))==NULL){printf("can not open\n");return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1)m++;if(m==0){printf("no record!\n");return;}printf("please input the number:");scanf("%d",&snum); /*输入需要查找学生的学号*/for(i=0;i<m;i++)if(snum==stu[i].num) /**查找出入的学号是否在记录中*/{printf("find the student,show?(y/n)");scanf("%s",ch);if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0){printf("number name elective experiment required sum\t\n");printf(FORMAT,DATA); /*将查找出得结果按指定格式输出*/break;}}if(i==m)printf("can not find the student!\n"); /*没有找到要查额信息*/ system("pause");}void modify() /*自定义修改函数*/{int i,j,snum,m=0;FILE *fp;if((fp=fopen("d:\\data.txt","ab+"))==NULL){printf("can not open\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("please input the number of the student which do you want to modify!\n");scanf("%d",&snum);for(i=0;i<m;i++)if(snum==stu[i].num) /*检索记录中是否有要修改的信息*/break;if(i<m){printf("find the student! you can modify now!\n");printf("please input per centnm:");printf("\nelective:");scanf("%f",&Ielec);printf("\nexperiment:");scanf("%f",&Iexpe);printf("\nrequired course:");scanf("%f",&Irequ);printf("name:\n");scanf("%s",&stu[i].name);printf("\nelective:");scanf("%lf",&stu[i].elec);printf("\nexperiment:");scanf("%lf",&stu[i].expe);printf("\nrequired course:");scanf("%lf",&stu[i].requ);stu[i].sum=stu[i].elec*Ielec+stu[i].expe*Iexpe+stu[i].requ*Irequ;}else{printf("can not find!");getch();return;}if((fp=fopen("d:\\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);}void del() /*自定义删除函数*/{int snum,i,j,m=0;char ch[2];FILE *fp;if((fp=fopen("d:\\data.txt","ab+"))==NULL){printf("can not open\n");return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1)m++;fclose(fp);if(m==0){printf("no record!\n");return;}printf("please input the number:");scanf("%d",&snum);for(i=0;i<m;i++)if(snum=stu[i].num)break;printf("find the student,delete?(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("d:\\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!\n");getch();}fclose(fp);printf("delete successfully!\n");}void insert() /*自定义插入函数*/{int i,j,k,snum,m=0;FILE *fp;if((fp=fopen("d:\\data.txt","ab+"))==NULL){printf("can not open\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("please input the position where you want to insert!\n");scanf("%d",&snum); /*输入要插入的位置*/for(i=0;i<m;i++)if(snum==stu[i].num)break;for(j=m-1;j>i;j--)stu[j+1]=stu[j]; /*从最后一条记录开始均向后移一位*/ printf("please input the new information:\n");printf("number:");scanf("%d",&stu[i+1].num);for(k=0;k<m;k++)if(stu[k].num==stu[i+1].num&&k!=i+1){printf("the number is existing,press any to continue!");getch();fclose(fp);return;}printf("please input per centum:");printf("\nelective:");scanf("%f",&Felec);printf("\nexperiment:");scanf("%f",&Fexpe);printf("\nrequired course:");scanf("%f",&Frequ);printf("name:\n");scanf("%s",&stu[i+1].name);printf("\nelective:");scanf("%lf",&stu[i+1].elec);printf("\nexperiment:");scanf("%lf",&stu[i+1].expe);printf("\nrequired course:");scanf("%lf",&stu[i+1].requ);stu[i+1].sum=stu[i+1].elec*Felec+stu[i+1].expe*Fexpe+stu[i+1].requ*Frequ; /*计算总成绩*/if((fp=fopen("d:\\data.txt","wb"))==NULL){printf("can not open\n");return;}for(k=0;k<=m;k++)if(fwrite(&stu[k],LEN,1,fp)!=1) /*将修改后的记录写入磁盘中*/{printf("can not save!");getch();}fclose(fp);}void sort() /*自定义排名函数*/{struct student temp;FILE *fp;int i,j,m=0;if((fp=fopen("d:\\data.txt","ab+"))==NULL) /*判断文件是否打开成功*/ {printf("can not open\n");return;}while(!feof(fp))if(fread(&stu[m],LEN,1,fp)==1)m++; /*统计学生个数*/ if(m==0){printf("no record!\n");fclose(fp);return;}for(i=0;i<m-1;i++)for(j=i+1;j<m;j++)if(stu[j].sum>stu[i].sum){temp=stu[i];stu[i]=stu[j];stu[j]=temp;}for(i=0;i<m;i++)if(fwrite(&stu[i],LEN,1,fp)!=1){printf("can not save!");getch();return;}fclose(fp);}void total() /*自定义统计学生人数函数*/{FILE *fp;int m=0;if((fp=fopen("d:\\data.txt","ab+"))==NULL) /*判断文件是否打开成功*/ {printf("can not open\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("the class are %d students!\n"); /*将统计的个数输出*/fclose(fp);}。