Question
Need help, a bit confused.
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 players name and score. One example of score_alpha.txt, score_beta.txt and final score file score_final.txtare like below: ● score, alpha.txt Allen:12 Bob:25 Carl:36 Kevin:121 Tomy:99 Jack:108
Need help, a bit confused.Thank you.
Thank you.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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] = '\0';
   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] = '\0';
   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 \0
   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] = '\0';
}

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 ] = '\0';
               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 ] = '\0';
           cpyCharArray( names[i] + len+1 , tempo);
           totalPlayers++;
       }
   }

   qsort( names, totalPlayers, 50 , cmpFxn );
   int i = 0;
   for(; i < totalPlayers; i++){
       printf("%s\n", names[i] );
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Need help, a bit confused. Thank you. 2) (10 points) Assuming there is an offline game,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help, dont understand how to set up. 1 You are a game developer at Microsoft...

    need help, dont understand how to set up. 1 You are a game developer at Microsoft Corporation and are charged with the task of developing games in C++. 2 Please develop a Rock Paper Scissor game for two players. Each player should have one of the following options: • 1. Rock • 2. Paper • 3. Scissor A. Player One: person You need to ask the person to enter one option from the above three. 10 B. Player Two: computer...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • This needs to be done in c++11 and be compatible with g++ compiling Project description: Write...

    This needs to be done in c++11 and be compatible with g++ compiling Project description: Write a C++ program to simulate a simple card game between two players. The game proceeds as follows: The 52 cards in a deck of cards are shuffled and each player draws three cards from the top of the deck. Remaining cards are placed in a pile face-down between the two players. Players then select a card from the three in their hand. The player...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT