pvjc24/cv5/program.c

55 lines
1.3 KiB
C
Raw Normal View History

2024-03-13 09:55:11 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-03-20 16:26:54 +00:00
#include <time.h>
2024-03-13 09:55:11 +00:00
2024-03-20 14:57:31 +00:00
#define SIZE 100
2024-03-20 14:08:46 +00:00
2024-03-20 14:57:31 +00:00
struct student {
2024-03-20 15:42:19 +00:00
char name[SIZE];
2024-03-20 14:57:31 +00:00
int votes;
};
2024-03-13 09:55:11 +00:00
2024-03-20 16:26:54 +00:00
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;
}
2024-03-20 15:22:08 +00:00
}
2024-03-20 16:26:54 +00:00
return -1;
2024-03-20 14:57:31 +00:00
}
2024-03-20 15:22:08 +00:00
int main() {
2024-03-20 16:26:54 +00:00
struct student database[SIZE];
memset(database, 0, SIZE * sizeof(struct student));
int size = 0;
srand(time(NULL)); // Inicializácia generátora náhodných čísel
2024-03-20 16:29:22 +00:00
// Čítanie vstupu a vytváranie databázy
char input[SIZE];
while (fgets(input, SIZE, stdin) != NULL) {
int votes;
2024-03-20 15:42:19 +00:00
char name[SIZE];
2024-03-20 16:29:22 +00:00
sscanf(input, "%d %s %s", &votes, name, name + strlen(name) + 1); // Načítanie počtu hlasov, mena a priezviska
2024-03-20 16:26:54 +00:00
int id = find_student(database, size, name);
if (id < 0) {
2024-03-20 15:59:06 +00:00
strcpy(database[size].name, name);
database[size].votes = votes;
size++;
2024-03-20 16:26:54 +00:00
} else {
database[id].votes += votes;
2024-03-20 15:59:06 +00:00
}
2024-03-20 15:22:08 +00:00
}
2024-03-20 16:26:54 +00:00
// Výpis databázy
printf("Databaza:\n");
2024-03-20 15:22:08 +00:00
for (int i = 0; i < size; i++) {
printf("%d %s\n", database[i].votes, database[i].name);
}
2024-03-20 16:26:54 +00:00
2024-03-20 15:22:08 +00:00
return 0;
2024-03-13 09:55:11 +00:00
}