ADD file via upload

main
pfwail38f 1 year ago
parent 841a2f9fbc
commit 1c4bed9df1

87
5.c

@ -0,0 +1,87 @@
#include <stdio.h>
#include <string.h>
// 定义学生结构体
struct Student {
char id[10];
int class;
char name[20];
float score1, score2, score3;
};
// 定义删除学生函数
void deleteStudent(struct Student students[], int* numStudents, char key[]) {
int i, j, found = 0;
// 查找学生
for (i = 0; i < *numStudents; i++) {
if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) {
found = 1;
// 删除学生
for (j = i; j < *numStudents - 1; j++) {
students[j] = students[j + 1];
}
// 减少学生数量
(*numStudents)--;
break;
}
}
// 如果找到学生,则输出剩余学生信息
if (found) {
printf("Student deleted. Remaining students:\n");
for (i = 0; i < *numStudents; i++) {
printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].class, students[i].name,
students[i].score1, students[i].score2, students[i].score3);
}
}
else {
printf("Student not found.\n");
}
}
int main() {
// 初始化学生信息
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 numStudents = sizeof(students) / sizeof(students[0]);
// 输出初始学生信息
printf("Initial students:\n");
for (int i = 0; i < numStudents; i++) {
printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].class, students[i].name,
students[i].score1, students[i].score2, students[i].score3);
}
// 循环删除学生
char key[20];
char choice;
do {
// 接收用户输入要删除的学生的学号或姓名
printf("Enter the student ID or name to delete: ");
scanf("%s", key);
// 删除学生并输出结果
deleteStudent(students, &numStudents, key);
// 询问是否继续删除
printf("Are you sure (yes/no)? ");
scanf(" %c", &choice);
} while (choice == 'no');
// 输出最终学生信息
printf("Final students:\n");
for (int i = 0; i < numStudents; i++) {
printf("%s %d %s %.2f %.2f %.2f\n", students[i].id, students[i].class, students[i].name,
students[i].score1, students[i].score2, students[i].score3);
}
return 0;
}
Loading…
Cancel
Save