Přidat du4/program.c

This commit is contained in:
Mykola Syniavskyi 2025-03-20 23:48:26 +00:00
parent c084d57db7
commit 71acba7113

63
du4/program.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <string.h>
#define MAX_NAMELEN 50
#define MAX_STUDENTS 100
typedef struct {
char name[MAX_NAMELEN];
int votes;
} Student;
int main() {
Student studenty[MAX_STUDENTS];
char name[MAX_NAMELEN];
char line[255];
int znayd = 0;
int golosa=0;
int kilkstudentiv = 0;
while (fgets(line, sizeof(line), stdin)) {
if (sscanf(line, "%d %[^\n]", &golosa, name) != 2 || golosa <= 0) {
if (kilkstudentiv == 0) {
printf("Chyba: Neplatný vstup.\n");
return 1;
}
break;
}
for (int i = 0; i < kilkstudentiv; i++) {
if (strcmp(studenty[i].name, name) == 0) {
studenty[i].votes += golosa;
znayd = 1;
break;
}
}
if (znayd == 0) {
strcpy(studenty[kilkstudentiv].name, name);
studenty[kilkstudentiv].votes = golosa;
kilkstudentiv++;
}
}
if (kilkstudentiv == 0) {
printf("Chyba: Neplatný vstup.\n");
return 1; }
for (int i = 0; i < kilkstudentiv - 1; i++) {
for (int j = i + 1; j < kilkstudentiv; j++) {
if (studenty[i].votes < studenty[j].votes ||
(studenty[i].votes == studenty[j].votes && strcmp(studenty[i].name, studenty[j].name) > 0)) {
Student change = studenty[i];
studenty[i] = studenty[j];
studenty[j] = change;
}
}
}
printf("Vysledky:\n");
for (int i = 0; i < kilkstudentiv; i++) {
printf("%d %s\n", studenty[i].votes, studenty[i].name);
}
return 0;
}