2019-03-23 18:14:52 +00:00
|
|
|
#include "anketa.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
int count_students(struct student* students){
|
2019-03-23 19:18:59 +00:00
|
|
|
int i;
|
|
|
|
for(i=0; i<MAXSTUDENTS; i++){
|
2019-03-23 19:22:22 +00:00
|
|
|
if(students[i].votes<=0){
|
2019-03-23 19:18:59 +00:00
|
|
|
break;
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:22:22 +00:00
|
|
|
return i;
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int search(struct student* students,const char* name){
|
2019-03-23 19:28:18 +00:00
|
|
|
int a;
|
2019-03-23 19:32:11 +00:00
|
|
|
for(int i=0; i<count_students(students); i++){
|
2019-03-23 19:28:18 +00:00
|
|
|
a=strcmp(students[i].name,name);
|
|
|
|
if(a==0){
|
2019-03-23 19:25:18 +00:00
|
|
|
return i;
|
2019-03-23 19:29:33 +00:00
|
|
|
}else{
|
|
|
|
continue;
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:25:18 +00:00
|
|
|
return -1;
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int add_student(struct student* students, const char* name, int votes){
|
2019-03-24 17:03:19 +00:00
|
|
|
int index=search(students,name),pocet=count_students(students);
|
|
|
|
if(index==-1&&pocet<MAXSTUDENTS){
|
|
|
|
strcpy(students[pocet].name,name);
|
|
|
|
students[pocet].votes=votes;
|
2019-03-25 14:17:05 +00:00
|
|
|
return pocet;
|
2019-03-24 17:03:19 +00:00
|
|
|
}else if(pocet<MAXSTUDENTS){
|
|
|
|
students[index].votes+=votes;
|
2019-03-25 14:17:05 +00:00
|
|
|
return index;
|
2019-03-24 17:03:19 +00:00
|
|
|
}
|
2019-03-23 18:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare(const void* s1,const void* s2){
|
|
|
|
struct student* stud1=(struct student*)s1;
|
|
|
|
struct student* stud2=(struct student*)s2;
|
2019-03-25 14:37:46 +00:00
|
|
|
if(stud1->votes==stud2->votes){
|
|
|
|
return 0;
|
|
|
|
}else if(stud1->votes<stud2->votes){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sort_students(struct student* students){
|
2019-03-25 14:37:46 +00:00
|
|
|
qsort(students,count_students(students),sizeof(struct student),compare);
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_students(struct student* students){
|
2019-03-23 19:12:58 +00:00
|
|
|
for(int i=0;i<count_students(students);i++){
|
2019-03-23 18:14:52 +00:00
|
|
|
if(students[i].name!=NULL){
|
|
|
|
printf("%s\n %d\n", students[i].name, students[i].votes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_students(FILE* file,struct student* students){
|
2019-03-23 19:12:58 +00:00
|
|
|
char meno[100];
|
2019-03-25 14:17:05 +00:00
|
|
|
int body,result;
|
|
|
|
while(fscanf(file,"%s",&meno[0])!=EOF){
|
|
|
|
fscanf(file,"%d",&body);
|
|
|
|
result= add_student(students,meno,body);
|
|
|
|
if(result==-1){
|
2019-03-23 19:12:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-03-25 14:17:05 +00:00
|
|
|
}
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|