1
This commit is contained in:
commit
3840741399
78
anketa.c
Normal file
78
anketa.c
Normal file
@ -0,0 +1,78 @@
|
||||
#include "anketa.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
int count_students(struct student* students){
|
||||
int i;
|
||||
for(i=0; i<MAXSTUDENTS; i++){
|
||||
if(students[i].votes<=0){
|
||||
break;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int search(struct student* students,const char* name){
|
||||
int a;
|
||||
for(int i=0; i<count_students(students); i++){
|
||||
a=strcmp(students[i].name,name);
|
||||
if(a==0){
|
||||
return i;
|
||||
}else{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int add_student(struct student* students, const char* name, int votes){
|
||||
int index=search(students,name),pocet=count_students(students);
|
||||
if(index==-1&&pocet<MAXSTUDENTS){
|
||||
strcpy(students[pocet].name,name);
|
||||
students[pocet].votes=votes;
|
||||
return pocet;
|
||||
}else if(pocet<MAXSTUDENTS){
|
||||
students[index].votes+=votes;
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int compare(const void* s1,const void* s2){
|
||||
struct student* stud1=(struct student*)s1;
|
||||
struct student* stud2=(struct student*)s2;
|
||||
if(stud1->votes==stud2->votes){
|
||||
return 0;
|
||||
}else if(stud1->votes<stud2->votes){
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void sort_students(struct student* students){
|
||||
qsort(students,count_students(students),sizeof(struct student),compare);
|
||||
}
|
||||
|
||||
void print_students(struct student* students){
|
||||
for(int i=0;i<count_students(students);i++){
|
||||
if(students[i].name!=NULL){
|
||||
printf("%s\n %d\n", students[i].name, students[i].votes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void read_students(FILE* file,struct student* students){
|
||||
char meno[100];
|
||||
int body,result;
|
||||
while(fscanf(file,"%s",&meno[0])!=EOF){
|
||||
fscanf(file,"%d",&body);
|
||||
result= add_student(students,meno,body);
|
||||
if(result==-1){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user