Update 'du5/program.c'
This commit is contained in:
parent
5f0ad4f343
commit
b89f6695bf
173
du5/program.c
173
du5/program.c
@ -1,87 +1,88 @@
|
|||||||
#include <stdlib.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
#define SIZE 100
|
|
||||||
|
#define SIZE 100
|
||||||
struct student{
|
|
||||||
int votes;
|
struct student{
|
||||||
char name[SIZE];
|
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++){
|
int search_index(int size, const struct student list[SIZE], const char student[SIZE]){
|
||||||
if(strcmp(list[x].name, student) == 0){
|
for(int x = 0; x < size; x++){
|
||||||
return x;
|
if(strcmp(list[x].name, student) == 0){
|
||||||
}
|
return x;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return -1;
|
|
||||||
}
|
return -1;
|
||||||
|
}
|
||||||
int sort_by_votes(const void* p1, const void* p2){
|
|
||||||
struct student* student1 = (struct student*) p1;
|
int sort_by_votes(const void* p1, const void* p2){
|
||||||
struct student* student2 = (struct student*) p2;
|
struct student* student1 = (struct student*) p1;
|
||||||
|
struct student* student2 = (struct student*) p2;
|
||||||
if(student1->votes != student2->votes){
|
|
||||||
if(student1->votes < student2->votes){
|
if(student1->votes != student2->votes){
|
||||||
return 1;
|
if(student1->votes < student2->votes){
|
||||||
}else{
|
return 1;
|
||||||
return -1;
|
}else{
|
||||||
}
|
return -1;
|
||||||
}else{
|
}
|
||||||
return strcmp(student1->name, student2->name);
|
}else{
|
||||||
}
|
return strcmp(student1->name, student2->name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
int main(){
|
|
||||||
int votes;
|
int main(){
|
||||||
char name[SIZE];
|
int votes;
|
||||||
char line[SIZE];
|
char name[SIZE];
|
||||||
char *end;
|
char line[SIZE];
|
||||||
int counter = 0;
|
char *end;
|
||||||
|
int counter = 0;
|
||||||
struct student list[SIZE];
|
|
||||||
|
struct student list[SIZE];
|
||||||
while(fgets(line, SIZE, stdin) != NULL){
|
|
||||||
if(line[0] == '\n' || !isdigit(line[0])){
|
while(fgets(line, SIZE, stdin) != NULL){
|
||||||
break;
|
if(line[0] == '\n' || !isdigit(line[0])){
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
votes = strtol(line, &end, 10);
|
|
||||||
|
votes = strtol(line, &end, 10);
|
||||||
if(end[0] != ' ' || !isalpha(end[1])){
|
|
||||||
break;
|
if(end[0] != ' ' || !isalpha(end[1])){
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
if(end[strlen(end) - 1] == '\n'){
|
|
||||||
end[strlen(end) - 1] = '\0';
|
if(end[strlen(end) - 1] == '\n'){
|
||||||
}
|
end[strlen(end) - 1] = '\0';
|
||||||
|
}
|
||||||
strcpy(name, end + 1);
|
|
||||||
|
strcpy(name, end + 1);
|
||||||
int stud_index = search_index(counter, list, name);
|
|
||||||
|
int stud_index = search_index(counter, list, name);
|
||||||
if(stud_index == -1){
|
|
||||||
counter++;
|
if(stud_index == -1){
|
||||||
|
counter++;
|
||||||
list[counter - 1].votes = votes;
|
|
||||||
strcpy(list[counter - 1].name, name);
|
list[counter - 1].votes = votes;
|
||||||
}else{
|
strcpy(list[counter - 1].name, name);
|
||||||
list[stud_index].votes += votes;
|
}else{
|
||||||
}
|
list[stud_index].votes += votes;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if(counter == 0){
|
|
||||||
printf("Nepodarilo sa nacitat ziaden vstup\n");
|
if(counter == 0){
|
||||||
return 0;
|
printf("Nepodarilo sa nacitat ziaden vstup\n");
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
qsort(list, counter, sizeof(list[0]), sort_by_votes);
|
|
||||||
|
qsort(list, counter, sizeof(list[0]), sort_by_votes);
|
||||||
printf("Vysledky:\n");
|
|
||||||
|
printf("Vysledky:\n");
|
||||||
for(int x = 0; x < counter; x++){
|
|
||||||
printf("%d %s\n", list[x].votes, list[x].name);
|
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