要求:
程序设计实训(一)
一、实验内容
程序功能:最强版大连海事大学学生信息管理系统,实现学校各专业班级学生信息的管理(专业数可设置为3-5个)。环境:CLion2023+MinGW64
基本要求:
(1)输入10个学生的信息存储在文件studentInit.dat中。
(2)定义学生信息的结构体类型,包括:学号、姓名、专业、班级、3门成绩;和符号常量N(学生数)。结构体成员名字固定如下表。
(3)主要功能为输入数据、读写数据、修改数据、学生数据查找与排序。
(4)附加功能
#define N 10 struct Student{ char num[15];//学号 char name[15];//姓名 char major[10];//专业(zhineng,wanggong,ruangong) int classNo;//班级(1-2) int score[3];//3门课的成绩(0-2) }; typedef struct Student STU; |
二、实验要求
- *main函数:以菜单形式将各项功能提供给用户,根据用户的选择,调用相应的函数。
STU student[N]; //保存输入的N名学生信息
定义局部变量:
STU stu_class_ave[N]; //按平均成绩排序后的某个班级的学生信息;
int count; //实际元素个数
定义局部变量:
STU stu_class_ subject [N]; //按某门课程成绩排序后的某个专业的学生信息;
int count; //实际元素个数
- *定义函数void Input(STU *p, int n):从文件sutdentInit中输入n个学生的信息。
- *定义函数void Output(STU *p):将p所指的某个学生信息表格化屏幕输出。
- *定义函数STU Fetch(int studentIndex):从文件中随机读取第studentIndex个(0<= studentIndex <=N-1)学生的信息。
- *定义函数void Search(STU *p, int classNo, int scoreSum);:实现班级和成绩的综合查找(如1班,总分>240的同学)。
- *定义函数int Max(STU *p, int scoreIndex):求所有学生、下标为scoreIndex的课程分数最高的学生序号(在数组中的下标),学生序号作为返回值。
- *定义函数void Sort_select(STU *p): 对所有学生,按平均成绩由低到高进行简单选择排序。
- *定义函数void Sort_buble(STU *p, int n); 对某个班级的学生,按平均成绩由高到低进行起泡排序。并调用Output输出。
- 定义函数void Sort_insert(STU *p, int n, char *major):对某个专业的学生,按某门课程成绩由低到高进行直接插入排序。并调用Output输出。
- 定义函数void Save(STU *p,int n) :将学生信息存入文件
- 加了很多功能:::
-
// // Created by David Son on 2024/7/8. // #include<stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <algorithm> #define STUDENT_INFO_SIZE 50 #define N 20 struct Student{ char num[15];//学号 char name[15];//姓名 char major[10];//专业(computer,software,network) int classNo;//班级(1-2) int score[3];//3门课的成绩(0-2) }; typedef struct Student STU; //读取数据 int readStudentData(STU student[], const char *filename) { FILE *fp; const char *filename5 = "studentInit.txt"; fp = fopen(filename5, "r"); if (fp == NULL) { perror("Error opening file"); return -1; } char line[100]; int i = 0; while (fgets(line, sizeof(line), fp)) { // 分割字符串并存入结构体数组 sscanf(line, "%s %s %s %d %d %d %d", student[i].num, student[i].name, student[i].major, &student[i].classNo, &student[i].score[0], &student[i].score[1], &student[i].score[2]); i++; if (i >= N) break; // 防止数组越界 } fclose(fp); return i; // 返回读取的记录数 } //表格化输出某个学生信息 // 函数定义:根据学号查找学生信息并输出 void findStudentByNumber(const char* number, const struct Student students[], int numStudents) { int found = 0; for (int i = 0; i < numStudents; ++i) { if (strcmp(students[i].num, number) == 0) { printf("找到学号为 %s 的学生信息:\n", number); printf("学号:%s\n", students[i].num); printf("姓名:%s\n", students[i].name); printf("专业:%s\n", students[i].major); printf("班级:%d\n", students[i].classNo); printf("成绩:\n"); printf("课程0:%d\n", students[i].score[0]); printf("课程1:%d\n", students[i].score[1]); printf("课程2:%d\n", students[i].score[2]); found = 1; break; } } if (!found) { printf("未找到学号为 %s 的学生信息。\n", number); } } STU Fetch(int studentIndex) { FILE *file; char filename[] = "studentInit.txt"; // 学生信息文本文件名 STU student2; // 尝试打开文件 if ((file = fopen(filename, "r")) == NULL) { perror("Error opening file"); exit(1); } // 定位到文件中的第studentIndex个学生信息 int count = 0; while (count <= studentIndex) { if (fscanf(file, "%s %s %s %d %d %d %d\n", student2.num, student2.name, student2.major, &student2.classNo, &student2.score[0], &student2.score[1], &student2.score[2]) != 7) { fprintf(stderr, "Error reading student data\n"); exit(1); } count++; } // 关闭文件 fclose(file); return student2; } void Search(STU *p, int classNo, const char *major, int scoreSum,int a) { printf("专业 %s 班级 %d, 总分大于 %d 的学生:\n", major, classNo, scoreSum); for (int i = 0; i < a; i++) { if (p[i].classNo == classNo && strcmp(p[i].major, major) == 0) { int sum = 0; for (int j = 0; j < 3; j++) { // 假设有3门课的成绩 sum += p[i].score[j]; } if (sum > scoreSum) { printf("姓名:%s,学号:%s,专业:%s,班级:%d,总分:%d\n", p[i].name, p[i].num, p[i].major, p[i].classNo, sum); } } } } void Search2(STU *p, int classNo, const char *major, int scoreSum,int a) { printf("班级 %d,专业 %s 总分小于 %d 的学生:\n", classNo, major, scoreSum); for (int i = 0; i < a; i++) { if (p[i].classNo == classNo && strcmp(p[i].major, major) == 0) { int sum = 0; for (int j = 0; j < 3; j++) { // 假设有3门课的成绩 sum += p[i].score[j]; } if (sum < scoreSum) { printf("姓名:%s,学号:%s,专业:%s,班级:%d,总分:%d\n", p[i].name, p[i].num, p[i].major, p[i].classNo, sum); } } } } int Max(STU *p, int scoreIndex,int s) { if (p == nullptr) { return -1; // 如果传入的指针为空,返回一个特定值表示错误 } int maxScore = p[0].score[scoreIndex]; // 初始化最高分为第一个学生的该课程分数 int maxIndex = 0; // 初始化最高分学生的序号为第一个学生的序号 // 遍历数组,找出最高分的学生序号 for (int i = 1; i < s; ++i) { // 假设数组大小为 10,实际应根据情况调整 if (p[i].score[scoreIndex] > maxScore) { maxScore = p[i].score[scoreIndex]; maxIndex = i; } } return maxIndex; } int Min(STU *p, int scoreIndex,int s) { if (p == nullptr) { return -1; // 如果传入的指针为空,返回一个特定值表示错误 } int minScore = p[0].score[scoreIndex]; // 初始化最高分为第一个学生的该课程分数 int minIndex = 0; // 初始化最高分学生的序号为第一个学生的序号 // 遍历数组,找出最高分的学生序号 for (int i = 1; i < s; ++i) { // 假设数组大小为 10,实际应根据情况调整 //printf("%d",p[i].score[scoreIndex]); if (p[i].score[scoreIndex] < minScore) { minScore = p[i].score[scoreIndex]; minIndex = i; } } return minIndex; } // 计算学生平均成绩的函数 float calculateAverageScore(const STU& student) { int sum = 0; for (int i = 0; i < 3; ++i) { // 假设有3门课 sum += student.score[i]; } return static_cast<float>(sum) / 3.0; // 返回平均成绩 } // 比较两个学生的平均成绩大小,用于选择排序 bool compareByAverageScore(const STU& student1, const STU& student2) { return calculateAverageScore(student1) < calculateAverageScore(student2);//返回0/1 } // 简单选择排序函数,排序并输出学生信息: 遍历找到最小的排列到最前面 void sortAndPrintStudents(STU *p, int n) { // 简单选择排序,依次双重遍历,找剩下最小的排到前 for (int i = 0; i < n - 1; ++i) { int min_idx = 0; for (int j = i + 1; j < n; ++j) { if (compareByAverageScore(p[j], p[min_idx])) { min_idx = j; //j更小,j最小 } } // 将找到的最小元素与当前位置交换,第一个if不用判断,直接在内存空间进行交换,交换0和最小的,1和最小的,2和最小的... if (min_idx != i) { std::swap(p[i], p[min_idx]); } } // 使用 printf 输出排序后的学生信息 printf("按平均成绩由低到高排序后的学生信息:\n"); for (int i = 0; i < n; ++i) { printf("姓名:%s,学号:%s,专业:%s,班级:%d,平均成绩:%.2f\n", p[i].name, p[i].num, p[i].major, p[i].classNo, calculateAverageScore(p[i])); } } void sortAndPrintStudents1(STU *p, int n) { for (int i = 0; i < n - 1; ++i) { int max_idx = i; for (int j = i + 1; j < n; ++j) { if (!compareByAverageScore(p[j], p[max_idx])) { max_idx = j; // 如果p[j]的平均成绩比p[max_idx]大,则更新max_idx } } if (max_idx != i) { std::swap(p[i], p[max_idx]); // 将找到的最大元素与当前位置交换 } } // 输出降序排序后的学生信息 printf("按平均成绩由高到低排序后的学生信息:\n"); for (int i = 0; i < n; ++i) { printf("姓名:%s,学号:%s,专业:%s,班级:%d,平均成绩:%.2f\n", p[i].name, p[i].num, p[i].major, p[i].classNo, calculateAverageScore(p[i])); } } // 定义一个结构体用于存储学生的平均成绩和原始信息,为原来结构体增加一个平均成绩的新属性 struct AvgScoreSTU { STU student; float avgScore; }; // 冒泡排序函数,同时计算并存储平均成绩和排序结果到stu_class_ave数组中 void SortAndCalculateAverage(STU *p, int n, AvgScoreSTU *stu_class_ave) { // 计算每个学生的平均成绩并存储到临时结构体数组中 AvgScoreSTU temp[N]; for (int i = 0; i < n; ++i) { temp[i].student = p[i]; int sum = 0; for (int j = 0; j < 3; ++j) { // 假设有3门课 sum += p[i].score[j]; } temp[i].avgScore = static_cast<float>(sum) / 3.0; } // 冒泡排序,同时将排序结果存储到stu_class_ave数组中,每次比较之后,最小元素一定会被放在最后 for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - i - 1; ++j) { if (temp[j].avgScore < temp[j + 1].avgScore) { std::swap(temp[j], temp[j + 1]); } } } // 将排序后的结果存储到stu_class_ave数组中 for (int i = 0; i < n; ++i) { stu_class_ave[i] = temp[i]; } } void SortAndCalculateAverage1(STU *p, int n, AvgScoreSTU *stu_class_ave) { // 计算每个学生的平均成绩并存储到临时结构体数组中 AvgScoreSTU temp[N]; for (int i = 0; i < n; ++i) { temp[i].student = p[i]; int sum = 0; for (int j = 0; j < 3; ++j) { // 假设有3门课 sum += p[i].score[j]; } temp[i].avgScore = static_cast<float>(sum) / 3.0; } // 冒泡排序,同时将排序结果存储到stu_class_ave数组中 for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - i - 1; ++j) { // 改成升序排序条件 if (temp[j].avgScore > temp[j + 1].avgScore) { std::swap(temp[j], temp[j + 1]); } } } // 将排序后的结果存储到stu_class_ave数组中 for (int i = 0; i < n; ++i) { stu_class_ave[i] = temp[i]; } } void Output(struct Student *students, int count) { printf("Student information:\n"); printf("学号 姓名 专业 班级 课程0 课程1 课程2\n"); for (int i = 0; i < count; i++) { printf("%-8s %-12s %-12s %-5d %-5d %-5d %-5d\n", students[i].num, students[i].name, students[i].major, students[i].classNo, students[i].score[0], students[i].score[1], students[i].score[2]); } } // 按指定课程成绩排序学生信息 void Sort_insert(struct Student *students, int count, char *major, int course_index) { // 筛选出指定专业的学生 struct Student sorted_students[N]; int sorted_count = 0; for (int i = 0; i < count; i++) { if (strcmp(students[i].major, major) == 0) { sorted_students[sorted_count++] = students[i]; } } // 使用插入排序按照指定课程成绩(course_index 对应 score[course_index]),数据依次插入前面 已排好 的数据,逐步更新排序序列。key和前面排号的比,寻找插入位置 for (int i = 1; i < sorted_count; i++) { struct Student key = sorted_students[i]; int j = i - 1; while (j >= 0 && sorted_students[j].score[course_index] > key.score[course_index]) { sorted_students[j + 1] = sorted_students[j]; //新去的元素和前面的再依次比,大的往后 j--; } //依次比完之后,如果第一个记录大的话会被覆盖,恢复第一个记录 sorted_students[j + 1] = key; } // 输出排序后的结果 Output(sorted_students, sorted_count); } void Sort_insert1(struct Student *students, int count, char *major, int course_index) { // 筛选出指定专业的学生 struct Student sorted_students[N]; int sorted_count = 0; for (int i = 0; i < count; i++) { if (strcmp(students[i].major, major) == 0) { sorted_students[sorted_count++] = students[i]; } } // 使用插入排序按照指定课程成绩(course_index 对应 score[course_index]),数据依次插入前面已排好的数据,逐步更新排序序列 for (int i = 1; i < sorted_count; i++) { struct Student key = sorted_students[i]; int j = i - 1; while (j >= 0 && sorted_students[j].score[course_index] < key.score[course_index]) { sorted_students[j + 1] = sorted_students[j]; j--; } sorted_students[j + 1] = key; } // 输出排序后的结果 Output(sorted_students, sorted_count); } void UpdateScore(STU student[], int count, const char* studentNum, int courseNum, int newScore) { // 遍历数组,找到学号为 studentNum 的学生,并更新对应课程的成绩 for (int i = 0; i < count; i++) { if (strcmp(student[i].num, studentNum) == 0) { student[i].score[courseNum] = newScore; // courseNum 是 1、2 或 3,所以要减去 1 printf("成绩更新成功!\n"); return; // 找到学生并更新后立即返回 } } printf("未找到学号为 %s 的学生。\n", studentNum); } void SaveToFile(STU *students, int numStudents, const char *filename) { FILE *fp; fp = fopen(filename, "w"); // 以写入模式打开文件 if (fp == NULL) { printf("无法打开文件 %s。\n", filename); return; } for (int i = 0; i < numStudents; ++i) { fprintf(fp, "%s %s %s %d %d %d %d\n", students[i].num, students[i].name, students[i].major, students[i].classNo, students[i].score[0], students[i].score[1], students[i].score[2]); } fclose(fp); printf("学生信息已成功保存到文件 %s。\n", filename); } // 插入新的学生信息函数实现 void insertStudent(STU students[], int *count) { if (*count < N) { printf("请输入学生的学号: \n"); scanf("%s", students[*count].num); // 假设学号不超过15个字符 printf("请输入学生的姓名: \n"); scanf("%s", students[*count].name); // 假设姓名不超过15个字符 printf("请输入学生的专业(zhineng, wanggong, ruangong): \n"); scanf("%s", students[*count].major); // 假设专业名不超过10个字符 printf("请输入学生的班级: \n"); scanf("%d", &students[*count].classNo); // 假设班级为1或2 printf("请输入学生的3门课的成绩(用空格分隔): \n"); scanf("%d %d %d", &students[*count].score[0], &students[*count].score[1], &students[*count].score[2]); (*count)++; // 更新学生数量 printf("学生信息插入成功!\n"); } else { printf("数组已满,无法插入更多学生信息。\n"); } } // 遍历输出所有学生信息函数实现 void printAllStudents(STU students[], int count) { for (int i = 0; i < count; ++i) { //printf("%d",i); //printf("%d",count); printf("学号:%s 姓名:%s 专业:%s 班级:%d 成绩:%d %d %d\n", students[i].num, students[i].name, students[i].major, students[i].classNo, students[i].score[0], students[i].score[1], students[i].score[2]); } } // 删除指定学号的学生信息函数实现 void deleteStudent(STU students[], int *count, const char *numToDelete) { int found = 0; // 标记是否找到要删除的学生 for (int i = 0; i < *count; ++i) { if (strcmp(students[i].num, numToDelete) == 0) { // 找到要删除的学生数据,将后面的学生数据向前移动 for (int j = i; j < *count - 1; ++j) { strcpy(students[j].num, students[j + 1].num); strcpy(students[j].name, students[j + 1].name); strcpy(students[j].major, students[j + 1].major); students[j].classNo = students[j + 1].classNo; students[j].score[0] = students[j + 1].score[0]; students[j].score[1] = students[j + 1].score[1]; students[j].score[2] = students[j + 1].score[2]; } // 清空最后一个学生数据 strcpy(students[*count - 1].num, ""); strcpy(students[*count - 1].name, ""); strcpy(students[*count - 1].major, ""); students[*count - 1].classNo = 0; students[*count - 1].score[0] = 0; students[*count - 1].score[1] = 0; students[*count - 1].score[2] = 0; (*count)--; // 更新学生数量 found = 1; // 设置找到标记 break; // 已删除学生,退出循环 } } if (!found) { printf("未找到学号为 %s 的学生信息。\n", numToDelete); } else { printf("学号为 %s 的学生信息已成功删除。\n", numToDelete); } } // 遍历输出所有学生信息函数实现 void printAllStudents1(STU students[], int count) { for (int i = 0; i < count; ++i) { printf("学号:%s, 姓名:%s, 专业:%s, 班级:%d, 成绩:%d, %d, %d\n", students[i].num, students[i].name, students[i].major, students[i].classNo, students[i].score[0], students[i].score[1], students[i].score[2]); } } // 从文件中读取备份的学生信息并添加到STU数组末尾 void restoreDeletedStudent(const char *filename, struct Student students[], int *currentSize, const char *targetNum) { FILE *file = fopen(filename, "r"); if (file == NULL) { perror("Error opening file"); return; } // 逐行读取文件中的学生信息,找到匹配的学号后添加到students数组末尾 char num[15]; char name[15]; char major[10]; int classNo; int score[3]; while (fscanf(file, "%s %s %s %d %d %d %d", num, name, major, &classNo, &score[0], &score[1], &score[2]) == 7) { if (strcmp(num, targetNum) == 0) { strcpy(students[*currentSize].num, num); strcpy(students[*currentSize].name, name); strcpy(students[*currentSize].major, major); students[*currentSize].classNo = classNo; students[*currentSize].score[0] = score[0]; students[*currentSize].score[1] = score[1]; students[*currentSize].score[2] = score[2]; (*currentSize)++; break; // 找到并添加后退出循环 } } fclose(file); } // 输出学生信息数组 void printStudents(const struct Student students[], int numStudents) { for (int i = 0; i < numStudents; ++i) { printf("学号:%s\n", students[i].num); printf("姓名:%s\n", students[i].name); printf("专业:%s\n", students[i].major); printf("班级:%d\n", students[i].classNo); printf("成绩:%d, %d, %d\n", students[i].score[0], students[i].score[1], students[i].score[2]); printf("\n"); } } int main() { int a = 0; int cishu=5; STU student[N]; int count=0; char r[20]; while (1) { printf("请输入用户swh的密码:\n"); scanf("%s", r); while (cishu != 0) { if (strcmp(r, "swh040527") == 0) { printf("登陆成功!\n"); cishu=1; break; } else { if (cishu == 0) { printf("登录失败,请等待30s后重新登陆!\n"); break; } cishu--; printf("密码错误,请重新登录\n"); printf("您还有%d次机会\n", cishu); printf("请输入用户swh的密码:\n"); scanf("%s", r); } } if (cishu == 0) { printf("登录失败,请等待30s后重新登陆!\n"); break; } break; } while(1){ if (cishu == 0) { break; } printf("----------------------------------------\n"); printf("欢迎来到大连海事大学学生信息管理系统\n"); printf("请输入数字以选择相应功能\n"); printf("1:从文件中读取学生信息\n"); printf("2:继续手动添加学生信息\n"); printf("3:表格化输出某个学生信息\n"); printf("4:随机输出某个学生信息\n"); printf("5:班级和成绩的综合查找\n"); printf("6:查找某门课分数最高或者最低的学生\n"); printf("7:所有学生平均成绩的简单选择排序输出\n"); printf("8:某个专业班级学生的平均成绩冒泡排序\n"); printf("9:某个专业学生的平均成绩插入排序\n"); printf("10:修改某个学生的成绩信息\n"); printf("11:保存学生信息到文件\n"); printf("12:删除指定学生信息\n"); printf("13:恢复指定学生信息\n"); printf("14:退出系统\n"); printf("请输入序号:\n"); scanf("%d", &a); if ((a < 1) || (a > 14)){ printf("你的输入有误,请重新输入\n"); } //printf("你的输入有误\n"); switch (a) { case 1: { int i=0; if (readStudentData(student, "student3.txt") == -1) { fprintf(stderr, "Error reading student data from file.\n"); return 1; } i = readStudentData(student, "student3.txt"); printf("学生信息读取完毕。\n"); for (int j = 0; j < i; j++) { count++; printf("学号:%s, 姓名:%s, 专业:%s, 班级:%d, 成绩:%d %d %d\n", student[j].num, student[j].name, student[j].major, student[j].classNo, student[j].score[0], student[j].score[1], student[j].score[2]); } break; } case 2:{ //printf("%d",count); // 进行学生信息的插入 insertStudent(student, &count); // 输出所有学生信息 printf("当前所有学生信息:\n"); printAllStudents(student, count);; break; } case 3: { char x[20]; int b; printf("请输入你要输出的学生学号:"); scanf("%s", x);//天哪,问题根源 findStudentByNumber(x, student, count); break; } case 4: { //int index ; // 例如,读取第2个学生的信息 //printf("请输入你要查找第几个学生信息:"); //scanf("%d", &index); srand(time(NULL)); int index = rand() % 10; printf("这是第%d个学生信息:\n", index); STU student1 = Fetch(index); printf("学号: %s\n", student1.num); printf("姓名: %s\n", student1.name); printf("专业: %s\n", student1.major); printf("班级: %d\n", student1.classNo); printf("成绩:\n"); printf(" 课程1: %d\n", student1.score[0]); printf(" 课程2: %d\n", student1.score[1]); printf(" 课程3: %d\n", student1.score[2]); break; } case 5:{ int y,w,v; int z; printf("请输入专业名称:zhineng 0 wanggong 1 ruangong 2\n"); scanf("%d",&z); printf("请输入班级序号:\n"); scanf("%d",&y); printf("请输入你要查找的目的,大于界限0还是小于界限1:\n"); scanf("%d",&w); if(w==0){ printf("请输入你要查找学生的课程总分大于多少:\n"); scanf("%d",&v); if(z==0){ Search(student, y, "zhineng", v,count); // 查找2班智能科学与技术专业总分大于240的学生 } if(z==1){ Search(student, y, "wanggong", v,count); // 查找2班智能科学与技术专业总分大于240的学生 } if(z==2){ Search(student, y, "ruangong", v,count); // 查找2班智能科学与技术专业总分大于240的学生 } } if(w==1){ printf("请输入你要查找学生的课程总分小于多少:\n"); scanf("%d",&v); if(z==0){ Search2(student, y, "zhineng", v,count); // 查找2班智能科学与技术专业总分大于240的学生 } if(z==1){ Search2(student, y, "wanggong", v,count); // 查找2班智能科学与技术专业总分大于240的学生 } if(z==2){ Search2(student, y, "ruangong", v,count); // 查找2班智能科学与技术专业总分大于240的学生 } } break; } case 6:{ int x,m; printf("请输入课程序号012\n"); scanf("%d",&x); printf("查找最高分还是最低分?最高分:0 最低分:1\n"); scanf("%d",&m); if(m==0) { int highestScoreIndex = Max(student, x,count); if (highestScoreIndex != -1) { printf("分数最高的学生是:%s\n", student[highestScoreIndex].name); printf("学号:%s\n", student[highestScoreIndex].num); printf("专业:%s\n", student[highestScoreIndex].major); printf("班级:%d\n", student[highestScoreIndex].classNo); printf("第%d门课程分数课程分数:%d\n", x, student[highestScoreIndex].score[x]); } else { printf("学生数组为空,或者出现了其他错误。\n"); } } if(m==1){ int highestScoreIndex = Min(student, x,count); if (highestScoreIndex != -1) { printf("分数最低的学生是:%s\n", student[highestScoreIndex].name); printf("学号:%s\n", student[highestScoreIndex].num); printf("专业:%s\n", student[highestScoreIndex].major); printf("班级:%d\n", student[highestScoreIndex].classNo); printf("第%d门课程分数课程分数:%d\n", x, student[highestScoreIndex].score[x]); } else { printf("学生数组为空,或者出现了其他错误。\n"); } } break; } case 7:{ int y=0; printf("你想要进行升序排序0还是降序排序1:\n"); scanf("%d",&y); if(y==0){ sortAndPrintStudents(student, count); } if(y==1){ sortAndPrintStudents1(student, count); } break; } case 8:{ int u; int w = 0; AvgScoreSTU stu_class_ave[15]; // 存储排序后的学生信息的数组 int y=0; printf("你想要进行升序排序0还是降序排序1:\n"); scanf("%d",&y); if(y==0){ SortAndCalculateAverage(student, count, stu_class_ave); } if(y==1){ SortAndCalculateAverage(student, count, stu_class_ave); } // 调用排序和计算平均成绩函数,同时将结果存储到stu_class_ave数组中 //SortAndCalculateAverage(student, 10, stu_class_ave); printf("选择专业班级智能1:0\t智能2:1\t网工1:2\t网工2:3\t软工1:4\t软工2:5\n"); scanf("%d",&u); //Output(stu_class_ave,count); // 输出排序后的学生信息,先排序,后选择性输出(遍历,选择并复制到新数组,输出) if(u==0){ for (int i = 0; i < 10; ++i) { // 添加筛选条件:专业为 "zhineng" 且班级为 2 的学生信息才保存,这里最好使用第二个数组来保存,我使用同一个数组,只不过原来的记录被选择性覆盖消失了 if (strcmp(stu_class_ave[i].student.major, "zhineng") == 0 && stu_class_ave[i].student.classNo == 1) { // 将符合条件的学生信息保存到 stu_class_ave 数组中 strcpy(stu_class_ave[w].student.num, stu_class_ave[i].student.num); strcpy(stu_class_ave[w].student.name, stu_class_ave[i].student.name); strcpy(stu_class_ave[w].student.major, stu_class_ave[i].student.major); stu_class_ave[w].student.classNo = stu_class_ave[i].student.classNo; // 如果 stu_class_ave 数组有成绩字段,也要进行复制,不用的,因为最后单个的成绩也没有输出,不必写入 memcpy(stu_class_ave[w].student.score, stu_class_ave[i].student.score, sizeof(int) * 3); stu_class_ave[w].avgScore = stu_class_ave[i].avgScore; w++; // 如果已经找到 N 条符合条件的学生信息,则退出循环 if (w == N) break; } } } if(u==1){ for (int i = 0; i < 10; ++i) { // 添加筛选条件:专业为 "zhineng" 且班级为 2 的学生信息才保存 if (strcmp(stu_class_ave[i].student.major, "zhineng") == 0 && stu_class_ave[i].student.classNo == 2) { // 将符合条件的学生信息保存到 stu_class_ave 数组中 strcpy(stu_class_ave[w].student.num, stu_class_ave[i].student.num); strcpy(stu_class_ave[w].student.name, stu_class_ave[i].student.name); strcpy(stu_class_ave[w].student.major, stu_class_ave[i].student.major); stu_class_ave[w].student.classNo = stu_class_ave[i].student.classNo; // 如果 stu_class_ave 数组有成绩字段,也要进行复制 memcpy(stu_class_ave[w].student.score, stu_class_ave[i].student.score, sizeof(int) * 3); stu_class_ave[w].avgScore = stu_class_ave[i].avgScore; w++; // 如果已经找到 N 条符合条件的学生信息,则退出循环 if (w == N) break; } } } if(u==2){ for (int i = 0; i < 10; ++i) { // 添加筛选条件:专业为 "zhineng" 且班级为 2 的学生信息才保存 if (strcmp(stu_class_ave[i].student.major, "wanggong") == 0 && stu_class_ave[i].student.classNo == 1) { // 将符合条件的学生信息保存到 stu_class_ave 数组中 strcpy(stu_class_ave[w].student.num, stu_class_ave[i].student.num); strcpy(stu_class_ave[w].student.name, stu_class_ave[i].student.name); strcpy(stu_class_ave[w].student.major, stu_class_ave[i].student.major); stu_class_ave[w].student.classNo = stu_class_ave[i].student.classNo; // 如果 stu_class_ave 数组有成绩字段,也要进行复制 memcpy(stu_class_ave[w].student.score, stu_class_ave[i].student.score, sizeof(int) * 3); stu_class_ave[w].avgScore = stu_class_ave[i].avgScore; w++; // 如果已经找到 N 条符合条件的学生信息,则退出循环 if (w == N) break; } } } if(u==3){ for (int i = 0; i < 10; ++i) { // 添加筛选条件:专业为 "zhineng" 且班级为 2 的学生信息才保存 if (strcmp(stu_class_ave[i].student.major, "wanggong") == 0 && stu_class_ave[i].student.classNo == 2) { // 将符合条件的学生信息保存到 stu_class_ave 数组中 strcpy(stu_class_ave[w].student.num, stu_class_ave[i].student.num); strcpy(stu_class_ave[w].student.name, stu_class_ave[i].student.name); strcpy(stu_class_ave[w].student.major, stu_class_ave[i].student.major); stu_class_ave[w].student.classNo = stu_class_ave[i].student.classNo; // 如果 stu_class_ave 数组有成绩字段,也要进行复制 memcpy(stu_class_ave[w].student.score, stu_class_ave[i].student.score, sizeof(int) * 3); stu_class_ave[w].avgScore = stu_class_ave[i].avgScore; w++; // 如果已经找到 N 条符合条件的学生信息,则退出循环 if (w == N) break; } } } if(u==4){ for (int i = 0; i < 10; ++i) { // 添加筛选条件:专业为 "zhineng" 且班级为 2 的学生信息才保存 if (strcmp(stu_class_ave[i].student.major, "ruangong") == 0 && stu_class_ave[i].student.classNo == 1) { // 将符合条件的学生信息保存到 stu_class_ave 数组中 strcpy(stu_class_ave[w].student.num, stu_class_ave[i].student.num); strcpy(stu_class_ave[w].student.name, stu_class_ave[i].student.name); strcpy(stu_class_ave[w].student.major, stu_class_ave[i].student.major); stu_class_ave[w].student.classNo = stu_class_ave[i].student.classNo; // 如果 stu_class_ave 数组有成绩字段,也要进行复制 memcpy(stu_class_ave[w].student.score, stu_class_ave[i].student.score, sizeof(int) * 3); stu_class_ave[w].avgScore = stu_class_ave[i].avgScore; w++; // 如果已经找到 N 条符合条件的学生信息,则退出循环 if (w == N) break; } } } if(u==5){ for (int i = 0; i < 10; ++i) { // 添加筛选条件:专业为 "zhineng" 且班级为 2 的学生信息才保存 if (strcmp(stu_class_ave[i].student.major, "ruangong") == 0 && stu_class_ave[i].student.classNo == 2) { // 将符合条件的学生信息保存到 stu_class_ave 数组中 strcpy(stu_class_ave[w].student.num, stu_class_ave[i].student.num); strcpy(stu_class_ave[w].student.name, stu_class_ave[i].student.name); strcpy(stu_class_ave[w].student.major, stu_class_ave[i].student.major); stu_class_ave[w].student.classNo = stu_class_ave[i].student.classNo; // 如果 stu_class_ave 数组有成绩字段,也要进行复制 memcpy(stu_class_ave[w].student.score, stu_class_ave[i].student.score, sizeof(int) * 3); stu_class_ave[w].avgScore = stu_class_ave[i].avgScore; w++; // 如果已经找到 N 条符合条件的学生信息,则退出循环 if (w == N) break; } } } printf("符合条件的按平均成绩由高到低排序后的学生信息:\n"); for (int i = 0; i < w; ++i) { printf("姓名:%s,学号:%s,专业:%s,班级:%d,平均成绩:%.2f\n", stu_class_ave[i].student.name, stu_class_ave[i].student.num, stu_class_ave[i].student.major, stu_class_ave[i].student.classNo, stu_class_ave[i].avgScore); } break; } case 9:{ int q,p,b; printf("请选择专业:\n智能:0\t网工:1\t软工:2\n"); scanf("%d",&q); printf("升序排序0还是降序排序1?\n"); scanf("%d",&b); if(b==0) { printf("请选择你要升序排序的课程012\n"); scanf("%d", &p); if (q == 0) { if (p == 0) { Sort_insert(student, count, "zhineng", 0); } if (p == 1) { Sort_insert(student, count, "zhineng", 1); } if (p == 2) { Sort_insert(student, count, "zhineng", 2); } } if (q == 1) { if (p == 0) { Sort_insert(student, count, "wanggong", 0); } if (p == 1) { Sort_insert(student, count, "wanggong", 1); } if (p == 2) { Sort_insert(student, count, "wanggong", 2); } } if (q == 2) { if (p == 0) { Sort_insert(student, count, "ruangong", 0); } if (p == 1) { Sort_insert(student, count, "ruangong", 1); } if (p == 2) { Sort_insert(student, count, "ruangong", 2); } } } if(b==1){ printf("请选择你要降序排序的课程012\n"); scanf("%d", &p); if (q == 0) { if (p == 0) { Sort_insert1(student, count, "zhineng", 0); } if (p == 1) { Sort_insert1(student, count, "zhineng", 1); } if (p == 2) { Sort_insert1(student, count, "zhineng", 2); } } if (q == 1) { if (p == 0) { Sort_insert1(student, count, "wanggong", 0); } if (p == 1) { Sort_insert1(student, count, "wanggong", 1); } if (p == 2) { Sort_insert1(student, count, "wanggong", 2); } } if (q == 2) { if (p == 0) { Sort_insert1(student, count, "ruangong", 0); } if (p == 1) { Sort_insert1(student, count, "ruangong", 1); } if (p == 2) { Sort_insert1(student, count, "ruangong", 2); } } } break; } case 10:{ int ha,hu; char hi[20]; printf("请选择你要更改成绩的学生的学号:\n"); scanf("%s",hi); printf("更改第几门课程分数012:\n"); scanf("%d",&ha); printf("更改课程新的分数:\n"); scanf("%d",&hu); UpdateScore(student, count, hi, ha, hu); printf("更新后的学生信息:\n"); for (int i = 0; i < count; i++) { printf("学号:%s,姓名:%s,专业:%s,班级:%d,成绩:%d %d %d\n", student[i].num, student[i].name, student[i].major, student[i].classNo, student[i].score[0], student[i].score[1], student[i].score[2]); } } case 11:{ SaveToFile(student, count, "new_student.txt"); break; } case 12:{ // 删除指定学号的学生信息 int f=0; char s[15]; //const char *numToDelete = "2220221234"; printf("请输入你要删除学生的学号:\n"); scanf("%s",s); while(f==0) { for (int i = 0; i < count; ++i) { if (strcmp(student[i].num, s) == 0) { f = 1; deleteStudent(student, &count, s); } } if(f==0){ printf("没有您要删除的学生,请检查学号,重新输入:\n"); printf("请输入你要删除学生的学号:\n"); scanf("%s",s); } } //deleteStudent(student, &count, s); // 输出删除后的学生信息 printf("\n删除学号为 %s 的学生信息后:\n", s); printAllStudents1(student, count); break; } case 13:{ //int numStudents = 3; // 当前学生信息数组的长度 char v[15]; printf("请输入恢复你要恢复学生数据的学号:\n"); scanf("%s",v); // 调用恢复学生信息的函数, restoreDeletedStudent("new_student.txt", student, &count, v); // 打印所有学生信息,包括恢复的信息 printf("恢复已删除的学生信息后:\n"); //printStudents(student, count); for (int j = 0; j < count; j++) { printf("学号:%s, 姓名:%s, 专业:%s, 班级:%d, 成绩:%d %d %d\n", student[j].num, student[j].name, student[j].major, student[j].classNo, student[j].score[0], student[j].score[1], student[j].score[2]); } break; } case 14:{ printf("欢迎下次使用!\n"); break; } } if(a==14) break; } return 0; }