发布网友 发布时间:2023-02-08 05:56
共1个回答
热心网友 时间:2023-04-26 19:48
#include "stdio.h" #include "string.h" struct Student { char num[20]; char name[20]; int score; }; /*声明学生数组及学生数量*/ struct Student students[1000]; int n=0; //单个输出 void Student_print(int i) { printf("%10s%10s%8s\n","学号","姓名","分数"); printf("-------------------------------------\n"); printf("%10s%10s%8d",students[i].num,students[i].name,students[i].score); } //显示全部信息 void Student_display() { int i; printf("%10s%10s%8s%\n","学号","姓名","分数"); printf("-------------------------------------\n"); for (i=0;i<n;i++) { printf("%10s%10s%8d\n",students[i].num,students[i].name,students[i].score); } } //排序 void Student_Sortnum() { int i,j; struct Student temp; for (i=0;i<n;i++) { for (j=1;j<n-i;j++) { if (strcmp(students[j-1].num,students[j].num)>0) { temp=students[j-1]; students[j-1]=students[j]; students[j]=temp; } } } Student_display(); } void Student_Sortname() { int i,j; struct Student temp; for (i=0;i<n;i++) { for (j=1;j<n-i;j++) { if (strcmp(students[j-1].name,students[j].name)>0) { temp=students[j-1]; students[j-1]=students[j]; students[j]=temp; } } }Student_display(); } void Student_Sortscore() { int i,j; struct Student temp; for (i=0;i<n;i++) { for (j=1;j<n-i;j++) { if (students[j-1].score<students[j].score) { temp=students[j-1]; students[j-1]=students[j]; students[j]=temp; } } }Student_display(); } //增加学生信息 void Student_add() {int f=1,i; char h[20]; while(1) { printf("请输入学号:"); scanf("%s",&h); for(i=0;i<n;i++) if(strcmp(students[i].num,h)==0) { printf("该学生已存在!");f=0;break;} if(f==0)break; strcpy(students[n].num,h); printf("请输入姓名:"); scanf("%s",&students[n].name); printf("请输入成绩:"); scanf("%d",&students[n].score); getchar(); n++; printf("是否继续?(y/n)"); if (getchar()=='n') { break; } } } //写入学生信息 void write() { FILE *fp; int i; if ((fp=fopen("data.txt","wb"))==NULL) { printf("不能打开文件!\n"); return; } if (fwrite(&n,sizeof(int),1,fp)!=1) { printf("写入文件错误!\n"); } for (i=0;i<n;i++) { if (fwrite(&students[i],sizeof(struct Student),1,fp)!=1) { printf("写入文件错误!\n"); } } fclose(fp); } //读取学生信息 void read() { FILE *fp; int i; if ((fp=fopen("data.txt","rb"))==NULL) { printf("不能打开文件!\n"); return; } if (fread(&n,sizeof(int),1,fp)!=1) { n=-1; } else { for(i=0;i<n;i++) { fread(&students[i],sizeof(struct Student),1,fp); } } fclose(fp); } //通过学号返回数组下标 int Student_SearchByi(char numb[]) { int i; for (i=0;i<n;i++) { if (strcmp(students[i].num,numb)==0) { return i; } } return -1; } // 查询 void Student_select() { while(1) { char numb[20]; int index; printf("请输入要查询的学生的学号:"); scanf("%s",&numb); getchar(); index=Student_SearchByi(numb); if (index==-1) { printf("该学生不存在!\n"); } else { printf("你要查询的学生信息为:\n"); Student_print(index); } printf("是否继续?(y/n)"); if (getchar()=='n') { break; } } } //删除信息 void Student_delete() { int i; while(1) { char numb[20]; int index; printf("请输入要删除的学生的学号:"); scanf("%s",&numb); getchar(); index=Student_SearchByi(numb); if (index==-1) { printf("学生不存在!\n"); } else { printf("你要删除的学生信息为:\n"); Student_print(index); printf("是否真的要删除?(y/n)"); if (getchar()=='y') { for (i=index;i<n-1;i++) { students[i]=students[i+1]; } n--; } getchar(); } printf("是否继续?(y/n)"); if (getchar()=='n') { break; } } } //修改学生信息 void Student_alter() { while(1) { char numb[20]; int index; printf("请输入要修改的学生的学号:"); scanf("%s",&numb); getchar(); index=Student_SearchByi(numb); if (index==-1) { printf("学生不存在!\n"); } else { printf("你要修改的学生信息为:\n"); Student_print(index); printf("-- 请输入新值--\n"); printf("请输入学号:"); scanf("%s",&students[index].num); getchar(); printf("请输入姓名:"); scanf("%s",&students[index].name); getchar(); printf("请输入分数:"); scanf("%d",&students[index].score); getchar(); } printf("是否继续?(y/n)"); if (getchar()=='n') { break; } } } //显示任意两个分数段之间学生信息 void Student_displaybysco() { int sco1,sco2,i; printf("请输入分数:"); scanf("%d%d",&sco1,&sco2); printf("%10s%10s%8s\n","学号","姓名","分数"); printf("--------------------------------------\n"); for(i=0;i<n;i++) if(students[i].score>=sco1&&students[i].score<=sco2) printf("%10s%10s%8d\n",students[i].num,students[i].name,students[i].score); } //主函数!! void main() { int choice,cho,flag=1; read(); while(1) { printf("\n------ 学生成绩管理系统------\n"); printf("\n---耿瑞冰---\n"); printf("1. 增加学生记录\n"); printf("2. 修改学生记录\n"); printf("3. 删除学生记录\n"); printf("4. 按学号查询学生记录\n"); printf("5. 排序\n"); printf("6.显示分数段之间学生信息\n"); printf("7. 退出\n"); printf("请选择(1-7):"); scanf("%d",&choice); switch(choice) { case 1: Student_add(); break; case 2: Student_alter(); break; case 3: Student_delete(); break; case 4: Student_select(); break; case 5: { printf("8.按学号排序\n9.按姓名排序\n10.按分数排序\n"); scanf("%d",&cho); switch(cho) { case 8: Student_Sortnum();break; case 9: Student_Sortname();break; case 10: Student_Sortscore();break; } //Student_display(); } break; case 6: Student_displaybysco(); break; case 7: flag=0; //exit(0); break; } if(flag==0) break; write(); } }