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.
134 lines
3.9 KiB
134 lines
3.9 KiB
1 year ago
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define MAX_STUDENTS 3
|
||
|
#define MAX_NAME_LENGTH 20
|
||
|
|
||
|
struct Student {
|
||
|
char id[MAX_NAME_LENGTH];
|
||
|
int class;
|
||
|
char name[MAX_NAME_LENGTH];
|
||
|
float score1;
|
||
|
float score2;
|
||
|
float score3;
|
||
|
};
|
||
|
|
||
|
void printStudent(struct Student student) {
|
||
|
printf("%s %d %s %.1f %.1f %.1f\n", student.id, student.class, student.name, student.score1, student.score2, student.score3);
|
||
|
}
|
||
|
|
||
|
void printAllStudents(struct Student students[], int count) {
|
||
|
for (int i = 0; i < count; i++) {
|
||
|
printStudent(students[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int deleteStudent(struct Student students[], int* count, char* search) {
|
||
|
int found = 0;
|
||
|
for (int i = 0; i < *count; i++) {
|
||
|
if (strcmp(students[i].id, search) == 0 || strcmp(students[i].name, search) == 0) {
|
||
|
found = 1;
|
||
|
for (int j = i; j < *count - 1; j++) {
|
||
|
students[j] = students[j + 1];
|
||
|
}
|
||
|
(*count)--;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return found;
|
||
|
}
|
||
|
|
||
|
void sortStudents(struct Student students[], int count) {
|
||
|
int i, j;
|
||
|
struct Student temp;
|
||
|
for (i = 0; i < count - 1; i++) {
|
||
|
for (j = 0; j < count - i - 1; j++) {
|
||
|
if (students[j].class > students[j + 1].class ||
|
||
|
(students[j].class == students[j + 1].class && students[j].score1 + students[j].score2 + students[j].score3 < students[j + 1].score1 + students[j + 1].score2 + students[j + 1].score3)) {
|
||
|
temp = students[j];
|
||
|
students[j] = students[j + 1];
|
||
|
students[j + 1] = temp;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
struct Student students[MAX_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 studentCount = MAX_STUDENTS;
|
||
|
|
||
|
char search[MAX_NAME_LENGTH];
|
||
|
printf("Enter student ID or name to delete: ");
|
||
|
scanf("%s", search);
|
||
|
|
||
|
int found = deleteStudent(students, &studentCount, search);
|
||
|
if (found) {
|
||
|
sortStudents(students, studentCount);
|
||
|
printAllStudents(students, studentCount);
|
||
|
|
||
|
char confirm[3];
|
||
|
printf("Are you sure (yes/no)? ");
|
||
|
scanf("%s", confirm);
|
||
|
if (strcmp(confirm, "yes") == 0) {
|
||
|
return 0;
|
||
|
}
|
||
|
} else {
|
||
|
printf("Student not found.\n");
|
||
|
}
|
||
|
|
||
|
printAllStudents(students, studentCount);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void sortStudents(struct Student students[], int count) {
|
||
|
int i, j;
|
||
|
struct Student temp;
|
||
|
for (i = 0; i < count - 1; i++) {
|
||
|
for (j = 0; j < count - i - 1; j++) {
|
||
|
if (students[j].class > students[j + 1].class ||
|
||
|
(students[j].class == students[j + 1].class && students[j].score1 + students[j].score2 + students[j].score3 < students[j + 1].score1 + students[j + 1].score2 + students[j + 1].score3)) {
|
||
|
temp = students[j];
|
||
|
students[j] = students[j + 1];
|
||
|
students[j + 1] = temp;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
struct Student students[MAX_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 studentCount = MAX_STUDENTS;
|
||
|
|
||
|
char search[MAX_NAME_LENGTH];
|
||
|
printf("Enter student ID or name to delete: ");
|
||
|
scanf("%s", search);
|
||
|
|
||
|
int found = deleteStudent(students, &studentCount, search);
|
||
|
if (found) {
|
||
|
sortStudents(students, studentCount);
|
||
|
printAllStudents(students, studentCount);
|
||
|
|
||
|
char confirm[3];
|
||
|
printf("Are you sure (yes/no)? ");
|
||
|
scanf("%s", confirm);
|
||
|
if (strcmp(confirm, "yes") == 0) {
|
||
|
return 0;
|
||
|
}
|
||
|
} else {
|
||
|
printf("Student not found.\n");
|
||
|
}
|
||
|
|
||
|
printAllStudents(students, studentCount);
|
||
|
|
||
|
return 0;
|
||
|
}
|