1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include <stdio.h> #include <stdlib.h> #include <string.h>
struct S { char name[50]; float final_score; float class_score; char is_cadre; char is_west; int published_papers; int scholarship; };
void input(struct S *s) { printf("Input name: "); scanf("%s", s->name); printf("Input final score: "); scanf("%f", &(s->final_score)); printf("Input class score: "); scanf("%f", &(s->class_score)); printf("Class cadre or not? (Y/N): "); scanf(" %c", &(s->is_cadre)); printf("Students from the West or not? (Y/N): "); scanf(" %c", &(s->is_west)); printf("Input the number of published papers: "); scanf("%d", &(s->published_papers)); }
int sort(const void *a, const void *b) { return strcmp(((struct S *)a)->name, ((struct S *)b)->name); }
void print(struct S *s) { printf("Name: %s\n", s->name); printf("Final score: %.2f\n", s->final_score); printf("Class score: %.2f\n", s->class_score); printf("Class cadre: %c\n", s->is_cadre); printf("Students from the West: %c\n", s->is_west); printf("Published papers: %d\n", s->published_papers); printf("Scholarship: %d\n", s->scholarship); printf("\n"); }
void addup(struct S *s) { s->scholarship = 0;
if (s->final_score > 80 && s->published_papers >= 1) { s->scholarship += 8000; } if (s->final_score > 85 && s->class_score > 80) { s->scholarship += 4000; } if (s->final_score > 90) { s->scholarship += 2000; } if (s->final_score > 85 && s->is_west == 'Y') { s->scholarship += 1000; } if (s->class_score > 80 && s->is_cadre == 'Y') { s->scholarship += 850; } }
struct S findMax(struct S *students, int n) { struct S maxStudent = students[0];
for (int i = 1; i < n; i++) { if (students[i].scholarship > maxStudent.scholarship) { maxStudent = students[i]; } } return maxStudent; }
int main() { int n; printf("Input n: "); scanf("%d", &n); struct S *students = (struct S *)malloc(n * sizeof(struct S)); for (int i = 0; i < n; i++) { printf("\nEnter data for student %d:\n", i + 1); input(&students[i]); addup(&students[i]); }
qsort(students, n, sizeof(struct S), sort); printf("\nStudent information:\n"); for (int i = 0; i < n; i++) { print(&students[i]); }
struct S maxStudent = findMax(students, n); printf("\nStudent with the highest scholarship:\n"); print(&maxStudent); free(students);
return 0; }
|