cv5
This commit is contained in:
		
							parent
							
								
									67846c51bd
								
							
						
					
					
						commit
						9318117a6a
					
				@ -4,81 +4,80 @@
 | 
			
		||||
 | 
			
		||||
#define SIZE 100
 | 
			
		||||
 | 
			
		||||
// Štruktúra pre uloženie informácií o študentovi
 | 
			
		||||
struct student {
 | 
			
		||||
    char name[SIZE];
 | 
			
		||||
    int votes;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Funkcia pre porovnanie dvoch študentov pri triedení
 | 
			
		||||
// Funkcia pre porovnanie dvoch študentov
 | 
			
		||||
int compare(const void* p1, const void* p2) {
 | 
			
		||||
    const struct student* s1 = (const struct student*)p1;
 | 
			
		||||
    const struct student* s2 = (const struct student*)p2;
 | 
			
		||||
    struct student* s1 = (struct student*)p1;
 | 
			
		||||
    struct student* s2 = (struct student*)p2;
 | 
			
		||||
    
 | 
			
		||||
    // Porovnanie podľa počtu hlasov
 | 
			
		||||
    int result = s2->votes - s1->votes;
 | 
			
		||||
    if (result == 0) {
 | 
			
		||||
    // Porovnáme najprv počet hlasov
 | 
			
		||||
    if (s1->votes != s2->votes) {
 | 
			
		||||
        return s2->votes - s1->votes; // Zoradíme zostupne podľa počtu hlasov
 | 
			
		||||
    } else {
 | 
			
		||||
        // Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
 | 
			
		||||
        return strcmp(s1->name, s2->name);
 | 
			
		||||
    }
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Funkcia na vyhľadanie študenta 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) {
 | 
			
		||||
            return i; // Nájdený študent
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -1; // Študent nebol nájdený
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    struct student students[SIZE];
 | 
			
		||||
    memset(students, 0, SIZE * sizeof(struct student));
 | 
			
		||||
    int size = 0;
 | 
			
		||||
 | 
			
		||||
    char line[SIZE];
 | 
			
		||||
    char name[SIZE];
 | 
			
		||||
    int votes;
 | 
			
		||||
 | 
			
		||||
    //printf("Enter votes for 'Student of the Year' contest (or 'q' to quit):\n");
 | 
			
		||||
    struct student database[SIZE]; // Databáza študentov
 | 
			
		||||
    memset(database, 0, SIZE * sizeof(struct student)); // Inicializácia pamäte
 | 
			
		||||
    
 | 
			
		||||
    int size = 0; // Aktuálny počet študentov v databáze
 | 
			
		||||
    
 | 
			
		||||
    // Načítanie hlasov zo vstupu
 | 
			
		||||
    char line[SIZE];
 | 
			
		||||
    while (fgets(line, SIZE, stdin) != NULL) {
 | 
			
		||||
        // Kontrola ukončenia vstupu
 | 
			
		||||
        if (strcmp(line, "q\n") == 0 || strcmp(line, "Q\n") == 0) {
 | 
			
		||||
        char name[SIZE];
 | 
			
		||||
        int votes;
 | 
			
		||||
        // Rozdelenie riadku na počet hlasov a meno študenta
 | 
			
		||||
        if (sscanf(line, "%d %s", &votes, name) != 2) {
 | 
			
		||||
            fprintf(stderr, "Chybný formát vstupu!\n");
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Načítanie počtu hlasov a mena zo vstupu
 | 
			
		||||
        if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
 | 
			
		||||
            //printf("Invalid input format. Exiting.\n");
 | 
			
		||||
            return 1;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Vyhľadanie študenta v databáze
 | 
			
		||||
        int index = -1;
 | 
			
		||||
        for (int i = 0; i < size; i++) {
 | 
			
		||||
            if (strcmp(students[i].name, name) == 0) {
 | 
			
		||||
                index = i;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Ak študent nie je v databáze, pridáme ho
 | 
			
		||||
        int index = find_student(database, size, name);
 | 
			
		||||
        if (index == -1) {
 | 
			
		||||
            // Nový študent
 | 
			
		||||
            if (size < SIZE) {
 | 
			
		||||
                strcpy(students[size].name, name);
 | 
			
		||||
                students[size].votes = votes;
 | 
			
		||||
                // Pridanie študenta do databázy
 | 
			
		||||
                strcpy(database[size].name, name);
 | 
			
		||||
                database[size].votes = votes;
 | 
			
		||||
                size++;
 | 
			
		||||
            } else {
 | 
			
		||||
                printf("Database is full. Exiting.\n");
 | 
			
		||||
                return 1;
 | 
			
		||||
                fprintf(stderr, "Databáza je plná!\n");
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            // Ak študent už existuje, zvýšime počet hlasov
 | 
			
		||||
            students[index].votes += votes;
 | 
			
		||||
            // Existujúci študent, pridáme hlasy
 | 
			
		||||
            database[index].votes += votes;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Triedenie databázy podľa počtu hlasov a mena
 | 
			
		||||
    qsort(students, size, sizeof(struct student), compare);
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    // Zoradenie databázy podľa počtu hlasov a mena
 | 
			
		||||
    qsort(database, size, sizeof(struct student), compare);
 | 
			
		||||
    
 | 
			
		||||
    // Výpis výsledkov
 | 
			
		||||
    printf("\nResults:\n");
 | 
			
		||||
    for (int i = 0; i < size; i++) {
 | 
			
		||||
        printf("%d %s\n", students[i].votes, students[i].name);
 | 
			
		||||
    printf("Výsledky:\n");
 | 
			
		||||
    for (int i = 0; i < size; ++i) {
 | 
			
		||||
        printf("%d %s\n", database[i].votes, database[i].name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user