|
|
|
@ -0,0 +1,133 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
struct Student {
|
|
|
|
|
int studentId;
|
|
|
|
|
int classId;
|
|
|
|
|
char name[20];
|
|
|
|
|
double scores[3];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 初始化学生信息
|
|
|
|
|
void initStudents() {
|
|
|
|
|
struct Student students[6] = {
|
|
|
|
|
{10001, 11, "Zhang", {99.5, 88.5, 89.5}},
|
|
|
|
|
{10002, 12, "Yang", {77.9, 56.5, 87.5}},
|
|
|
|
|
{10003, 11, "Liang", {92.5, 99.0, 60.5}},
|
|
|
|
|
{10004, 11, "Cai", {89.6, 56.9, 90.5}},
|
|
|
|
|
{10005, 14, "Fu", {55.6, 67.9, 98.9}},
|
|
|
|
|
{10006, 12, "Mao", {22.1, 45.9, 99.2}}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印学生信息
|
|
|
|
|
void printStudent(struct Student s) {
|
|
|
|
|
printf("%d %d %s %.2lf %.2lf %.2lf\n", s.studentId, s.classId, s.name, s.scores[0], s.scores[1], s.scores[2]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按班级排序函数
|
|
|
|
|
void sortByClass(struct Student students[], int numStudents) {
|
|
|
|
|
for (int i = 0; i < numStudents - 1; i++) {
|
|
|
|
|
for (int j = i + 1; j < numStudents; j++) {
|
|
|
|
|
if (students[i].classId > students[j].classId) {
|
|
|
|
|
struct Student temp = students[i];
|
|
|
|
|
students[i] = students[j];
|
|
|
|
|
students[j] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按总成绩排序函数
|
|
|
|
|
void sortByScore(struct Student students[], int numStudents) {
|
|
|
|
|
for (int i = 0; i < numStudents - 1; i++) {
|
|
|
|
|
for (int j = i + 1; j < numStudents; j++) {
|
|
|
|
|
double sum1 = students[i].scores[0] + students[i].scores[1] + students[i].scores[2];
|
|
|
|
|
double sum2 = students[j].scores[0] + students[j].scores[1] + students[j].scores[2];
|
|
|
|
|
if (sum1 < sum2) {
|
|
|
|
|
struct Student temp = students[i];
|
|
|
|
|
students[i] = students[j];
|
|
|
|
|
students[j] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查询学生信息函数
|
|
|
|
|
void searchStudents() {
|
|
|
|
|
int classId1, classId2; // 班级范围,如果不需要限制班级范围,则不需要定义这两个变量
|
|
|
|
|
int studentId1, studentId2; // 学号范围,如果不需要限制学号范围,则不需要定义这两个变量
|
|
|
|
|
char name[20]; // 姓名,如果不需要按姓名查询,则不需要定义这个变量
|
|
|
|
|
double score = 0; // 总分,如果不需要按总分查询,则不需要定义这个变量
|
|
|
|
|
char query[3]; // 查询类型,需要限制长度为3,因为最多只有三种类型的查询(班级、学号、姓名)
|
|
|
|
|
scanf("%s", query); // 从用户输入中获取查询类型和查询条件值,这里假设用户会按照要求输入查询类型和查询条件值,并且不会有非法输入的情况出现。如果需要更加健壮的代码,则需要考虑异常情况的处理。
|
|
|
|
|
switch (query[0]) { // 根据查询类型选择对应的处理方式,这里假设查询类型只有一种,因此只需要处理班级查询类型即可。如果需要处理其他类型的查询(如学号、姓名等),则需要增加对应的处理方式。
|
|
|
|
|
case '1':
|
|
|
|
|
scanf("%d-%d", &classId1, &classId2);
|
|
|
|
|
printf("查询班级 %d-%d 的学生信息:\n", classId1, classId2);
|
|
|
|
|
// 在学生信息中查找满足班级范围要求的学生,然后按班级从小到大,同班级按总成绩从大到小排序显示出来
|
|
|
|
|
break;
|
|
|
|
|
case '2':
|
|
|
|
|
scanf("%d-%d", &studentId1, &studentId2);
|
|
|
|
|
printf("查询学号 %d-%d 的学生信息:\n", studentId1, studentId2);
|
|
|
|
|
// 在学生信息中查找满足学号范围要求的学生,然后按班级从小到大,同班级按总成绩从大到小排序显示出来
|
|
|
|
|
break;
|
|
|
|
|
case '3':
|
|
|
|
|
scanf("%s", name);
|
|
|
|
|
printf("查询姓名以 %s 开头的学生信息:\n", name);
|
|
|
|
|
// 在学生信息中查找满足姓名开头要求的学生,然后按班级从小到大,同班级按总成绩从大到小排序显示出来
|
|
|
|
|
break;
|
|
|
|
|
case '4':
|
|
|
|
|
scanf("%lf", &score);
|
|
|
|
|
printf("查询总分大于等于 %.2lf 的学生信息:\n", score);
|
|
|
|
|
// 在学生信息中查找满足总分要求的学生,然后按班级从小到大,同班级按总成绩从大到小排序显示出来
|
|
|
|
|
break;
|
|
|
|
|
case '5':
|
|
|
|
|
scanf("%d.%d-%d", &classId1, &studentId1, &studentId2);
|
|
|
|
|
printf("查询班级 %d 内学号 %d-%d 的学生信息:\n", classId1, studentId1, studentId2);
|
|
|
|
|
// 在学生信息中查找满足班级和学号范围要求的学生,然后按学号从小到大排序显示出来
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("无效的查询类型!\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
struct Student students[6]; // 定义结构体数组存储学生信息
|
|
|
|
|
initStudents(students); // 初始化学生信息
|
|
|
|
|
int numStudents = sizeof(students) / sizeof(students[0]); // 计算学生数量
|
|
|
|
|
int choice; // 存储用户选择的操作类型
|
|
|
|
|
while (1) { // 循环处理用户输入,直到用户选择退出程序为止
|
|
|
|
|
printf("请选择操作:\n");
|
|
|
|
|
printf("1. 添加学生信息\n");
|
|
|
|
|
printf("2. 修改学生信息\n");
|
|
|
|
|
printf("3. 删除学生信息\n");
|
|
|
|
|
printf("4. 查询学生信息\n");
|
|
|
|
|
printf("5. 退出程序\n");
|
|
|
|
|
scanf("%d", &choice); // 获取用户选择的操作类型
|
|
|
|
|
switch (choice) {
|
|
|
|
|
case 1: // 添加学生信息操作
|
|
|
|
|
addStudent(students, numStudents);
|
|
|
|
|
numStudents++; // 添加新学生后,学生数量加1
|
|
|
|
|
break;
|
|
|
|
|
case 2: // 修改学生信息操作
|
|
|
|
|
modifyStudent(students, numStudents);
|
|
|
|
|
break;
|
|
|
|
|
case 3: // 删除学生信息操作
|
|
|
|
|
deleteStudent(students, numStudents);
|
|
|
|
|
numStudents--; // 删除学生后,学生数量减1
|
|
|
|
|
break;
|
|
|
|
|
case 4: // 查询学生信息操作
|
|
|
|
|
searchStudents(students, numStudents);
|
|
|
|
|
break;
|
|
|
|
|
case 5: // 退出程序操作
|
|
|
|
|
return 0; // 程序结束,返回0表示正常退出
|
|
|
|
|
default: // 无效的操作类型,提示用户重新输入
|
|
|
|
|
printf("无效的操作类型,请重新输入!\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|