c1
This commit is contained in:
		
							parent
							
								
									df0e5738df
								
							
						
					
					
						commit
						957fbf894d
					
				
							
								
								
									
										71
									
								
								du5/program.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								du5/program.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,71 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#define SIZE 100
 | 
			
		||||
 | 
			
		||||
struct student {
 | 
			
		||||
    char name[SIZE];
 | 
			
		||||
    int votes;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int find_student(struct student* students,int size, const char* name){
 | 
			
		||||
	for(int i=0; i<size; i++){
 | 
			
		||||
		if(!strcmp(students[i].name, name)){
 | 
			
		||||
			return i;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int compare_votes(const void * a, const void * b){
 | 
			
		||||
	if(((struct student*)a)->votes < ((struct student*)b)->votes)
 | 
			
		||||
		return 1;
 | 
			
		||||
	if(((struct student*)a)->votes == ((struct student*)b)->votes)
 | 
			
		||||
		return strcmp(((struct student*)a)->name, ((struct student*)b)->name);
 | 
			
		||||
	if(((struct student*)a)->votes > ((struct student*)b)->votes)
 | 
			
		||||
		return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char const *argv[]){
 | 
			
		||||
	struct student databaza[SIZE];
 | 
			
		||||
	memset(databaza,0,SIZE*sizeof(struct student));
 | 
			
		||||
	int size = 0;
 | 
			
		||||
 | 
			
		||||
	while(size!=SIZE){
 | 
			
		||||
		char line[SIZE];
 | 
			
		||||
		memset(line,0,SIZE);
 | 
			
		||||
		char* r = fgets(line,SIZE,stdin);
 | 
			
		||||
		if (r == NULL){
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		char* end = NULL;
 | 
			
		||||
		int value = strtol(line,&end,10);
 | 
			
		||||
		if (value == 0){
 | 
			
		||||
		    break;
 | 
			
		||||
		}
 | 
			
		||||
		char tmp[SIZE];
 | 
			
		||||
		memset(tmp, 0, SIZE);
 | 
			
		||||
		memcpy(tmp, end+1, strlen(end+1)-1);
 | 
			
		||||
		int idx = find_student(databaza, size, tmp);
 | 
			
		||||
		if(idx!=-1){
 | 
			
		||||
			memcpy(databaza[idx].name, tmp, strlen(tmp));
 | 
			
		||||
			databaza[idx].votes += value;
 | 
			
		||||
		}
 | 
			
		||||
		else{
 | 
			
		||||
		memcpy(databaza[size].name, tmp, strlen(tmp));
 | 
			
		||||
		databaza[size].votes = value;
 | 
			
		||||
		size++;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	qsort(databaza, size, sizeof(struct student), compare_votes);
 | 
			
		||||
 | 
			
		||||
	printf("Vysledky:\n");
 | 
			
		||||
	for(int i=0; i<size; i++){
 | 
			
		||||
		printf("%d %s\n", databaza[i].votes, databaza[i].name);
 | 
			
		||||
	}
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user