Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<string.h>
- #include <stdlib.h>
- #define SIZE 100
- //Struktura
- struct student{
- char name[SIZE];
- int votes;
- };
- int find_student(struct student* students,int size, const char* name){
- int i = size;
- while (i >= 0){
- if(strcmp(students[i].name, name)==0){
- return i;
- }
- i--;
- }
- return -1;
- }
- int compareV(const void* p1, const void* p2){
- struct student* s1 = (struct student*)p1;
- struct student* s2 = (struct student*)p2;
- if (s2->votes == s1->votes){
- return strcmp(s1->name,s2->name);
- }
- else{
- return (s2->votes - s1->votes);
- }
- }
- int main(){
- //Zadefinovanie struktury
- struct student databaza[SIZE];
- memset(databaza,0,SIZE*sizeof(struct student));
- int size = 0;
- int i=0;
- //nacitavanie
- while(1){
- //Deklaracia
- char line[SIZE];
- memset(line,0,SIZE);
- //Samotne nacitavanie
- char* r = fgets(line,SIZE,stdin);
- if (r == NULL){
- break;
- }
- //Nacitanie poctu hlasov
- char* end = NULL;
- int value = strtol(line,&end,10);
- if (value == 0){
- break;
- }
- //Zapisanie poctu hlasov
- databaza[size].votes = value;
- //Nacitanie mena
- char name[SIZE];
- memset(name,0,SIZE);
- char* zaciatok_mena = end + 1;
- int velkost_mena = strlen(zaciatok_mena) - 1;
- if (velkost_mena > 0){
- memcpy(name,zaciatok_mena,velkost_mena);
- }
- else {
- break;
- }
- //Zapisovanie mena do Struct
- int id = find_student(databaza,size,name);
- if (id< 0){
- // Skopirujte zaznam na posledne miesto v poli
- memcpy(databaza[size].name,name,velkost_mena);
- // Zvacsite pocet zaznamov
- size+=1;
- }
- else {
- databaza[id].votes = value + databaza[id].votes;
- }
- }
- int n = sizeof(databaza) / sizeof(struct student);
- qsort(databaza, n, sizeof(struct student), compareV);
- //Vypisovanie
- puts("Vysledky:");
- for (i=0; i<size;i++){
- printf("%d %s\n", databaza[i].votes, databaza[i].name);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement