funguje
This commit is contained in:
		
							parent
							
								
									e4cd44c9ee
								
							
						
					
					
						commit
						462d6dcf0e
					
				@ -1,78 +1,48 @@
 | 
				
			|||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define SIZE 100
 | 
					#define SIZE 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Definícia štruktúry pre uchovanie informácií o študentovi
 | 
					 | 
				
			||||||
struct student {
 | 
					struct student {
 | 
				
			||||||
    char name[SIZE];
 | 
					    char name[SIZE];
 | 
				
			||||||
    int votes;
 | 
					    int votes;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Funkcia pre porovnanie dvoch záznamov
 | 
					int find_student(struct student* students, int size, const char* name) {
 | 
				
			||||||
int compare(const void* p1, const void* p2){
 | 
					    for (int i = 0; i < size; i++) {
 | 
				
			||||||
    struct student* s1 = (struct student*)p1;
 | 
					        if (strcmp(students[i].name, name) == 0) {
 | 
				
			||||||
    struct student* s2 = (struct student*)p2;
 | 
					            return i;
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    // Porovnávanie podľa počtu hlasov, ak sú rovnaké, potom podľa abecedy
 | 
					 | 
				
			||||||
    if (s1->votes != s2->votes) {
 | 
					 | 
				
			||||||
        return s2->votes - s1->votes; // Zoradenie zostupne podľa počtu hlasov
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        return strcmp(s1->name, s2->name); // Zoradenie abecedne
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    return -1;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main() {
 | 
					int main() {
 | 
				
			||||||
    struct student database[SIZE]; // Databáza študentov
 | 
					    struct student database[SIZE];
 | 
				
			||||||
    memset(database, 0, SIZE * sizeof(struct student)); // Inicializácia pamäte
 | 
					    memset(database, 0, SIZE * sizeof(struct student));
 | 
				
			||||||
    int size = 0; // Aktuálny počet študentov v databáze
 | 
					    int size = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Zoznam študentov s počtom hlasov
 | 
					    srand(time(NULL)); // Inicializácia generátora náhodných čísel
 | 
				
			||||||
    char input[][SIZE] = {
 | 
					 | 
				
			||||||
        "2 Bardos Mrtakrys",
 | 
					 | 
				
			||||||
        "1 Rita Umhi",
 | 
					 | 
				
			||||||
        "1 Prylenn Alak",
 | 
					 | 
				
			||||||
        "10 Lak'hi Elavorg",
 | 
					 | 
				
			||||||
        "3 Prylenn Alak",
 | 
					 | 
				
			||||||
        "3 Prylenn Alak",
 | 
					 | 
				
			||||||
        "3 Prylenn Alak",
 | 
					 | 
				
			||||||
        "1 Rita Umhi"
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    int input_size = sizeof(input) / sizeof(input[0]);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Vytvorenie databázy študentov zo vstupných údajov
 | 
					    // Generovanie náhodných dát do databázy
 | 
				
			||||||
    for (int i = 0; i < input_size; i++) {
 | 
					    for (int i = 0; i < 10; i++) {
 | 
				
			||||||
        int votes;
 | 
					        int votes = rand() % 11; // Generovanie náhodného počtu hlasov od 0 do 10
 | 
				
			||||||
        char name[SIZE];
 | 
					        char name[SIZE];
 | 
				
			||||||
        if (sscanf(input[i], "%d %[^\n]", &votes, name) != 2) {
 | 
					        sprintf(name, "Student %d", i+1); // Vytvorenie náhodného mena a priezviska študenta
 | 
				
			||||||
            printf("Chyba pri načítaní vstupu!\n");
 | 
					        int id = find_student(database, size, name);
 | 
				
			||||||
            return 1;
 | 
					        if (id < 0) {
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        // Hľadanie študenta v databáze
 | 
					 | 
				
			||||||
        int found = 0;
 | 
					 | 
				
			||||||
        for (int j = 0; j < size; j++) {
 | 
					 | 
				
			||||||
            if (strcmp(database[j].name, name) == 0) {
 | 
					 | 
				
			||||||
                database[j].votes += votes;
 | 
					 | 
				
			||||||
                found = 1;
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        // Pridanie nového študenta do databázy
 | 
					 | 
				
			||||||
        if (!found) {
 | 
					 | 
				
			||||||
            strcpy(database[size].name, name);
 | 
					            strcpy(database[size].name, name);
 | 
				
			||||||
            database[size].votes = votes;
 | 
					            database[size].votes = votes;
 | 
				
			||||||
            size++;
 | 
					            size++;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            database[id].votes += votes;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Zoradenie databázy podľa počtu hlasov a abecedne
 | 
					    // Výpis databázy
 | 
				
			||||||
    qsort(database, size, sizeof(struct student), compare);
 | 
					    printf("Databaza:\n");
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    // Výpis výsledkov
 | 
					 | 
				
			||||||
    printf("Vysledky:\n");
 | 
					 | 
				
			||||||
    for (int i = 0; i < size; i++) {
 | 
					    for (int i = 0; i < size; i++) {
 | 
				
			||||||
        printf("%d %s\n", database[i].votes, database[i].name);
 | 
					        printf("%d %s\n", database[i].votes, database[i].name);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user