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 <stdlib.h> | ||||||
| #include <string.h> | #include <string.h> | ||||||
| 
 | 
 | ||||||
| #define SIZE 100 | #define MAX_SIZE 100 | ||||||
|  | #define MAX_STUDENTS 100 | ||||||
| 
 | 
 | ||||||
| typedef struct { | // Štruktúra pre reprezentáciu študenta
 | ||||||
|     char* meno; | struct Student { | ||||||
|     int hlasov; |     char name[MAX_SIZE]; | ||||||
| } Student; |     int votes; | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
| int find_student_index(Student* students, int num_students, const char* meno) { | // Porovnávacia funkcia pre triedenie študentov
 | ||||||
|     for (int i = 0; i < num_students; i++) { | int compare(const void *p1, const void *p2) { | ||||||
|         if (strcmp(students[i].meno, meno) == 0) { |     const struct Student *s1 = (const struct Student *)p1; | ||||||
|             return i; // Nájdený študent
 |     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
 |     // Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
 | ||||||
| } |     return strcmp(s1->name, s2->name); | ||||||
| 
 |  | ||||||
| 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); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main() { | 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; |     int num_students = 0; | ||||||
|     Student* students = NULL; |  | ||||||
| 
 | 
 | ||||||
|     while (fgets(line, SIZE, stdin) != NULL) { |     // Načítame každý riadok zo súboru
 | ||||||
|         char* endptr; |     char line[MAX_SIZE]; | ||||||
|         int hlasov = strtol(line, &endptr, 10); |     while (fgets(line, sizeof(line), file) != NULL) { | ||||||
|         if (endptr == line || *endptr != ' ' || hlasov < 0) { |         // Načítame počet hlasov a meno
 | ||||||
|             printf("CHYBA: Neplatny zapis na riadku.\n"); |         int votes; | ||||||
|             return 1; |         char name[MAX_SIZE]; | ||||||
|         } |         if (sscanf(line, "%d %99[^\n]", &votes, name) == 2) { | ||||||
|         endptr++; // Preskočenie medzery
 |             // Uložíme študenta do pola študentov
 | ||||||
| 
 |             strcpy(students[num_students].name, name); | ||||||
|         // Preskočenie medzier za počtom hlasov
 |             students[num_students].votes = votes; | ||||||
|         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; |  | ||||||
|             num_students++; |             num_students++; | ||||||
|         } else { |         } else { | ||||||
|             // Študent existuje, zvýšime počet hlasov
 |             fprintf(stderr, "CHYBA: Neplatný zápis na riadku\n"); | ||||||
|             students[index].hlasov += hlasov; |  | ||||||
|             free(meno); // Uvoľníme meno, pretože sme ho nevyužili
 |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Triedenie databázy
 |     // Uzavrieme súbor
 | ||||||
|     qsort(students, num_students, sizeof(Student), compare); |     fclose(file); | ||||||
| 
 | 
 | ||||||
|     // Výpis výsledkov
 |     // Zoradíme študentov podľa počtu hlasov
 | ||||||
|     printf("Vysledky:\n"); |     qsort(students, num_students, sizeof(struct Student), compare); | ||||||
|     for (int i = 0; i < num_students; i++) { |  | ||||||
|         printf("%d %s\n", students[i].hlasov, students[i].meno); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     // Uvoľnenie pamäte
 |     // Vypíšeme výsledky
 | ||||||
|  |     printf("Výsledky:\n"); | ||||||
|     for (int i = 0; i < num_students; i++) { |     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; |     return 0; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user