123
This commit is contained in:
parent
f7323ac3c7
commit
29911df6d3
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#define SIZE 100
|
#define SIZE 100
|
||||||
|
|
||||||
struct student {
|
struct student{
|
||||||
char name[SIZE];
|
char name[SIZE];
|
||||||
int votes;
|
int votes;
|
||||||
};
|
};
|
||||||
@ -20,6 +20,7 @@ int find_student(const char* name){
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare(const void* a, const void* b) {
|
int compare(const void* a, const void* b) {
|
||||||
struct student* s1 = (struct student*)a;
|
struct student* s1 = (struct student*)a;
|
||||||
struct student* s2 = (struct student*)b;
|
struct student* s2 = (struct student*)b;
|
||||||
@ -36,18 +37,30 @@ int compare(const void* a, const void* b) {
|
|||||||
int main() {
|
int main() {
|
||||||
char line[SIZE];
|
char line[SIZE];
|
||||||
memset(databaza, 0, sizeof(databaza));
|
memset(databaza, 0, sizeof(databaza));
|
||||||
|
|
||||||
while (fgets(line, SIZE, stdin)) {
|
while (fgets(line, SIZE, stdin)) {
|
||||||
char* end = NULL;
|
char* end = NULL;
|
||||||
int votes = strtol(line, &end, 10);
|
int votes = strtol(line, &end, 10);
|
||||||
|
if (votes == 0 && end == line) {
|
||||||
|
printf("Chyba: Neplatny format vstupu!\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
while (*end == ' ') {
|
||||||
|
end++;
|
||||||
|
}
|
||||||
|
if (*end == '\0' || *end == '\n') {
|
||||||
|
printf("Chyba: Neplatny format - chyba meno!\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
char name[SIZE];
|
char name[SIZE];
|
||||||
memset(name, 0, SIZE);
|
memset(name, 0, SIZE);
|
||||||
strncpy(name, end + 1, strlen(end + 1) - 1);
|
strncpy(name, end, SIZE - 1);
|
||||||
|
name[strcspn(name, "\n")] = '\0';
|
||||||
|
|
||||||
int id = find_student(name);
|
int id = find_student(name);
|
||||||
if (id < 0){
|
if (id < 0) {
|
||||||
if (size >= SIZE){
|
if (size >= SIZE) {
|
||||||
printf("Chyba: Databaza je plna!\n");
|
printf("Chyba: Databaza je plna!\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -55,14 +68,12 @@ int main() {
|
|||||||
strncpy(databaza[size].name, name, SIZE - 1);
|
strncpy(databaza[size].name, name, SIZE - 1);
|
||||||
databaza[size].votes = votes;
|
databaza[size].votes = votes;
|
||||||
size++;
|
size++;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
databaza[id].votes += votes;
|
databaza[id].votes += votes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size == 0){
|
if (size == 0) {
|
||||||
printf("Chyba: Nebol nacitany ziadny platny zaznam!\n");
|
printf("Chyba: Nebol nacitany ziadny platny zaznam!\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -75,3 +86,4 @@ int main() {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user