a
This commit is contained in:
parent
e0eab6984f
commit
ee2fcadb98
@ -1,13 +1,15 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_MENO 100
|
#define MAX_M 100
|
||||||
#define MAX_STUDENTI 100
|
#define MAX_S 100
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char meno[MAX_MENO];
|
char meno[MAX_M];
|
||||||
int hlasy;
|
int hlasy;
|
||||||
} Student;
|
} Student;
|
||||||
|
|
||||||
@ -26,40 +28,56 @@ int porovnaj(const void *a, const void *b) {
|
|||||||
Student *s1 = (Student *)a;
|
Student *s1 = (Student *)a;
|
||||||
Student *s2 = (Student *)b;
|
Student *s2 = (Student *)b;
|
||||||
|
|
||||||
|
|
||||||
if (s2->hlasy != s1->hlasy) {
|
if (s2->hlasy != s1->hlasy) {
|
||||||
return s2->hlasy - s1->hlasy;
|
return s2->hlasy - s1->hlasy;
|
||||||
}
|
}
|
||||||
|
|
||||||
return strcmp(s1->meno, s2->meno);
|
return strcmp(s1->meno, s2->meno);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int je_cislo(const char *retazec) {
|
||||||
|
while (*retazec) {
|
||||||
|
if (!isdigit(*retazec)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
retazec++;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Student studenti[MAX_STUDENTI];
|
Student studenti[MAX_S];
|
||||||
int pocet_studentov = 0;
|
int pocet_studentov = 0;
|
||||||
char meno[MAX_MENO];
|
char meno[MAX_M];
|
||||||
|
char vstup[MAX_M + 10];
|
||||||
int hlasy;
|
int hlasy;
|
||||||
|
int nacitane = 0;
|
||||||
|
|
||||||
|
|
||||||
while (scanf("%d ", &hlasy) == 1) {
|
while (fgets(vstup, sizeof(vstup), stdin) != NULL)
|
||||||
if (fgets(meno, MAX_MENO, stdin) == NULL) {
|
vstup[strcspn(vstup, "\n")] = 0;
|
||||||
printf("CHYBA\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
meno[strcspn(meno, "\n")] = 0;
|
|
||||||
|
|
||||||
if (hlasy <= 0 || strlen(meno) == 0) {
|
|
||||||
printf("CHYBA\n");
|
if (strlen(vstup) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (sscanf(vstup, "%d %[^\n]", &hlasy, meno) != 2 || hlasy <= 0) {
|
||||||
|
printf("Nepodarilo nacitat nic\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nacitane = 1;
|
||||||
|
|
||||||
int index = najdi_studenta(studenti, pocet_studentov, meno);
|
int index = najdi_studenta(studenti, pocet_studentov, meno);
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
studenti[index].hlasy += hlasy;
|
studenti[index].hlasy += hlasy;
|
||||||
} else {
|
} else {
|
||||||
if (pocet_studentov >= MAX_STUDENTI) {
|
if (pocet_studentov >= MAX_S) {
|
||||||
printf("CHYBA\n");
|
printf("Nepodarilo nacitat nic\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
strcpy(studenti[pocet_studentov].meno, meno);
|
strcpy(studenti[pocet_studentov].meno, meno);
|
||||||
@ -68,10 +86,16 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (!nacitane) {
|
||||||
|
printf("Nepodarilo nacitat nic\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
qsort(studenti, pocet_studentov, sizeof(Student), porovnaj);
|
qsort(studenti, pocet_studentov, sizeof(Student), porovnaj);
|
||||||
|
|
||||||
|
|
||||||
printf("Vysledky:\n");
|
printf("Vysledky:\n");
|
||||||
for (int i = 0; i < pocet_studentov; i++) {
|
for (int i = 0; i < pocet_studentov; i++) {
|
||||||
printf("%d %s\n", studenti[i].hlasy, studenti[i].meno);
|
printf("%d %s\n", studenti[i].hlasy, studenti[i].meno);
|
||||||
|
Loading…
Reference in New Issue
Block a user