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.
117 lines
3.4 KiB
117 lines
3.4 KiB
6 days ago
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#define MAX_STUDENTS 100
|
||
|
|
||
|
typedef struct Student {
|
||
|
char id[20];
|
||
|
int classNum;
|
||
|
char name[20];
|
||
|
double score1;
|
||
|
double score2;
|
||
|
double score3;
|
||
|
} Student;
|
||
|
|
||
|
int compare(const void *a, const void *b) {
|
||
|
Student *s1 = (Student *)a;
|
||
|
Student *s2 = (Student *)b;
|
||
|
if (s1->classNum!= s2->classNum) {
|
||
|
return s1->classNum - s2->classNum;
|
||
|
}
|
||
|
double sum1 = s1->score1 + s1->score2 + s1->score3;
|
||
|
double sum2 = s2->score1 + s2->score2 + s2->score3;
|
||
|
return (sum2 > sum1) - (sum2 < sum1);
|
||
|
}
|
||
|
|
||
|
int findByID(Student students[], int n, char id[]) {
|
||
|
int i;
|
||
|
for ( i = 0; i < n; i++) {
|
||
|
if (strcmp( students[i].id,id) == 0) {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
void modifyStudent(Student students[], int n, char id[], int newClass, char newName[],
|
||
|
double newScore1, double newScore2, double newScore3) {
|
||
|
int index = findByID(students, n, id);
|
||
|
if (index!= -1) {
|
||
|
students[index].classNum = newClass;
|
||
|
strcpy(students[index].name, newName);
|
||
|
students[index].score1 = newScore1;
|
||
|
students[index].score2 = newScore2;
|
||
|
students[index].score3 = newScore3;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void printStudents(Student students[], int n,char inputID[]) {
|
||
|
int currentClass = -1;
|
||
|
int i;
|
||
|
for ( i = 0; i < n; i++) {
|
||
|
if (students[i].classNum!= currentClass) {
|
||
|
if (i!= 0) {
|
||
|
printf("\n");
|
||
|
}
|
||
|
currentClass = students[i].classNum;
|
||
|
printf("%d %s %s %.1lf %.1lf %.1lf", currentClass, students[i].id, students[i].name,
|
||
|
students[i].score1, students[i].score2, students[i].score3);
|
||
|
if (i == findByID(students, n, inputID)) {
|
||
|
printf(" modified");
|
||
|
}
|
||
|
printf("\n");
|
||
|
} else {
|
||
|
printf(" %s %s %.1lf %.1lf %.1lf", students[i].id, students[i].name,
|
||
|
students[i].score1, students[i].score2, students[i].score3);
|
||
|
if (i == findByID(students, n, inputID)) {
|
||
|
printf(" modified");}
|
||
|
printf("\n");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
Student students[MAX_STUDENTS];
|
||
|
int numStudents = 3;
|
||
|
|
||
|
// ???????????
|
||
|
strcpy(students[0].id, "10001");
|
||
|
students[0].classNum = 11;
|
||
|
strcpy(students[0].name, "Zhang");
|
||
|
students[0].score1 = 99.5;
|
||
|
students[0].score2 = 88.5;
|
||
|
students[0].score3 = 89.5;
|
||
|
|
||
|
strcpy(students[1].id, "10002");
|
||
|
students[1].classNum = 12;
|
||
|
strcpy(students[1].name, "Yang");
|
||
|
students[1].score1 = 77.9;
|
||
|
students[1].score2 = 56.5;
|
||
|
students[1].score3 = 87.5;
|
||
|
|
||
|
strcpy(students[2].id, "10003");
|
||
|
students[2].classNum = 11;
|
||
|
strcpy(students[2].name, "Liang");
|
||
|
students[2].score1 = 92.5;
|
||
|
students[2].score2 = 99.0;
|
||
|
students[2].score3 = 60.5;
|
||
|
|
||
|
qsort(students, numStudents, sizeof(Student), compare);
|
||
|
|
||
|
char inputID[20];
|
||
|
int inputClass;
|
||
|
char inputName[20];
|
||
|
double inputScore1, inputScore2, inputScore3;
|
||
|
printf("please input the student 's information");
|
||
|
scanf("%s %d %s %lf %lf %lf", inputID, &inputClass, inputName, &inputScore1, &inputScore2, &inputScore3);
|
||
|
|
||
|
modifyStudent(students, numStudents, inputID, inputClass, inputName, inputScore1, inputScore2, inputScore3);
|
||
|
printStudents(students, numStudents,inputID);
|
||
|
|
||
|
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|