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.
82 lines
2.5 KiB
82 lines
2.5 KiB
6 days ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define MAX_STUDENTS 100
|
||
|
|
||
|
typedef struct {
|
||
|
char id[20];
|
||
|
char clas[20];
|
||
|
char name[20];
|
||
|
double score1;
|
||
|
double score2;
|
||
|
double score3;
|
||
|
double totalScore;
|
||
|
} Student;
|
||
|
|
||
|
// Comparison function for sorting
|
||
|
int compareStudents(const void *a, const void *b) {
|
||
|
Student *studentA = (Student *)a;
|
||
|
Student *studentB = (Student *)b;
|
||
|
int classComparison = strcmp(studentA->clas, studentB->clas);
|
||
|
if (classComparison != 0) {
|
||
|
return classComparison;
|
||
|
} else {
|
||
|
return (studentB->totalScore - studentA->totalScore > 0) ? 1 : -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void modifyStudent(Student students[], int *count) {
|
||
|
char id[20], clas[20], name[20];
|
||
|
double score1, score2, score3;
|
||
|
|
||
|
printf("Enter student id to modify: ");
|
||
|
scanf("%s", id);
|
||
|
for (int i = 0; i < *count; i++) {
|
||
|
if (strcmp(students[i].id, id) == 0) {
|
||
|
printf("Enter class, name, score1, score2, score3: ");
|
||
|
scanf("%s %s %lf %lf %lf", clas, name, &score1, &score2, &score3);
|
||
|
strcpy(students[i].clas, clas);
|
||
|
strcpy(students[i].name, name);
|
||
|
students[i].score1 = score1;
|
||
|
students[i].score2 = score2;
|
||
|
students[i].score3 = score3;
|
||
|
students[i].totalScore = score1 + score2 + score3;
|
||
|
qsort(students, *count, sizeof(Student), compareStudents);
|
||
|
printf("Student modified.\n");
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
printf("Student not found.\n");
|
||
|
}
|
||
|
|
||
|
void printStudents(const Student students[], int count) {
|
||
|
for (int i = 0; i < count; i++) {
|
||
|
if (i == 0 || strcmp(students[i].clas, students[i-1].clas) != 0) {
|
||
|
printf("%s ", students[i].clas);
|
||
|
}
|
||
|
printf("%s %s %.1f %.1f %.1f", students[i].id, students[i].name, students[i].score1, students[i].score2, students[i].score3);
|
||
|
if (i == count - 1 || strcmp(students[i+1].clas, students[i].clas) != 0) {
|
||
|
printf(" \n");
|
||
|
} else {
|
||
|
printf(" modified\n");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
Student students[MAX_STUDENTS] = {
|
||
|
{"10001", "11", "Zhang", 99.5, 88.5, 89.5, 277.5},
|
||
|
{"10002", "12", "Yang", 77.9, 56.5, 87.5, 221.9},
|
||
|
{"10003", "11", "Liang", 92.5, 99.0, 60.5, 252.0}
|
||
|
};
|
||
|
int studentCount = 3;
|
||
|
|
||
|
qsort(students, studentCount, sizeof(Student), compareStudents);
|
||
|
|
||
|
modifyStudent(students, &studentCount);
|
||
|
printStudents(students, studentCount);
|
||
|
|
||
|
return 0;
|
||
|
}
|