Teória na začiatok

Vypracované úlohy

10_cvicenie.pdf

⚠️ Programy sú rozdelené z dôvodu prehľadnosti a pre rýchly prístup pre debug, preto nespĺňajú všetky stanovené podmienky od Cvičiaceho

Úloha prvá:

/* Musk Elon 1337 */
/* cvicenie 10    */
/* 27.11.2021     */ 

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

typedef struct student {
    char priezvisko_meno[100];
    int rocnik;
    float priemer;
} STUDENT;

STUDENT *nacitajPole(char *subor, int *nacitane)
{
    FILE *f = fopen(subor,"r");
    char buff[100];
    int pocet,rocnik;
    float priemer;
    STUDENT *ziak;

    if(f==NULL) 
    return NULL;

    fscanf(f,"%d",&pocet);
    fgets(buff,100,f);
    *nacitane = pocet;
    ziak = malloc(pocet*sizeof(STUDENT));

    for(int i = 0; i < pocet; i++)
    {
        fgets(ziak[i].priezvisko_meno,100,f);
        ziak[i].priezvisko_meno[strlen(ziak[i].priezvisko_meno)-1] = '\0';
        fscanf(f,"%d %f",&ziak[i].rocnik,&ziak[i].priemer);
        fgetc(f);
    }
    fclose(f);
    return ziak;
}

Úloha druhá:

void vypisPole (STUDENT *studenti, int pocet)
{
    for(int i=0;i<pocet;i++)
    {
        printf("%s;%d;%0.2f\n",studenti[i].priezvisko_meno,studenti[i].rocnik,studenti[i].priemer);
    }
}

Úloha tretia:

STUDENT *najlepsiPole (STUDENT *studenti, int pocet, int rocnik)
{
    STUDENT *najlepsi = NULL;
    int naj_index = 0;

    for(int i=0;i<pocet;i++)
    {
        if(studenti[i].rocnik==rocnik)
        {
            if(najlepsi != NULL)
            {
                if(studenti[i].priemer < najlepsi->priemer)
                {
                    najlepsi = &studenti[i];
                }
            }
            else
            najlepsi = &studenti[i];
        }
    }
    return najlepsi;
}

MAIN:

int main()
{
    STUDENT *pole, *naj;
    int pocet;
    pole = nacitajPole("vstup.txt", &pocet);

    if (pole == NULL) 
    {
        printf("nepodarilo sa nacitat a naplnit pole");
        return -1;
    }
    vypisPole(pole, pocet);
    naj = najlepsiPole(pole, pocet, 3);

    if (naj == NULL) 
    {
        printf("v rocniku nie je student");
        return -2;
    }

    printf("najlepsi student je %s", naj->priezvisko_meno);

    free(pole); //// TOTO tu zabudol pan vyucujuci KAZDE MALLOCOVANE POLE TREBA FREEOVAT!
    return 0;
}

Úlohy na precvičenie - neboli