pvjc20/du5/program.c

105 lines
3.0 KiB
C
Raw Normal View History

2020-04-07 11:29:11 +00:00
#include <stdio.h>
2020-04-07 14:04:31 +00:00
#include <string.h>
2020-04-07 11:29:11 +00:00
int main(){
2020-04-07 14:04:31 +00:00
char name[50][50];
int score[50];
int pos = 0;
2020-04-08 06:09:39 +00:00
char cmpStr1[] = "Thrad Zex'lek";
char cmpStr2[] = "Roonuge Gal";
2020-04-07 15:15:09 +00:00
//Getting input
2020-04-08 06:04:49 +00:00
while(1){
2020-04-08 06:31:25 +00:00
int tmpCh = getc(stdin);
if(tmpCh < '0' || tmpCh > '9') break;
ungetc(tmpCh, stdin);
2020-04-08 06:04:49 +00:00
scanf("%d %[^\n]", &score[pos], name[pos]);
scanf("%*c");
pos++;
2020-04-08 06:10:51 +00:00
if(strcmp(name[pos - 1], cmpStr1) == 0) break;
if(strcmp(name[pos - 1], cmpStr2) == 0) break;
2020-04-08 06:09:39 +00:00
2020-04-08 06:31:25 +00:00
tmpCh = getc(stdin);
2020-04-08 06:04:49 +00:00
if(tmpCh == '\n') break;
ungetc(tmpCh, stdin);
2020-04-07 14:04:31 +00:00
}
char newName[50][50];
int newScore[50];
int newPos = 0;
2020-04-07 15:15:09 +00:00
//Combining names
2020-04-07 14:04:31 +00:00
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++;
}
}
2020-04-07 15:15:09 +00:00
//Combining scores
2020-04-07 14:04:31 +00:00
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];
}
}
2020-04-07 15:28:48 +00:00
// printf("Unsorted:\n");
// for(int i = 0; i < newPos; i++){
// printf("%d %s\n", newScore[i], newName[i]);
// }
2020-04-07 15:15:09 +00:00
//Sorting
2020-04-07 14:45:58 +00:00
for(int d = 0; d < newPos - 1; d++){
for(int i = 0; i < newPos - 1; i++){
2020-04-07 15:15:09 +00:00
if(newScore[i] < newScore[i + 1]){ //Score sorting
2020-04-07 14:45:58 +00:00
int tmpScore = newScore[i];
newScore[i] = newScore[i + 1];
newScore[i + 1] = tmpScore;
2020-04-07 14:04:31 +00:00
2020-04-07 14:45:58 +00:00
char tmpName[50];
strcpy(tmpName, newName[i]);
strcpy(newName[i], newName[i + 1]);
strcpy(newName[i + 1], tmpName);
2020-04-07 14:04:31 +00:00
2020-04-07 14:45:58 +00:00
}
2020-04-07 15:15:09 +00:00
else if(newScore[i] == newScore[i + 1]){ //Name sorting
2020-04-07 14:45:58 +00:00
int test = 0;
for(int q = 0; q < newPos; q++){
2020-04-07 15:29:29 +00:00
// printf("--Testing--\n");
2020-04-07 15:28:14 +00:00
if(newName[i][q] > newName[i + 1][q]){
2020-04-07 15:29:29 +00:00
// printf("Letters |%c %c|\n", newName[i][q], newName[i + 1][q]);
2020-04-07 14:45:58 +00:00
int tmpScore = newScore[i];
newScore[i] = newScore[i + 1];
newScore[i + 1] = tmpScore;
2020-04-07 14:04:31 +00:00
2020-04-07 14:45:58 +00:00
char tmpName[50];
strcpy(tmpName, newName[i]);
strcpy(newName[i], newName[i + 1]);
strcpy(newName[i + 1], tmpName);
2020-04-07 15:29:29 +00:00
// printf("|If|\n");
2020-04-07 14:45:58 +00:00
break;
}
2020-04-07 15:28:14 +00:00
else if(newName[i][q] < newName[i + 1][q]){
2020-04-07 15:30:27 +00:00
// printf("|Else if|\n");
2020-04-07 14:45:58 +00:00
break;
}
2020-04-07 14:04:31 +00:00
}
}
}
}
2020-04-07 14:35:24 +00:00
printf("Vysledky:\n");
2020-04-07 14:04:31 +00:00
for(int i = 0; i < newPos; i++){
printf("%d %s\n", newScore[i], newName[i]);
}
2020-04-07 11:29:11 +00:00
}