Thank you. 2) (10 points) Assuming there is an offline game, once players finish
ID: 3936091 • Letter: T
Question
Thank you. 2) (10 points) Assuming there is an offline game, once players finish the game, the final score would be stored in a score file. Now we have multiple players playing that game. Each player needs to play that game in two computers "Alpha" and "Beta". So there are two score files generated separately in two computers. Our goal is to check those two files and join the score for each player together to create a final file "score final.txt" for future statics Suppose the score file in computer "Alpha" has been renamed as "score _alpha.txt"; in computer "Beta" the score file has been renamed as "score_beta.txt" In score file, a colon separates the player's name and score. One example of "score_alpha.txt", "score_beta.txt" and final score file "score_final.txt'are like below: score, alpha.txt Allen:12 Bob:25 Carl:36 Kevin:121 Tomy:99 Jack:108Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int parse_input( char inp[200], char name[10] ){
int i =0;
for(; i < strlen(inp); i++ ){
if( inp[i] == ':' ){
break;
}
name[i] = inp[i];
}
name[i] = '';
i++;
int score = 0;
for(; i < strlen(inp); i++ ){
score*= 10;
score = score + (inp[i]-'0');
};
return score;
}
int isMatch( char a[], char b[] ){
if( strlen(a) == strlen(b) ){
int i =0 ;
for(; i < strlen(a); i++){
if( a[i] != b[i] ){ return 0;}
}
return 1;
}
return 0;
}
void cpyCharArray( char copyInto[], char copyWhat[] ){
int i = 0;
for(; i < strlen(copyWhat); i++){
copyInto[i] = copyWhat[i];
}
copyInto[i] = '';
return;
}
int cmpFxn( const void* aa, const void* bb ){
char* a = (char*)(aa);
char* b = (char*)(bb);
int l2 = strlen(b);
int min = strlen(a);
if( l2 < min ){ min = l2; }
int i = 0;
for(; i < min; i++){
if( a[i] < b[i] ){ return 0; }
if( a[i] > b[i] ){ return 1; }
}
if( l2 > min ){ return 0; }
return 1;
}
int intToStr( int input, char out[] ){
//end with
char temp[100];
int i = 0;
while(input != 0 ){
temp[i++] = (input%10 + '0') ;
input = input/10;
}
i--;
int j = 0;
for(; i>= 0; i-- ){
out[j++] = temp[i];
}
out[j] = '';
}
int main(){
char Orignames[100][50];
char names[100][50];
int totalPlayers = 0;
char inp[200];
while( scanf("%s", inp ) != EOF ){
char name[10];
int score = parse_input( inp , name );
int i = 0;
char tempo[100];
intToStr( score, tempo );
for(; i < totalPlayers; i++){
if( isMatch(name, Orignames[i] ) ){
int len = strlen(names[i]);
names[i][ len ] = ':';
names[i][ len+1 ] = '';
cpyCharArray( names[i] + len+1 , tempo);
break;
}
}
if( i == totalPlayers ){
cpyCharArray( names[ totalPlayers ] , name );
cpyCharArray( Orignames[ totalPlayers ] , name );
int len = strlen(names[i]);
names[i][ len ] = ':';
names[i][ len+1 ] = '';
cpyCharArray( names[i] + len+1 , tempo);
totalPlayers++;
}
}
qsort( names, totalPlayers, 50 , cmpFxn );
int i = 0;
for(; i < totalPlayers; i++){
printf("%s ", names[i] );
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.