Merge branch 'master' of git.kemt.fei.tuke.sk:ps625sf/pvjc23
This commit is contained in:
		
						commit
						c0abcbca6e
					
				
							
								
								
									
										88
									
								
								du5/program.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								du5/program.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,88 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
 | 
			
		||||
#define SIZE 100
 | 
			
		||||
 | 
			
		||||
struct student{
 | 
			
		||||
    int votes;
 | 
			
		||||
    char name[SIZE];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int search_index(int size, const struct student list[SIZE], const char student[SIZE]){
 | 
			
		||||
    for(int x = 0; x < size; x++){
 | 
			
		||||
        if(strcmp(list[x].name, student) == 0){
 | 
			
		||||
            return x;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int sort_by_votes(const void* p1, const void* p2){
 | 
			
		||||
    struct student* student1 = (struct student*) p1;
 | 
			
		||||
    struct student* student2 = (struct student*) p2;
 | 
			
		||||
 | 
			
		||||
    if(student1->votes != student2->votes){
 | 
			
		||||
        if(student1->votes < student2->votes){
 | 
			
		||||
            return 1;
 | 
			
		||||
        }else{
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
    }else{
 | 
			
		||||
        return strcmp(student1->name, student2->name);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(){
 | 
			
		||||
    int votes;
 | 
			
		||||
    char name[SIZE];
 | 
			
		||||
    char line[SIZE];
 | 
			
		||||
    char *end;
 | 
			
		||||
    int counter = 0;
 | 
			
		||||
 | 
			
		||||
    struct student list[SIZE];
 | 
			
		||||
 | 
			
		||||
    while(fgets(line, SIZE, stdin) != NULL){
 | 
			
		||||
        if(line[0] == '\n' || !isdigit(line[0])){
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        votes = strtol(line, &end, 10);
 | 
			
		||||
 | 
			
		||||
        if(end[0] != ' ' || !isalpha(end[1])){
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if(end[strlen(end) - 1] == '\n'){
 | 
			
		||||
            end[strlen(end) - 1] = '\0';
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        strcpy(name, end + 1);
 | 
			
		||||
 | 
			
		||||
        int stud_index = search_index(counter, list, name);
 | 
			
		||||
 | 
			
		||||
        if(stud_index == -1){
 | 
			
		||||
            counter++;
 | 
			
		||||
 | 
			
		||||
            list[counter - 1].votes = votes;
 | 
			
		||||
            strcpy(list[counter - 1].name, name);
 | 
			
		||||
        }else{
 | 
			
		||||
            list[stud_index].votes += votes;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(counter == 0){
 | 
			
		||||
        printf("Nepodarilo nacitat nic\n");
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qsort(list, counter, sizeof(list[0]), sort_by_votes);
 | 
			
		||||
 | 
			
		||||
    printf("Vysledky:\n");
 | 
			
		||||
 | 
			
		||||
    for(int x = 0; x < counter; x++){
 | 
			
		||||
        printf("%d %s\n", list[x].votes, list[x].name);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user