du2
This commit is contained in:
parent
baaae4526f
commit
af1a1ee824
@ -0,0 +1,99 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define SIZE 100
|
||||
|
||||
struct student{
|
||||
char name[SIZE];
|
||||
int votes;
|
||||
};
|
||||
|
||||
int Fstudent(struct student* students, int size, const char* name){
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (strcmp(students[i].name, name) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int compare(const void* p1, const void* p2){
|
||||
struct student* s1 = (struct student*)p1;
|
||||
struct student* s2 = (struct student*)p2;
|
||||
// s1->votes je počet hlasov
|
||||
// s1->name je meno študenta
|
||||
if (s1->votes != s2->votes) {
|
||||
return s2->votes - s1->votes;
|
||||
}
|
||||
|
||||
return strcmp(s1->name, s2->name);
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
struct student databaza[SIZE];
|
||||
memset(databaza,0,SIZE*sizeof(struct student));
|
||||
int size = 0;
|
||||
|
||||
while(1){
|
||||
char line[SIZE];
|
||||
memset(line,0,SIZE);
|
||||
char* r = fgets(line,SIZE,stdin);
|
||||
if (r == NULL){
|
||||
break;
|
||||
// Nastal koniec načítania
|
||||
}
|
||||
|
||||
char* end = NULL;
|
||||
int value = strtol(line,&end,10);
|
||||
if (value == 0){
|
||||
break;
|
||||
// Premena sa nepodarila
|
||||
}
|
||||
if (*end != ' ') {
|
||||
break;
|
||||
}
|
||||
|
||||
char name[SIZE];
|
||||
memset(name, 0, SIZE);
|
||||
|
||||
char* zaciatok_mena = end + 1;
|
||||
int velkost_mena = strlen(zaciatok_mena) - 1;
|
||||
|
||||
memcpy(name,zaciatok_mena,velkost_mena);
|
||||
name[velkost_mena] = '\0';
|
||||
|
||||
if (velkost_mena <= 1) {
|
||||
break;
|
||||
}
|
||||
int id = Fstudent(databaza, size, name);
|
||||
|
||||
if (id< 0){
|
||||
if (size >= SIZE) {
|
||||
break;
|
||||
}
|
||||
// Skopirujte zaznam na posledne miesto v poli
|
||||
memcpy(databaza[size].name,name,velkost_mena + 1);
|
||||
databaza[size].votes = value;
|
||||
// Zvacsite pocet zaznamov
|
||||
size+=1;
|
||||
}else{
|
||||
databaza[id].votes += value;
|
||||
}
|
||||
}
|
||||
if (size == 0) {
|
||||
printf("Chyba\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
qsort(databaza, size, sizeof(struct student), compare);
|
||||
|
||||
printf("Vysledky:\n");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user