You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123xj/6c8c9d0552e7613b0bb0108013c...

91 lines
2.5 KiB

#include <stdio.h>
#include <string.h>
// 定义学生结构体
struct Student {
char studentID[10];
int classs;
char name[20];
float score1;
float score2;
float score3;
};
// 初始化学生信息数组
struct Student students[] = {
{"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}
};
// 学生信息数组的长度
int length = sizeof(students) / sizeof(students[0]);
// 根据学号或姓名删除学生信息
void deleteStudent(char* input) {
int i, j;
int found = 0; // 标记是否找到待删除的学生信息
// 遍历学生信息数组
for (i = 0; i < length; i++) {
// 判断学号或姓名是否匹配
if (strcmp(students[i].studentID, input) == 0 || strcmp(students[i].name, input) == 0) {
found = 1; // 找到了待删除的学生信息
// 将后面的学生信息向前移动
for (j = i; j < length - 1; j++) {
students[j] = students[j + 1];
}
length--; // 学生信息数组长度减一
break;
}
}
}
// 按要求输出学生信息
void printStudents() {
int i;
// 排序学生信息数组
for (i = 0; i < length - 1; i++) {
int minIndex = i;
int j;
for (j = i + 1; j < length; j++) {
// 按班级从小到大排序,同一班级内按总成绩从大到小排序
if (students[j].classs < students[minIndex].classs ||
(students[j].classs == students[minIndex].classs &&
students[j].score1 + students[j].score2 + students[j].score3 >
students[minIndex].score1 + students[minIndex].score2 + students[minIndex].score3)) {
minIndex = j;
}
}
// 交换位置
struct Student temp = students[i];
students[i] = students[minIndex];
students[minIndex] = temp;
}
// 输出学生信息
for (i = 0; i < length; i++) {
printf("\n%s %d %s %.1f %.1f %.1f\n", students[i].studentID, students[i].classs, students[i].name, students[i].score1, students[i].score2, students[i].score3);
}
}
int main() {
char input[20];
// 提示用户输入待删除学生的学号或姓名
printf("请输入待删除学生的学号或姓名:");
scanf("%s", input);
printf("Are you sure(yes/no)?\n");
char c;
scanf(" %c", &c);
if (c=='y'){
deleteStudent(input);
printStudents();}
else if(c=='n')
printStudents();
return 0;
}