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++){
|
|
|
|
if(students[i].votes<0){
|
|
|
|
break;
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:18:59 +00:00
|
|
|
return i;
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int search(struct student* students,const char* name){
|
|
|
|
int index=-1;
|
|
|
|
for(int i=0; i<MAXSTUDENTS; i++){
|
|
|
|
if(strcmp(students[i].name,name)==0){
|
|
|
|
index=i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int add_student(struct student* students, const char* name, int votes){
|
|
|
|
int index=-1, x=0;
|
|
|
|
for(int i=0; i<MAXSTUDENTS; i++){
|
|
|
|
if(strcmp(students[i].name,name)==0){
|
|
|
|
index=i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(students[i].name!=NULL){
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(index!=-1){
|
|
|
|
students[index].votes+=votes;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(x<(MAXSTUDENTS-1)){
|
|
|
|
strcpy(students[x].name,name);
|
|
|
|
students[x].votes=votes;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare(const void* s1,const void* s2){
|
|
|
|
struct student* stud1=(struct student*)s1;
|
|
|
|
struct student* stud2=(struct student*)s2;
|
|
|
|
return stud1->votes==stud2->votes?0:stud1->votes<stud2->votes?1:-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sort_students(struct student* students){
|
|
|
|
qsort(students, count_students(students), sizeof(struct student), compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
int body;
|
|
|
|
do{
|
|
|
|
fscanf(file,"%s",&meno[0]);
|
|
|
|
fscanf(file,"%d",&body);
|
|
|
|
if(add_student(students,meno,body)==-1){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}while(meno[0]=EOF);
|
|
|
|
|
2019-03-23 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 19:12:58 +00:00
|
|
|
|