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.

87 lines
2.4 KiB

#include <stdio.h>
#include <string.h>
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><E1B9B9>
struct Student {
char id[10];
int class;
char name[20];
float score1, score2, score3;
};
// <20><><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void deleteStudent(struct Student students[], int* numStudents, char key[]) {
int i, j, found = 0;
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7>
for (i = 0; i < *numStudents; i++) {
if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) {
found = 1;
// ɾ<><C9BE>ѧ<EFBFBD><D1A7>
for (j = i; j < *numStudents - 1; j++) {
students[j] = students[j + 1];
}
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
(*numStudents)--;
break;
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
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() {
// <20><>ʼ<EFBFBD><CABC>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
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]);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼѧ<CABC><D1A7><EFBFBD><EFBFBD>Ϣ
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);
}
// ѭ<><D1AD>ɾ<EFBFBD><C9BE>ѧ<EFBFBD><D1A7>
char key[20];
char choice;
do {
// <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>Ҫɾ<D2AA><C9BE><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>ѧ<EFBFBD>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD>
printf("Enter the student ID or name to delete: ");
scanf("%s", key);
// ɾ<><C9BE>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
deleteStudent(students, &numStudents, key);
// ѯ<><D1AF><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>
printf("Are you sure (yes/no)? ");
scanf(" %c", &choice);
} while (choice == 'no');
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
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;
}