test
This commit is contained in:
		
							parent
							
								
									589aeab644
								
							
						
					
					
						commit
						0cb54e54df
					
				
							
								
								
									
										109
									
								
								cv5/program.c
									
									
									
									
									
								
							
							
						
						
									
										109
									
								
								cv5/program.c
									
									
									
									
									
								
							| @ -3,76 +3,81 @@ | |||||||
| #include <string.h> | #include <string.h> | ||||||
| 
 | 
 | ||||||
| typedef struct student { | typedef struct student { | ||||||
|   char *name; |     char *name; | ||||||
|   int vote_count; |     int vote_count; | ||||||
| } student_t; | } student_t; | ||||||
| 
 | 
 | ||||||
| int compare_students(const void *a, const void *b) { | int compare_students(const void *a, const void *b) { | ||||||
|   const student_t *student_a = (const student_t *)a; |     const student_t *student_a = (const student_t *)a; | ||||||
|   const student_t *student_b = (const student_t *)b; |     const student_t *student_b = (const student_t *)b; | ||||||
| 
 | 
 | ||||||
|   if (student_a->vote_count > student_b->vote_count) { |     if (student_a->vote_count > student_b->vote_count) { | ||||||
|     return -1; |         return -1; | ||||||
|   } else if (student_a->vote_count < student_b->vote_count) { |     } else if (student_a->vote_count < student_b->vote_count) { | ||||||
|     return 1; |         return 1; | ||||||
|   } |     } | ||||||
| 
 | 
 | ||||||
|   return strcmp(student_a->name, student_b->name); |     return strcmp(student_a->name, student_b->name); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main() { | int main() { | ||||||
|   student_t *students = NULL; |     student_t *students = NULL; | ||||||
|   int students_capacity = 0; |     int students_capacity = 0; | ||||||
|   int student_count = 0; |     int student_count = 0; | ||||||
| 
 | 
 | ||||||
|   char line[1024]; |     char line[1024]; | ||||||
|   while (fgets(line, sizeof(line), stdin)) { |     while (fgets(line, sizeof(line), stdin)) { | ||||||
|     int vote_count; |         int vote_count; | ||||||
|     char name[1024]; |         char name[1024]; | ||||||
|     sscanf(line, "%d %[^\n]", &vote_count, name); |         sscanf(line, "%d %[^\n]", &vote_count, name); | ||||||
| 
 | 
 | ||||||
|     int existing_student_index = -1; |         int existing_student_index = -1; | ||||||
|     for (int i = 0; i < student_count; i++) { |         for (int i = 0; i < student_count; i++) { | ||||||
|       if (strcmp(students[i].name, name) == 0) { |             if (strcmp(students[i].name, name) == 0) { | ||||||
|         existing_student_index = i; |                 existing_student_index = i; | ||||||
|         break; |                 break; | ||||||
|       } |             } | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     if (existing_student_index != -1) { |  | ||||||
|       students[existing_student_index].vote_count += vote_count; |  | ||||||
|     } else { |  | ||||||
|       if (student_count >= students_capacity) { |  | ||||||
|         students_capacity = (students_capacity + 1) * 2; |  | ||||||
|         student_t *new_students = realloc(students, students_capacity * sizeof(student_t)); |  | ||||||
|         if (new_students == NULL) { |  | ||||||
|           fprintf(stderr, "Error: Memory reallocation failed\n"); |  | ||||||
|           return 1; |  | ||||||
|         } |         } | ||||||
|         students = new_students; |  | ||||||
|       } |  | ||||||
| 
 | 
 | ||||||
|       students[student_count].name = strdup(name); |         if (existing_student_index != -1) { | ||||||
|       students[student_count].vote_count = vote_count; |             students[existing_student_index].vote_count += vote_count; | ||||||
|       student_count++; |         } else { | ||||||
|  |             if (student_count >= students_capacity) { | ||||||
|  |                 students_capacity = (students_capacity + 1) * 2; | ||||||
|  |                 student_t *new_students = realloc(students, students_capacity * sizeof(student_t)); | ||||||
|  |                 if (new_students == NULL) { | ||||||
|  |                     fprintf(stderr, "Error: Memory reallocation failed\n"); | ||||||
|  |                     return 1; | ||||||
|  |                 } | ||||||
|  |                 students = new_students; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             students[student_count].name = strdup(name); | ||||||
|  |             students[student_count].vote_count = vote_count; | ||||||
|  |             student_count++; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |    | ||||||
|  |         if (student_count == 1) { | ||||||
|  |             printf("Vysledky:\n"); | ||||||
|  |             printf("%d %s\n", students[0].vote_count, students[0].name); | ||||||
|  |             free(students[0].name); | ||||||
|  |             free(students); | ||||||
|  |             return 0; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|   if (student_count == 1) { |  | ||||||
|     printf("Vysledky:\n"); |  | ||||||
|     printf("%d %s\n", students[0].vote_count, students[0].name); |  | ||||||
|   } else { |  | ||||||
|     qsort(students, student_count, sizeof(student_t), compare_students); |     qsort(students, student_count, sizeof(student_t), compare_students); | ||||||
|  | 
 | ||||||
|     printf("Vysledky:\n"); |     printf("Vysledky:\n"); | ||||||
|     for (int i = 0; i < student_count; i++) { |     for (int i = 0; i < student_count; i++) { | ||||||
|       printf("%d %s\n", students[i].vote_count, students[i].name); |         printf("%d %s\n", students[i].vote_count, students[i].name); | ||||||
|     } |     } | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|   for (int i = 0; i < student_count; i++) { |     for (int i = 0; i < student_count; i++) { | ||||||
|     free(students[i].name); |         free(students[i].name); | ||||||
|   } |     } | ||||||
|   free(students); |     free(students); | ||||||
| 
 | 
 | ||||||
|   return 0; |     return 0; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user