pvjc20/du5/program.c

96 lines
2.8 KiB
C

#include <stdio.h>
#include <string.h>
int main(){
char name[50][50];
int score[50];
int pos = 0;
//Getting input
while(1){
scanf("%d %[^\n]", &score[pos], name[pos]);
scanf("%*c");
pos++;
int tmpCh = getc(stdin);
if(tmpCh == '\n') break;
ungetc(tmpCh, stdin);
}
char newName[50][50];
int newScore[50];
int newPos = 0;
//Combining names
for(int i = 0; i < pos; i++){
int check = 0;
for(int q = 0; q < newPos; q++){
if(q != i){
if(strcmp(name[i], newName[q]) == 0){
check = 1;
break;
}
}
}
if(check == 0){
strcpy(newName[newPos], name[i]);
newScore[newPos] = 0;
newPos++;
}
}
//Combining scores
for(int i = 0; i < newPos; i++){
for(int q = 0; q < pos; q++){
if(strcmp(newName[i], name[q]) == 0) newScore[i] += score[q];
}
}
// printf("Unsorted:\n");
// for(int i = 0; i < newPos; i++){
// printf("%d %s\n", newScore[i], newName[i]);
// }
//Sorting
for(int d = 0; d < newPos - 1; d++){
for(int i = 0; i < newPos - 1; i++){
if(newScore[i] < newScore[i + 1]){ //Score sorting
int tmpScore = newScore[i];
newScore[i] = newScore[i + 1];
newScore[i + 1] = tmpScore;
char tmpName[50];
strcpy(tmpName, newName[i]);
strcpy(newName[i], newName[i + 1]);
strcpy(newName[i + 1], tmpName);
}
else if(newScore[i] == newScore[i + 1]){ //Name sorting
int test = 0;
for(int q = 0; q < newPos; q++){
// printf("--Testing--\n");
if(newName[i][q] > newName[i + 1][q]){
// printf("Letters |%c %c|\n", newName[i][q], newName[i + 1][q]);
int tmpScore = newScore[i];
newScore[i] = newScore[i + 1];
newScore[i + 1] = tmpScore;
char tmpName[50];
strcpy(tmpName, newName[i]);
strcpy(newName[i], newName[i + 1]);
strcpy(newName[i + 1], tmpName);
// printf("|If|\n");
break;
}
else if(newName[i][q] < newName[i + 1][q]){
// printf("|Else if|\n");
break;
}
}
}
}
}
printf("Vysledky:\n");
for(int i = 0; i < newPos; i++){
printf("%d %s\n", newScore[i], newName[i]);
}
}