Добавить du4/program.c
This commit is contained in:
		
							parent
							
								
									848580647e
								
							
						
					
					
						commit
						e4788b3193
					
				
							
								
								
									
										77
									
								
								du4/program.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								du4/program.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#define SIZE 100 // Maximálny počet študentov a dĺžka mena
 | 
			
		||||
 | 
			
		||||
struct student {
 | 
			
		||||
    char name[SIZE]; // Meno študenta
 | 
			
		||||
    int votes; // Počet hlasov pre študenta
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct student database[SIZE]; // Databáza študentov
 | 
			
		||||
int size = 0;
 | 
			
		||||
 | 
			
		||||
// Funkcia na nájdenie študenta podľa mena v databáze
 | 
			
		||||
int find_student(struct student *students, int size, const char *name) {
 | 
			
		||||
    for (int i = 0; i < size; i++) {
 | 
			
		||||
        if (strcmp(students[i].name, name) == 0) { // Porovnanie mien, či sú rovnaké
 | 
			
		||||
            return i;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Porovnávacia funkcia pre qsort na zoradenie študentov
 | 
			
		||||
int compare(const void *p1, const void *p2) {
 | 
			
		||||
    struct student *s1 = (struct student *)p1;
 | 
			
		||||
    struct student *s2 = (struct student *)p2;
 | 
			
		||||
    if (s2->votes != s1->votes) {
 | 
			
		||||
        return s2->votes - s1->votes; // Triedenie zostupne podľa hlasov
 | 
			
		||||
    }
 | 
			
		||||
    return strcmp(s1->name, s2->name); // Triedenie lexikograficky, ak sú hlasy rovnaké
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    memset(database, 0, SIZE * sizeof(struct student));
 | 
			
		||||
    char line[SIZE];
 | 
			
		||||
    while (fgets(line, SIZE, stdin) != NULL) {
 | 
			
		||||
        char *end = NULL;
 | 
			
		||||
        int votes = strtol(line, &end, 10);
 | 
			
		||||
        if (votes == 0) {
 | 
			
		||||
            continue; // Neplatný vstup, preskočiť
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Preskočiť medzeru po čísle hlasov
 | 
			
		||||
        while (*end == ' ') {
 | 
			
		||||
            end++;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        char name[SIZE];
 | 
			
		||||
        memset(name, 0, SIZE);
 | 
			
		||||
        int name_len = strlen(end);
 | 
			
		||||
        if (name_len > 0 && end[name_len - 1] == '\n') { // Odstrániť nový riadok na konci
 | 
			
		||||
            end[name_len - 1] = '\0';
 | 
			
		||||
        }
 | 
			
		||||
        strcpy(name, end);
 | 
			
		||||
        
 | 
			
		||||
        int id = find_student(database, size, name); // Hľadanie študenta v databáze
 | 
			
		||||
        if (id < 0) { // Nový študent
 | 
			
		||||
            if (size < SIZE) { // Kontrola, či je miesto v databáze
 | 
			
		||||
                strcpy(database[size].name, name); // Uloženie mena nového študenta
 | 
			
		||||
                database[size].votes = votes; // Nastavenie hlasov pre nového študenta
 | 
			
		||||
                size++;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            database[id].votes += votes; // Pridanie hlasov existujúcemu študentovi
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    qsort(database, size, sizeof(struct student), compare); // Zoradenie databázy podľa hlasov a mien
 | 
			
		||||
    
 | 
			
		||||
    for (int i = 0; i < size; i++) {
 | 
			
		||||
        printf("%d %s\n", database[i].votes, database[i].name); // Výpis výsledkov
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user