funguje
This commit is contained in:
		
							parent
							
								
									71e5daa9f9
								
							
						
					
					
						commit
						994444a401
					
				@ -2,71 +2,52 @@
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#define SIZE 100 // Veľkosť pomocného poľa pre načítanie riadku
 | 
			
		||||
#define SIZE 100
 | 
			
		||||
 | 
			
		||||
// Definícia štruktúry pre študenta
 | 
			
		||||
struct student {
 | 
			
		||||
    char name[50];
 | 
			
		||||
// Štruktúra pre reprezentáciu študenta
 | 
			
		||||
typedef struct {
 | 
			
		||||
    char name[SIZE];
 | 
			
		||||
    int votes;
 | 
			
		||||
};
 | 
			
		||||
} Student;
 | 
			
		||||
 | 
			
		||||
// Funkcia na porovnanie študentov pri triedení
 | 
			
		||||
int compare(const void *s1, const void *s2) {
 | 
			
		||||
    struct student *student1 = (struct student *)s1;
 | 
			
		||||
    struct student *student2 = (struct student *)s2;
 | 
			
		||||
    
 | 
			
		||||
    // Ak majú rôzny počet hlasov, porovnáme podľa počtu hlasov
 | 
			
		||||
    if (student1->votes != student2->votes) {
 | 
			
		||||
        return student2->votes - student1->votes; // Zoradenie zostupne podľa počtu hlasov
 | 
			
		||||
    } else {
 | 
			
		||||
        // Ak majú rovnaký počet hlasov, porovnáme podľa abecedy
 | 
			
		||||
        return strcmp(student1->name, student2->name);
 | 
			
		||||
    }
 | 
			
		||||
// Funkcia na pridanie študenta do databázy
 | 
			
		||||
void addStudent(Student database[], char name[], int votes, int *count) {
 | 
			
		||||
    strcpy(database[*count].name, name);
 | 
			
		||||
    database[*count].votes = votes;
 | 
			
		||||
    (*count)++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    struct student students[100]; // Pole pre ukladanie študentov
 | 
			
		||||
    int num_students = 0; // Počet načítaných študentov
 | 
			
		||||
    
 | 
			
		||||
    // Načítanie hlasov zo vstupu
 | 
			
		||||
    while (1) {
 | 
			
		||||
        int votes;
 | 
			
		||||
        char name[50];
 | 
			
		||||
    char line[SIZE];
 | 
			
		||||
    char name[SIZE];
 | 
			
		||||
    int votes;
 | 
			
		||||
 | 
			
		||||
        // Načítame riadok zo vstupu
 | 
			
		||||
        memset(line, 0, SIZE);
 | 
			
		||||
        char* r = fgets(line, SIZE, stdin);
 | 
			
		||||
        if (r == NULL) {
 | 
			
		||||
            // Koniec vstupu
 | 
			
		||||
            break;
 | 
			
		||||
    // Databáza študentov
 | 
			
		||||
    Student database[SIZE];
 | 
			
		||||
    int count = 0;
 | 
			
		||||
 | 
			
		||||
    printf("Zadajte mená študentov a počet hlasov:\n");
 | 
			
		||||
    
 | 
			
		||||
    while (1) {
 | 
			
		||||
        // Načítanie riadku zo štandardného vstupu
 | 
			
		||||
        if (fgets(line, SIZE, stdin) == NULL || line[0] == '\n') {
 | 
			
		||||
            break; // Koniec načítania
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Načítame hlas a meno študenta zo vstupného riadku
 | 
			
		||||
        if (sscanf(line, "%d %49[^\n]", &votes, name) == 2) {
 | 
			
		||||
            // Ak sme načítali záznam úspešne, pridáme študenta do databázy
 | 
			
		||||
            strcpy(students[num_students].name, name);
 | 
			
		||||
            students[num_students].votes = votes;
 | 
			
		||||
            num_students++;
 | 
			
		||||
        } else {
 | 
			
		||||
            // Ak sa nepodarilo načítať, skončíme načítavanie
 | 
			
		||||
        // Rozdelenie riadku na meno a počet hlasov
 | 
			
		||||
        if (sscanf(line, "%s %d", name, &votes) != 2) {
 | 
			
		||||
            printf("CHYBA: Neplatny zapis na riadku.\n");
 | 
			
		||||
            return 1;
 | 
			
		||||
        }
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    // Ak sme načítali aspoň jedného študenta, zoradíme ich podľa počtu hlasov a mena
 | 
			
		||||
    if (num_students > 0) {
 | 
			
		||||
        qsort(students, num_students, sizeof(struct student), compare);
 | 
			
		||||
        // Pridanie študenta do databázy
 | 
			
		||||
        addStudent(database, name, votes, &count);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Výpis výsledkov
 | 
			
		||||
        printf("Vysledky:\n");
 | 
			
		||||
        for (int i = 0; i < num_students; i++) {
 | 
			
		||||
            printf("%d %s\n", students[i].votes, students[i].name);
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        // Ak sme nenenačítali žiadneho študenta, vypíšeme chybovú správu
 | 
			
		||||
        printf("CHYBA: Ziadny zaznam na vstupe.\n");
 | 
			
		||||
    printf("\nVysledky:\n");
 | 
			
		||||
    for (int i = 0; i < count; i++) {
 | 
			
		||||
        printf("%d %s\n", database[i].votes, database[i].name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user