Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LEN_MAX 31
- typedef struct {
- char name[LEN_MAX];
- float grade;
- } Stud;
- void readStud(Stud *stud) {
- scanf("%30s%f", stud->name, &stud->grade);
- }
- void printStud(const Stud *const stud) {
- printf("%s %g\n", stud->name, stud->grade);
- }
- int compStuds(const void *p1, const void *p2) {
- const Stud stud1 = *(const Stud*)p1;
- const Stud stud2 = *(const Stud*)p2;
- if (stud1.grade == stud2.grade) {
- return strcmp(stud1.name, stud2.name);
- }
- if (stud1.grade > stud2.grade) {
- return -1;
- }
- return 1;
- }
- int main() {
- int n;
- scanf("%d", &n);
- Stud *arr = calloc(sizeof(Stud), n);
- for (int i = 0; i < n; i++) {
- readStud(arr + i);
- }
- qsort(arr, n, sizeof(Stud), compStuds);
- for (int i = 0; i < n; i++) {
- printStud(arr + i);
- }
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement