pvjc23/du5/program.c

106 lines
2.4 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <ctype.h>
#define SIZE 100
struct student {
char name[SIZE];
int votes;
};
int find_student(struct student* students, int size, const char* nameo){
int con = 0;
for(int i = 0; i < size; i++){
if(strcmp (nameo, students[i].name) == 0){
con++;
return i;
}
}
if(con == 0){
return -1;
}
con = 0;
}
int compare_students(const void* a, const void* b) {
const struct student* student_a = (const struct student*) a;
const struct student* student_b = (const struct student*) b;
if (student_a->votes < student_b->votes) {
return 1;
}
else if (student_a->votes > student_b->votes) {
return -1;
}
else {
return strcmp(student_a->name, student_b->name);
}
}
int main(){
int STOP = 0;
int counter = 0;
struct student databaza[SIZE];
memset(databaza, 0, SIZE*sizeof(struct student));
int size = 0;
char line[SIZE];
memset(line, 0, SIZE);
char* r;
char* end = NULL;
int value = 0;
if (r == NULL){
// Nastal koniec načítania
}
while(STOP == 0){
r = fgets(line, SIZE, stdin);
if (r == NULL){
STOP = 1;
break;
}
value = strtol(line, &end, 10);
//printf("\n%d\n",value);
if(value == 0){
STOP = 1;
break;
}
char text[SIZE];
memset(text, 0, SIZE);
char* zaciatok_mena = end + 1;
int velkost_mena = strlen(zaciatok_mena) - 1;
if (velkost_mena > 0){
memcpy(text, zaciatok_mena, velkost_mena);
}
else {
STOP = 1;
break;
}
int id = find_student(databaza, size, text);
if (id < 0){
memcpy(databaza[size].name, text, velkost_mena);
databaza[size].votes = value;
size += 1;
}
else {
databaza[id].votes += value;
}
counter++;
}
qsort(databaza, size, sizeof(struct student), compare_students);
if(counter != 0){
printf("Vysledky:\n");
for (int i = 0; i < size; i++) {
printf("%d", databaza[i].votes);
printf(" %s\n", databaza[i].name);
}
}
else{
printf("Nepodarilo nacitat nic\n");
}
return 0;
}