IB001 – Seminární skupiny 16, 17, 18 a 19

Študent (súbory) – zdrojový kód

#include <stdio.h>
#include <stdlib.h>

#define SUBOR "studenti.txt"
#define MAX_POCET 128

typedef struct student
{
    char meno[20];
    char priezvisko[20];
    unsigned short uco;
    float priemer;
} typStudent;

int compare (const void * student1, const void * student2)
{
    float priemer1 = ((typStudent*)student1)->priemer;
    float priemer2 = ((typStudent*)student2)->priemer;

    if (priemer1 < priemer2)
        return -1;
    else
        return (priemer1 > priemer2);
}

void vypisPercentilu(typStudent studenti[], int pocet)
{
    qsort(studenti, pocet, sizeof(typStudent), compare); // zoradenie pola podla priemeru

    // pre kazdeho studenta zisti a vypis jeho percentil
    int i = 0;
    while (i < pocet)
    {
        // zistenie, kolko dalsich studentov ma rovnaky priemer
        int j = 0;
        while (studenti[i].priemer == studenti[i+j+1].priemer)
        {
            j++;
        }

        float percentil = 100.0 * (pocet - i) / pocet;

        // vypis vsetkych studentov s tymto percentilom
        for (int k = 0; k <= j; k++)
        {
            printf("%6.2f   %s %s, uco %hu, priemer: %.2f\n",
                   percentil, studenti[i+k].meno, studenti[i+k].priezvisko,
                   studenti[i+k].uco, studenti[i+k].priemer);
        }

        i += j + 1;
    }
}

int main(void)
{
    FILE *subor;
    typStudent studenti[MAX_POCET];
    int pocet = 0;

    subor = fopen(SUBOR, "r");
    if (subor == NULL)
    {
        printf("Chyba! Subor %s nie je mozne otvorit.", SUBOR);
        return 1;
    }

    int i = 0;
    while (!feof(subor))
    {
        if (fscanf(subor, "%19s %19s %hu %f",
                   studenti[i].meno, studenti[i].priezvisko,
                   &studenti[i].uco, &studenti[i].priemer) == 4)
        {
            i++;
        }

    }

    pocet = i;

    fclose(subor);

    printf("Vypis percentilu:\n");
    vypisPercentilu(studenti, pocet);

    return 0;
}