pvjc24/cv5/program.c

56 lines
1.3 KiB
C
Raw Normal View History

2024-03-21 22:47:06 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-03-21 23:01:55 +00:00
#define SIZE 100
2024-03-21 22:47:06 +00:00
2024-03-21 23:01:55 +00:00
// Štruktúra pre reprezentáciu študenta
typedef struct {
char name[SIZE];
2024-03-21 22:47:06 +00:00
int votes;
2024-03-21 23:01:55 +00:00
} Student;
2024-03-21 22:47:06 +00:00
2024-03-21 23:01:55 +00:00
// Funkcia na pridanie študenta do databázy
void addStudent(Student database[], char name[], int votes, int *count) {
strcpy(database[*count].name, name);
database[*count].votes = votes;
(*count)++;
2024-03-21 22:47:06 +00:00
}
int main() {
2024-03-21 23:01:55 +00:00
char line[SIZE];
char name[SIZE];
int votes;
// Databáza študentov
Student database[SIZE];
int count = 0;
printf("Zadajte mená študentov a počet hlasov:\n");
2024-03-21 22:58:42 +00:00
while (1) {
2024-03-21 23:01:55 +00:00
// Načítanie riadku zo štandardného vstupu
if (fgets(line, SIZE, stdin) == NULL || line[0] == '\n') {
break; // Koniec načítania
2024-03-21 22:58:42 +00:00
}
2024-03-21 23:01:55 +00:00
// Rozdelenie riadku na meno a počet hlasov
if (sscanf(line, "%s %d", name, &votes) != 2) {
2024-03-21 22:58:42 +00:00
printf("CHYBA: Neplatny zapis na riadku.\n");
2024-03-21 23:01:55 +00:00
continue;
2024-03-21 22:58:42 +00:00
}
2024-03-21 23:01:55 +00:00
// Pridanie študenta do databázy
addStudent(database, name, votes, &count);
2024-03-21 22:47:06 +00:00
}
2024-03-21 23:01:55 +00:00
// Výpis výsledkov
printf("\nVysledky:\n");
for (int i = 0; i < count; i++) {
printf("%d %s\n", database[i].votes, database[i].name);
2024-03-21 22:58:42 +00:00
}
2024-03-21 23:01:55 +00:00
2024-03-21 22:47:06 +00:00
return 0;
}