12
This commit is contained in:
parent
ce33e08499
commit
6185db179b
@ -12,47 +12,69 @@ struct student {
|
|||||||
struct student databaza[SIZE];
|
struct student databaza[SIZE];
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
int find_student(const char* name) {
|
int find_student(const char* name){
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++){
|
||||||
if (strcmp(databaza[i].name, name) == 0) {
|
if (strcmp(databaza[i].name, name) == 0){
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
int compare(const void* a, const void* b) {
|
||||||
|
struct student* s1 = (struct student*)a;
|
||||||
|
struct student* s2 = (struct student*)b;
|
||||||
|
|
||||||
|
if (s1->votes > s2->votes) {
|
||||||
|
return -1;
|
||||||
|
} else if (s1->votes < s2->votes) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return strcmp(s1->name, s2->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
if (votes <= 0 || *end != ' '){
|
||||||
|
printf("Chyba: Nespravny format vstupu!\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 + 1, strlen(end + 1) - 1);
|
||||||
|
|
||||||
int id = find_student(name);
|
int id = find_student(name);
|
||||||
|
if (id < 0){
|
||||||
|
if (size >= SIZE){
|
||||||
|
printf("Chyba: Databaza je plna!\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (id < 0) {
|
|
||||||
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){
|
||||||
|
printf("Chyba: Nebol nacitany ziadny platny zaznam!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
qsort(databaza, size, sizeof(struct student), compare);
|
||||||
|
|
||||||
printf("Vysledky:\n");
|
printf("Vysledky:\n");
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
printf("%d %s\n", databaza[i].votes, databaza[i].name);
|
printf("%d %s\n", databaza[i].votes, databaza[i].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user