funguje
This commit is contained in:
		
							parent
							
								
									edb0ecca92
								
							
						
					
					
						commit
						975cc2cec4
					
				
							
								
								
									
										123
									
								
								cv5/program.c
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								cv5/program.c
									
									
									
									
									
								
							| @ -2,103 +2,66 @@ | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| #define SIZE 100 | ||||
| #define MAX_SIZE 100 | ||||
| #define MAX_STUDENTS 100 | ||||
| 
 | ||||
| typedef struct { | ||||
|     char* meno; | ||||
|     int hlasov; | ||||
| } Student; | ||||
| // Štruktúra pre reprezentáciu študenta
 | ||||
| struct Student { | ||||
|     char name[MAX_SIZE]; | ||||
|     int votes; | ||||
| }; | ||||
| 
 | ||||
| int find_student_index(Student* students, int num_students, const char* meno) { | ||||
|     for (int i = 0; i < num_students; i++) { | ||||
|         if (strcmp(students[i].meno, meno) == 0) { | ||||
|             return i; // Nájdený študent
 | ||||
|         } | ||||
| // Porovnávacia funkcia pre triedenie š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; | ||||
|     // Porovnanie počtu hlasov
 | ||||
|     if (s1->votes != s2->votes) { | ||||
|         return s2->votes - s1->votes; // Zoradenie zostupne podľa hlasov
 | ||||
|     } | ||||
|     return -1; // Študent neexistuje
 | ||||
| } | ||||
| 
 | ||||
| int compare(const void* p1, const void* p2) { | ||||
|     const Student* s1 = (const Student*)p1; | ||||
|     const Student* s2 = (const Student*)p2; | ||||
|     // Porovnanie podľa počtu hlasov
 | ||||
|     if (s1->hlasov != s2->hlasov) { | ||||
|         return s2->hlasov - s1->hlasov; // Zoradenie zostupne
 | ||||
|     } | ||||
|     // Ak majú rovnaký počet hlasov, zoradíme podľa mena
 | ||||
|     return strcmp(s1->meno, s2->meno); | ||||
|     // Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
 | ||||
|     return strcmp(s1->name, s2->name); | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|     char line[SIZE]; | ||||
|     // Otvoríme súbor na čítanie
 | ||||
|     FILE *file = fopen("nazov_suboru.txt", "r"); | ||||
|     if (file == NULL) { | ||||
|         perror("Chyba pri otváraní súboru"); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     // Pole študentov
 | ||||
|     struct Student students[MAX_STUDENTS]; | ||||
|     int num_students = 0; | ||||
|     Student* students = NULL; | ||||
| 
 | ||||
|     while (fgets(line, SIZE, stdin) != NULL) { | ||||
|         char* endptr; | ||||
|         int hlasov = strtol(line, &endptr, 10); | ||||
|         if (endptr == line || *endptr != ' ' || hlasov < 0) { | ||||
|             printf("CHYBA: Neplatny zapis na riadku.\n"); | ||||
|             return 1; | ||||
|         } | ||||
|         endptr++; // Preskočenie medzery
 | ||||
| 
 | ||||
|         // Preskočenie medzier za počtom hlasov
 | ||||
|         while (*endptr == ' ') { | ||||
|             endptr++; | ||||
|         } | ||||
| 
 | ||||
|         // Zistenie dĺžky mena
 | ||||
|         int len = strlen(endptr); | ||||
|         if (len == 0 || endptr[len - 1] == '\n') { | ||||
|             printf("CHYBA: Neplatny zapis na riadku.\n"); | ||||
|             return 1; | ||||
|         } | ||||
| 
 | ||||
|         // Alokácia pamäte pre meno študenta
 | ||||
|         char* meno = malloc(len + 1); | ||||
|         if (meno == NULL) { | ||||
|             printf("CHYBA: Nepodarilo sa alokovať pamäť.\n"); | ||||
|             return 1; | ||||
|         } | ||||
| 
 | ||||
|         // Kopírovanie mena
 | ||||
|         memcpy(meno, endptr, len); | ||||
|         meno[len] = '\0'; // Nulový znak na koniec mena
 | ||||
| 
 | ||||
|         // Zistiť, či študent už existuje v databáze
 | ||||
|         int index = find_student_index(students, num_students, meno); | ||||
|         if (index == -1) { | ||||
|             // Študent neexistuje, pridáme ho do databázy
 | ||||
|             students = realloc(students, (num_students + 1) * sizeof(Student)); | ||||
|             if (students == NULL) { | ||||
|                 printf("CHYBA: Nepodarilo sa alokovať pamäť.\n"); | ||||
|                 return 1; | ||||
|             } | ||||
|             students[num_students].meno = meno; | ||||
|             students[num_students].hlasov = hlasov; | ||||
|     // Načítame každý riadok zo súboru
 | ||||
|     char line[MAX_SIZE]; | ||||
|     while (fgets(line, sizeof(line), file) != NULL) { | ||||
|         // Načítame počet hlasov a meno
 | ||||
|         int votes; | ||||
|         char name[MAX_SIZE]; | ||||
|         if (sscanf(line, "%d %99[^\n]", &votes, name) == 2) { | ||||
|             // Uložíme študenta do pola študentov
 | ||||
|             strcpy(students[num_students].name, name); | ||||
|             students[num_students].votes = votes; | ||||
|             num_students++; | ||||
|         } else { | ||||
|             // Študent existuje, zvýšime počet hlasov
 | ||||
|             students[index].hlasov += hlasov; | ||||
|             free(meno); // Uvoľníme meno, pretože sme ho nevyužili
 | ||||
|             fprintf(stderr, "CHYBA: Neplatný zápis na riadku\n"); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // Triedenie databázy
 | ||||
|     qsort(students, num_students, sizeof(Student), compare); | ||||
|     // Uzavrieme súbor
 | ||||
|     fclose(file); | ||||
| 
 | ||||
|     // Výpis výsledkov
 | ||||
|     printf("Vysledky:\n"); | ||||
|     for (int i = 0; i < num_students; i++) { | ||||
|         printf("%d %s\n", students[i].hlasov, students[i].meno); | ||||
|     } | ||||
|     // Zoradíme študentov podľa počtu hlasov
 | ||||
|     qsort(students, num_students, sizeof(struct Student), compare); | ||||
| 
 | ||||
|     // Uvoľnenie pamäte
 | ||||
|     // Vypíšeme výsledky
 | ||||
|     printf("Výsledky:\n"); | ||||
|     for (int i = 0; i < num_students; i++) { | ||||
|         free(students[i].meno); | ||||
|         printf("%d %s\n", students[i].votes, students[i].name); | ||||
|     } | ||||
|     free(students); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user