#include #include #include typedef struct { char id[20]; char clas[20]; char name[20]; double score1; double score2; double score3; double score; } Student; Student students[1000]; int studentCount=0; //输入学生信息 void inputStudentInfo( ) { printf("Id "); scanf("%s", students[studentCount].id); printf("Class "); scanf("%s", &students[studentCount].clas); printf("Name "); scanf("%s", students[studentCount].name); printf("Score1 "); scanf("%lf", &students[studentCount].score1); printf("Score2 "); scanf("%lf", &students[studentCount].score2); printf("Score3 "); scanf("%lf", &students[studentCount].score3); // 计算总成绩 students[studentCount].score = students[studentCount].score1 + students[studentCount].score2 + students[studentCount].score3; studentCount++; } //显示信息 void displayAllStudents() { if (studentCount == 0) { printf("No students to display.\n"); return; } int i; for (i=0; i < studentCount; i++) { // 输出学生信息 printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score); } } void sortByClassAndScore() { if (studentCount <= 1) { // 无需排序 return; } // 使用简单的冒泡排序 int i; for ( i = 0; i < studentCount - 1; i++) { int j; for ( j = 0; j < studentCount - 1 - i; j++) { if (atoi(students[j].clas) > atoi(students[j + 1].clas) || (atoi(students[j].clas)== atoi(students[j + 1].clas)&& students[j].score < students[j + 1].score)) { // 交换学生信息 Student temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } for (i = 0; i < studentCount; i++) { // 输出学生信息 printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score); } } //删除学生信息 id or name void deleteStudent() { if (studentCount <= 1) { printf("No students to delete.\n"); return; } char dele[20]; printf("Enter the ID of the student you want to delete: "); scanf("%s", dele); int i,j; int found = 0; for (i = 0; i < studentCount; i++) { if ((strcmp(students[i].id, dele) == 0) || (strcmp(students[i].name, dele) == 0)) { found = 1; // 删除学生,将后面的学生向前移动 for ( j = i; j < studentCount - 1; j++) { students[j] = students[j + 1]; } studentCount--; for ( i = 0; i < studentCount; i++) { // 输出学生信息 printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score); /* printf("Student with ID %s deleted.\n", studentId);*/ } break; } } if (!found) { // 输出学生信息 printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score); /*printf("Student with ID %s not found.\n", studentId);*/ } } //查询 id or cla void queryStudent() { if (studentCount == 0) { printf("No students to query.\n"); return; } char quer[20]; printf("Enter the ID of the student you want to query: "); scanf("%s", quer); int i; int found = 0; for (i = 0; i < studentCount; i++) { if (strcmp(students[i].id, quer) == 0||strcmp(students[i].clas,quer)==0) { found = 1; // 输出学生信息 printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score); } } if (!found) { printf("there is no eligible student"); } } int main() { while (1) { printf("1.input\n2.delete\n3.select\n4.order\n5.output\n6.quit\nplease input your option\n"); int option; scanf("%d", &option); switch(option){ case 1:{ while(1) { inputStudentInfo(); printf("continue?\n"); char ch[20]; scanf("%s", ch); if (strcmp(ch, "yes") != 0) { break; // 输入no,则跳出循环 } } break; } case 2:{ while(1) { deleteStudent(); printf("continue?\n"); char ch[20]; scanf("%s", ch); if (strcmp(ch, "yes") != 0) { break; // 输入no,则跳出循环 } } break; } case 3:{ while(1) { queryStudent(); printf("continue?\n"); char ch[20]; scanf("%s", ch); if (strcmp(ch, "yes") != 0) { break; // 输入no,则跳出循环 } } break; } case 4:{ if(studentCount>=3) { sortByClassAndScore(); } break; } case 5:{ displayAllStudents(); break; } case 6: exit(0); } } return 0; }