56 lines
1.3 KiB
C
56 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
#define SIZE 100
|
|
|
|
struct student {
|
|
char name[SIZE];
|
|
int votes;
|
|
};
|
|
|
|
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;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int main() {
|
|
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
|
|
|
|
// Definícia vstupu
|
|
char input[] = "10 Terian Dis\n";
|
|
|
|
// Zobrazenie definície vstupu
|
|
printf("Vstup:\n%s\n", input);
|
|
|
|
// Čítanie vstupu a vytváranie databázy
|
|
int votes;
|
|
char name[SIZE];
|
|
sscanf(input, "%d %s %s", &votes, name, name + strlen(name) + 1); // Načítanie počtu hlasov, mena a priezviska
|
|
|
|
int id = find_student(database, size, name);
|
|
if (id < 0) {
|
|
strcpy(database[size].name, name);
|
|
database[size].votes = votes;
|
|
size++;
|
|
} else {
|
|
database[id].votes += votes;
|
|
}
|
|
|
|
// Výpis databázy
|
|
printf("\nVysledky:\n");
|
|
printf("%d %s\n", database[0].votes, input + 3); // Vypíše počet hlasov a pôvodný vstup od 4. znaku
|
|
|
|
return 0;
|
|
}
|
|
|