printf("英语:");
scanf("%lf", &t.eng);
return t;
}
void input_record() /*输入若干条记录*/
{
char choice = 'y';
while (choice == 'Y' || choice == 'y') /*判断*/
{
Stud[Student_Count] = input_a_record();
printf("还有更多的记录吗?(Y/N)");
Student_Count++;
fflush(stdin); //清除输入缓冲区
choice = getchar();
}
}
void calc_total() // 计算总分
{
int i;
for (i = 0; i Stud[i].total = Stud[i].chi + Stud[i].math + Stud[i].eng; } } void sort_by_num()//按学号排序 { int i, j; Student t; for (i = 0; i for (j = 0; j if (Stud[j].num> Stud[j + 1].num){ t = Stud[j]; Stud[j] = Stud[j + 1]; Stud[j + 1] = t; } } void sort_by_total() { int i, j; Student t; for (i = 0; i < Student_Count - 1; i++) for (j = 0; j if (Stud[j].total>Stud[j+1].total){ t = Stud[j]; Stud[j] = Stud[j + 1]; Stud[j + 1] = t; } } int Delete_record_by_num(int num) /*按姓名查找,删除一条记录*/ { int i, j; int found = -1; //记录查找到的记录下标 for (i = 0; i if (Stud[i].num == num){ found = i; break; } } if (found != -1){ for (j = found; j { Stud[j] = Stud[j + 1]; } Student_Count--; return 0; } return(-1); } void Delete_a_record() { int num; printf("请输入要删除的学生学号:"); /*交互式问寻*/ scanf("%d", &num); if (Delete_record_by_num(num) == 0) printf("删除成功"); else printf("删除失败"); } Student *query_a_record_by_name(char *name) { int i = 0; while ((strcmp(Stud[i].name, name) != 0) && i < Student_Count) i++; if (i == Student_Count) return NULL; return &Stud[i]; } void query_a_record() { //fflush(stdin); char chname[20]; getchar(); printf("请输入要查找的名字:"); gets(chname); display_header(); display_a_record(query_a_record_by_name(&chname)); } void Statistic() /*输出统计信息*/ { int i; int pc, pm, pe, gc, gm, ge; pc = pm = pe = gc = gm = ge = 0; Student pass = { 0, "及格率", 0, 0, 0, 0 }, good = { 0, "优秀率", 0, 0, 0, 0 }; Student max = { -1, "最高分", 0, 0, 0, 0 }, aver = { -1, "平均分", 0, 0, 0, 0 }; for (i = 0; i aver.chi += Stud[i].chi; aver.math += Stud[i].math; aver.eng += Stud[i].eng; aver.total += Stud[i].total; if (Stud[i].chi>max.chi) max.chi = Stud[i].chi; if (Stud[i].math>max.math) max.math = Stud[i].math; if (Stud[i].eng>max.eng) max.eng = Stud[i].eng; if (Stud[i].total>max.total) max.total = Stud[i].total; if (Stud[i].chi>59) pc++; if (Stud[i].math>59) pm++; if (Stud[i].eng>59) pe++; if (Stud[i].chi >= 90) gc++; if (Stud[i].math >= 90) gm++; if (Stud[i].eng >= 90) ge++; } aver.chi = aver.chi / Student_Count; aver.math = aver.math / Student_Count; aver.eng = aver.eng / Student_Count; aver.total = aver.total / Student_Count; pass.chi = pc / Student_Count * 100; pass.math = pm / Student_Count * 100; pass.eng = pe / Student_Count * 100; good.chi = gc / Student_Count * 100; good.math = gm / Student_Count * 100; good.eng = ge / Student_Count * 100; display_header(); display_a_record(&max); display_a_record(&aver); display_a_record(&pass); display_a_record(&good); } void read_textfile() /*从文件中读入数据*/ { int i = 0; FILE *fp; /*定义文件指针*/ fflush(stdin); if ((fp = fopen("students.txt", "r")) == NULL) /*打开文件*/ { printf("error"); return(-1); } fscanf(fp, "%d\n", &Student_Count); /*读入总记录量*/ for (i = 0; i { fscanf(fp, "%d\n", &Stud[i].num); fscanf(fp, "%s\n", Stud[i].name); fscanf(fp, "%lf\n,%lf\n,%lf\n", &Stud[i].chi, &Stud[i].math, &Stud[i].eng); } fclose(fp); /*关闭文件*/ printf("读取完成\n"); } void write_textfile() { int i = 0; FILE *fp; if ((fp = fopen("students.txt","w")) == NULL){ printf("error"); exit(0); } fprintf(fp, "%d\n", Student_Count); for (i = 0; i fprintf(fp, "%d\n", Stud[i].num); fprintf(fp, "%s\n", Stud[i].name); fprintf(fp, "%lf\n,%lf\n,%lf\n",Stud[i].chi, Stud[i].math, Stud[i].eng); } fclose(fp); printf("保存成功!\n"); } void main() /*主函数*/ { int n = 0; char choice = ' '; while (choice != '0') { display_menu(); fflush(stdin); //清空输入缓冲区 choice = getchar(); switch (choice) { case '1': //添加记录 input_record(); display_all_record(); break; case '2': //显示所有记录 calc_total(); display_all_record(); break; case '3': //按学号排序 calc_total(); sort_by_num(); display_all_record(); break; case '4': calc_total(); sort_by_total(); display_all_record(); break; case '5': Delete_a_record(); break; case '6': query_a_record(); break; case '7': Statistic(); break; case '8': read_textfile(); break; case '9': write_textfile(); break; case '0': printf("感谢您使用本程序,再见!\n"); exit(0); break;/*结束程序*/ } } }