Advertisement
Phr0zen_Penguin

PlayerData.cpp - Data Entry/Sort Algorithm

Sep 1st, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.40 KB | None | 0 0
  1. /**
  2.  * [PlayerData.cpp]
  3.  * A program that accepts the names and scores from six different players, then outputs the data,
  4.  * sorted in descending order, by score.  It also outputs the same data in ascending alphabetical
  5.  * by name.
  6.  */
  7. #include <iostream>
  8.  
  9.  
  10. /**
  11.  *
  12.  */
  13. using std::cout;
  14. using std::cin;
  15. using std::endl;
  16.  
  17.  
  18. const   int MAX_NUM_PLAYERS = 6;                                                            // Maximum number of players to store data on
  19.  
  20.  
  21. /**
  22.  * Class Definition:
  23.  */
  24. class PlayerData
  25. {
  26.     public:
  27.         /**
  28.          * Constructor(s):
  29.          */
  30.         PlayerData(int _ci = 0)
  31.             { (_ci >= MAX_NUM_PLAYERS || _ci < 0) ? CurrIndex = 0 : CurrIndex = _ci; }  // Overloaded constructor with default argument
  32.         PlayerData(std::string _ln[], std::string _fn[], int _sc[])
  33.         : CurrIndex(0)
  34.             { SetPlayerData(_ln, _fn, _sc); }
  35.         PlayerData(PlayerData *_ppd)
  36.         : CurrIndex(0)
  37.             { SetPlayerData(_ppd); }
  38.         PlayerData(PlayerData &_pd)
  39.         : CurrIndex(0)
  40.             { SetPlayerData(_pd); }
  41.  
  42.         /**
  43.          * Destructor
  44.          */
  45.         ~PlayerData(){}
  46.  
  47.         /**
  48.          * Accessors/Getters:
  49.          */
  50.         int GetCurrIndex(void) { return CurrIndex; }
  51.         std::string GetCurrLastName(void) { return LastNames[CurrIndex]; }
  52.         std::string GetCurrFirstName(void) { return FirstNames[CurrIndex]; }                // Inline methods
  53.         int GetCurrScore(void) { return Scores[CurrIndex]; }
  54.         void GetCurrPlayerData(std::string &_ln, std::string &_fn, int &_sc)
  55.             { _ln = LastNames[CurrIndex]; _fn = FirstNames[CurrIndex]; _sc = Scores[CurrIndex]; }
  56.         std::string *GetLastNames(void) { return LastNames; }
  57.         std::string *GetFirstNames(void) { return FirstNames; }
  58.         int *GetScores(void) { return Scores; }
  59.         void GetPlayerData(std::string *&_ln, std::string *&_fn, int *&_sc)
  60.             { _ln = LastNames; _fn = FirstNames; _sc = Scores; }
  61.         void GetPlayerData(PlayerData &_pd)
  62.             { _pd.SetPlayerData(LastNames, FirstNames, Scores); }
  63.  
  64.         /**
  65.          * Mutators/Setters:
  66.          */
  67.         void SetCurrIndex(int _ci) { CurrIndex = _ci; }
  68.         void SetCurrLastName(std::string _ln) { LastNames[CurrIndex] = _ln; }
  69.         void SetCurrFirstName(std::string _fn) { FirstNames[CurrIndex] = _fn; }
  70.         void SetCurrScore(int _sc) { Scores[CurrIndex] = _sc; }
  71.         void SetCurrPlayerData(std::string _ln, std::string _fn, int _sc)
  72.             { LastNames[CurrIndex] = _ln; FirstNames[CurrIndex] = _fn; Scores[CurrIndex] = _sc; }
  73.         /**
  74.          * Note:
  75.          * Set arrays element-by-element, through iteration, to circumvent assignment issues.
  76.          */
  77.         void SetLastNames(std::string _ln[])
  78.             { for(int i = 0; i < MAX_NUM_PLAYERS; i++) LastNames[i] = _ln[i]; }
  79.         void SetFirstNames(std::string _fn[])
  80.             { for(int i = 0; i < MAX_NUM_PLAYERS; i++) FirstNames[i] = _fn[i]; }
  81.         void SetScores(int _sc[])
  82.             { for(int i = 0; i < MAX_NUM_PLAYERS; i++) Scores[i] = _sc[i]; }
  83.         void SetPlayerData(std::string _ln[], std::string _fn[], int _sc[])
  84.         {
  85.             for(int i = 0; i < MAX_NUM_PLAYERS; i++)
  86.             {
  87.                 LastNames[i] = _ln[i];
  88.                 FirstNames[i] = _fn[i];
  89.                 Scores[i] = _sc[i];
  90.             }
  91.         }
  92.         void SetPlayerData(PlayerData &_pd)
  93.         {
  94.             for(int i = 0; i < MAX_NUM_PLAYERS; i++)
  95.             {
  96.                 LastNames[i] = _pd.GetLastNames()[i];
  97.                 FirstNames[i] = _pd.GetFirstNames()[i];
  98.                 Scores[i] = _pd.GetScores()[i];
  99.             }
  100.         }
  101.         void SetPlayerData(PlayerData *_ppd)
  102.         {
  103.             for(int i = 0; i < MAX_NUM_PLAYERS; i++)
  104.             {
  105.                 LastNames[i] = _ppd->GetLastNames()[i];
  106.                 FirstNames[i] = _ppd->GetFirstNames()[i];
  107.                 Scores[i] = _ppd->GetScores()[i];
  108.             }
  109.         }
  110.  
  111.  
  112.     private:
  113.         int         CurrIndex;
  114.         std::string LastNames[MAX_NUM_PLAYERS];
  115.         std::string FirstNames[MAX_NUM_PLAYERS];
  116.         int         Scores[MAX_NUM_PLAYERS];
  117. };
  118.  
  119.  
  120. /**
  121.  * Function Prototypes:
  122.  */
  123. void PlayerDataEntry(PlayerData *, int NumOfPlayers = MAX_NUM_PLAYERS);
  124. void PlayerDataOutput(PlayerData *);
  125. void SortScoresDesc(PlayerData *, PlayerData *);
  126. void SortNamesAsc(PlayerData *, PlayerData *);
  127.  
  128.  
  129. /**
  130.  * The main() function:
  131.  *
  132.  * Program entry point.
  133.  */
  134. int main(void)
  135. {
  136.     PlayerData  *PData = new PlayerData();
  137.  
  138. /*
  139.     std::string LastNames[] = { "Doe", "Dough", "Zimmerman", "Cooper", "Tapper", "Fox" };
  140.     std::string FirstNames[] = { "John", "Jane", "Bob", "Alice", "Damion", "Megan" };
  141.     int         Scores[] = { 12, 14, 11, 10, 21, 13 };
  142.  
  143.     PData = new PlayerData(LastNames, FirstNames, Scores);
  144. */
  145.  
  146.  
  147.     /**
  148.      * Player Data Entry:
  149.      */
  150.     PlayerDataEntry(PData);
  151.  
  152.  
  153.     /**
  154.      * Player Data Output:
  155.      */
  156.     PlayerDataOutput(PData);
  157.  
  158.  
  159.     /**
  160.      * Clean up:
  161.      */
  162.     delete PData;                                                                   // House cleaning
  163.  
  164.     return 0;
  165. }
  166.  
  167.  
  168. /**
  169.  * The PlayerDataEntry() function:
  170.  *
  171.  * A function that accepts the required player data and stores it in an appropriately declared
  172.  * variable.
  173.  */
  174. void PlayerDataEntry(PlayerData *PData, int NumOfPlayers)
  175. {
  176.     std::string NameBuff;
  177.     int         ScoreBuff;
  178.  
  179.         for(int i = 0 ; i < NumOfPlayers; i++)
  180.         {
  181.             /**
  182.              * Data Entry:
  183.              */
  184.             cout << "[ PLAYER " << (i + 1) << " ]" << endl;
  185.  
  186.             PData->SetCurrIndex(i);
  187.  
  188.  
  189.             /**
  190.              * Last Name:
  191.              */
  192.             cout << "  Last Name: ";
  193.             std::getline(cin, NameBuff, '\n');
  194.  
  195.             PData->SetCurrLastName(NameBuff);
  196.  
  197.             NameBuff = "";
  198.  
  199.             //cin.ignore();
  200.  
  201.  
  202.             /**
  203.              * First Name:
  204.              */
  205.             cout << "  Frist Name: ";
  206.             std::getline(cin, NameBuff, '\n');
  207.  
  208.  
  209.             PData->SetCurrFirstName(NameBuff);
  210.  
  211.             NameBuff = "";
  212.  
  213.             //cin.ignore();
  214.  
  215.  
  216.             /**
  217.              * Score:
  218.              */
  219.             cout << "  Score: ";
  220.             cin >> ScoreBuff;
  221.  
  222.             PData->SetCurrScore(ScoreBuff);
  223.  
  224.             ScoreBuff = 0;
  225.  
  226.             cin.ignore();
  227.  
  228.             cout << endl << endl;
  229.         }
  230. }
  231.  
  232.  
  233. /**
  234.  * The PlayerDataOutput() function:
  235.  *
  236.  * A function that prints out all player data, first sorted in descending order, by score, and then
  237.  * sorted in ascending order, by name, using a PlayerData class, accepted as argument.
  238.  */
  239. void PlayerDataOutput(PlayerData *PData)
  240. {
  241.     PlayerData  *PDataS = new PlayerData();
  242.  
  243.  
  244.     /**
  245.      * Sort the data by score (descending).
  246.      */
  247.     SortScoresDesc(PData, PDataS);
  248.  
  249.     cout << "+-------------------------------+" << endl;
  250.     cout << "| PLAYER DATA :: SCORE :: DESC. |" << endl;
  251.     cout << "+-------------------------------+" << endl;
  252.  
  253.         /**
  254.          * Print out all player data:
  255.          */
  256.         for(int i = 0; i < MAX_NUM_PLAYERS; i ++)
  257.         {
  258.             PDataS->SetCurrIndex(i);
  259.  
  260.             cout << " " << PDataS->GetCurrLastName() << ", " << PDataS->GetCurrFirstName() << ": " << PDataS->GetCurrScore() << endl;
  261.         }
  262.  
  263.     cout << endl << endl;
  264.  
  265.  
  266.     PDataS = new PlayerData();
  267.  
  268.     /**
  269.      * Sort the data by name (ascending).
  270.      */
  271.     SortNamesAsc(PData, PDataS);
  272.  
  273.     cout << "+-------------------------------+" << endl;
  274.     cout << "|  PLAYER DATA :: NAME :: ASC.  |" << endl;
  275.     cout << "+-------------------------------+" << endl;
  276.  
  277.         /**
  278.          * Print out all player data:
  279.          */
  280.         for(int i = 0; i < MAX_NUM_PLAYERS; i ++)
  281.         {
  282.             PDataS->SetCurrIndex(i);
  283.  
  284.             cout << " " << PDataS->GetCurrLastName() << ", " << PDataS->GetCurrFirstName() << ": " << PDataS->GetCurrScore() << endl;
  285.         }
  286.  
  287.  
  288.     /**
  289.      * Clean up:
  290.      */
  291.     delete PDataS;
  292. }
  293.  
  294.  
  295. /**
  296.  * The SortScoresDesc() function:
  297.  *
  298.  * A function that sorts the player data, received as a source argument, descending, by score, and
  299.  * stores it in a location, received as a destination argument.
  300.  */
  301. void SortScoresDesc(PlayerData *PDataSrc, PlayerData *PDataDest)
  302. {
  303.     std::string *LastNames;
  304.     std::string *FirstNames;
  305.     int         *Scores;
  306.  
  307.     /**
  308.      * Get the player data:
  309.      */
  310.     PDataSrc->GetPlayerData(LastNames, FirstNames, Scores);
  311.  
  312.     /**
  313.      * Note:
  314.      * Using the selection sort algorithm.
  315.      */
  316.     int         i;
  317.     int         j;
  318.     int         first;
  319.     int         TempScore;
  320.     std::string TempLN;
  321.     std::string TempFN;
  322.  
  323.         for(i = MAX_NUM_PLAYERS - 1 ; i > 0; i--)
  324.         {
  325.             first = 0;                                                      // Initialize to subscript of first element.
  326.  
  327.                 for(j = 1; j <= i; j++)                                     // Locate smallest between positions 1 and i.
  328.                 {
  329.                     if(Scores[j] < Scores[first])
  330.                         first = j;
  331.                 }
  332.  
  333.             TempScore = Scores[first];                                      // Swap smallest found with element in position i.
  334.             TempLN = LastNames[first];
  335.             TempFN = FirstNames[first];
  336.  
  337.             Scores[first] = Scores[i];
  338.             LastNames[first] = LastNames[i];
  339.             FirstNames[first] = FirstNames[i];
  340.  
  341.             Scores[i] = TempScore;
  342.             LastNames[i] = TempLN;
  343.             FirstNames[i] = TempFN;
  344.         }
  345.  
  346.     /**
  347.      * Store the sorted player data:
  348.      */
  349.     PDataDest->SetPlayerData(LastNames, FirstNames, Scores);
  350.  
  351.     return;
  352. }
  353.  
  354.  
  355. /**
  356.  * The SortNamesAsc() function:
  357.  *
  358.  * A function that sorts the player data, received as a source argument, ascending, by name, and
  359.  * stores it in a location, received as a destination argument.
  360.  */
  361. void SortNamesAsc(PlayerData *PDataSrc, PlayerData *PDataDest)
  362. {
  363.     std::string *LastNames;
  364.     std::string *FirstNames;
  365.     int         *Scores;
  366.  
  367.     /**
  368.      * Get the player data:
  369.      */
  370.     PDataSrc->GetPlayerData(LastNames, FirstNames, Scores);
  371.  
  372.     /**
  373.      * Note:
  374.      * Using the insertion sort algorithm.
  375.      */
  376.     int         i;
  377.     int         j;
  378.     std::string LNKey;
  379.     std::string FNKey;
  380.     int         ScKey;
  381.  
  382.         for(j = 1; j < MAX_NUM_PLAYERS; j++)                                // Start with 1 (not 0).
  383.         {
  384.             LNKey = LastNames[j];
  385.             FNKey = FirstNames[j];
  386.             ScKey = Scores[j];
  387.  
  388.                 for(i = j - 1; (i >= 0) && (LastNames[i] > LNKey); i--)     // Smaller values move up.
  389.                 {
  390.                     LastNames[i + 1] = LastNames[i];
  391.                     FirstNames[i + 1] = FirstNames[i];
  392.                     Scores[i + 1] = Scores[i];
  393.                 }
  394.  
  395.             LastNames[i + 1] = LNKey;                                       // Put LNKey into its proper location.
  396.             FirstNames[i + 1] = FNKey;
  397.             Scores[i + 1] = ScKey;
  398.         }
  399.  
  400.     /**
  401.      * Store the sorted player data:
  402.      */
  403.     PDataDest->SetPlayerData(LastNames, FirstNames, Scores);
  404.  
  405.     return;
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement