Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [PlayerData.cpp]
- * A program that accepts the names and scores from six different players, then outputs the data,
- * sorted in descending order, by score. It also outputs the same data in ascending alphabetical
- * by name.
- */
- #include <iostream>
- /**
- *
- */
- using std::cout;
- using std::cin;
- using std::endl;
- const int MAX_NUM_PLAYERS = 6; // Maximum number of players to store data on
- /**
- * Class Definition:
- */
- class PlayerData
- {
- public:
- /**
- * Constructor(s):
- */
- PlayerData(int _ci = 0)
- { (_ci >= MAX_NUM_PLAYERS || _ci < 0) ? CurrIndex = 0 : CurrIndex = _ci; } // Overloaded constructor with default argument
- PlayerData(std::string _ln[], std::string _fn[], int _sc[])
- : CurrIndex(0)
- { SetPlayerData(_ln, _fn, _sc); }
- PlayerData(PlayerData *_ppd)
- : CurrIndex(0)
- { SetPlayerData(_ppd); }
- PlayerData(PlayerData &_pd)
- : CurrIndex(0)
- { SetPlayerData(_pd); }
- /**
- * Destructor
- */
- ~PlayerData(){}
- /**
- * Accessors/Getters:
- */
- int GetCurrIndex(void) { return CurrIndex; }
- std::string GetCurrLastName(void) { return LastNames[CurrIndex]; }
- std::string GetCurrFirstName(void) { return FirstNames[CurrIndex]; } // Inline methods
- int GetCurrScore(void) { return Scores[CurrIndex]; }
- void GetCurrPlayerData(std::string &_ln, std::string &_fn, int &_sc)
- { _ln = LastNames[CurrIndex]; _fn = FirstNames[CurrIndex]; _sc = Scores[CurrIndex]; }
- std::string *GetLastNames(void) { return LastNames; }
- std::string *GetFirstNames(void) { return FirstNames; }
- int *GetScores(void) { return Scores; }
- void GetPlayerData(std::string *&_ln, std::string *&_fn, int *&_sc)
- { _ln = LastNames; _fn = FirstNames; _sc = Scores; }
- void GetPlayerData(PlayerData &_pd)
- { _pd.SetPlayerData(LastNames, FirstNames, Scores); }
- /**
- * Mutators/Setters:
- */
- void SetCurrIndex(int _ci) { CurrIndex = _ci; }
- void SetCurrLastName(std::string _ln) { LastNames[CurrIndex] = _ln; }
- void SetCurrFirstName(std::string _fn) { FirstNames[CurrIndex] = _fn; }
- void SetCurrScore(int _sc) { Scores[CurrIndex] = _sc; }
- void SetCurrPlayerData(std::string _ln, std::string _fn, int _sc)
- { LastNames[CurrIndex] = _ln; FirstNames[CurrIndex] = _fn; Scores[CurrIndex] = _sc; }
- /**
- * Note:
- * Set arrays element-by-element, through iteration, to circumvent assignment issues.
- */
- void SetLastNames(std::string _ln[])
- { for(int i = 0; i < MAX_NUM_PLAYERS; i++) LastNames[i] = _ln[i]; }
- void SetFirstNames(std::string _fn[])
- { for(int i = 0; i < MAX_NUM_PLAYERS; i++) FirstNames[i] = _fn[i]; }
- void SetScores(int _sc[])
- { for(int i = 0; i < MAX_NUM_PLAYERS; i++) Scores[i] = _sc[i]; }
- void SetPlayerData(std::string _ln[], std::string _fn[], int _sc[])
- {
- for(int i = 0; i < MAX_NUM_PLAYERS; i++)
- {
- LastNames[i] = _ln[i];
- FirstNames[i] = _fn[i];
- Scores[i] = _sc[i];
- }
- }
- void SetPlayerData(PlayerData &_pd)
- {
- for(int i = 0; i < MAX_NUM_PLAYERS; i++)
- {
- LastNames[i] = _pd.GetLastNames()[i];
- FirstNames[i] = _pd.GetFirstNames()[i];
- Scores[i] = _pd.GetScores()[i];
- }
- }
- void SetPlayerData(PlayerData *_ppd)
- {
- for(int i = 0; i < MAX_NUM_PLAYERS; i++)
- {
- LastNames[i] = _ppd->GetLastNames()[i];
- FirstNames[i] = _ppd->GetFirstNames()[i];
- Scores[i] = _ppd->GetScores()[i];
- }
- }
- private:
- int CurrIndex;
- std::string LastNames[MAX_NUM_PLAYERS];
- std::string FirstNames[MAX_NUM_PLAYERS];
- int Scores[MAX_NUM_PLAYERS];
- };
- /**
- * Function Prototypes:
- */
- void PlayerDataEntry(PlayerData *, int NumOfPlayers = MAX_NUM_PLAYERS);
- void PlayerDataOutput(PlayerData *);
- void SortScoresDesc(PlayerData *, PlayerData *);
- void SortNamesAsc(PlayerData *, PlayerData *);
- /**
- * The main() function:
- *
- * Program entry point.
- */
- int main(void)
- {
- PlayerData *PData = new PlayerData();
- /*
- std::string LastNames[] = { "Doe", "Dough", "Zimmerman", "Cooper", "Tapper", "Fox" };
- std::string FirstNames[] = { "John", "Jane", "Bob", "Alice", "Damion", "Megan" };
- int Scores[] = { 12, 14, 11, 10, 21, 13 };
- PData = new PlayerData(LastNames, FirstNames, Scores);
- */
- /**
- * Player Data Entry:
- */
- PlayerDataEntry(PData);
- /**
- * Player Data Output:
- */
- PlayerDataOutput(PData);
- /**
- * Clean up:
- */
- delete PData; // House cleaning
- return 0;
- }
- /**
- * The PlayerDataEntry() function:
- *
- * A function that accepts the required player data and stores it in an appropriately declared
- * variable.
- */
- void PlayerDataEntry(PlayerData *PData, int NumOfPlayers)
- {
- std::string NameBuff;
- int ScoreBuff;
- for(int i = 0 ; i < NumOfPlayers; i++)
- {
- /**
- * Data Entry:
- */
- cout << "[ PLAYER " << (i + 1) << " ]" << endl;
- PData->SetCurrIndex(i);
- /**
- * Last Name:
- */
- cout << " Last Name: ";
- std::getline(cin, NameBuff, '\n');
- PData->SetCurrLastName(NameBuff);
- NameBuff = "";
- //cin.ignore();
- /**
- * First Name:
- */
- cout << " Frist Name: ";
- std::getline(cin, NameBuff, '\n');
- PData->SetCurrFirstName(NameBuff);
- NameBuff = "";
- //cin.ignore();
- /**
- * Score:
- */
- cout << " Score: ";
- cin >> ScoreBuff;
- PData->SetCurrScore(ScoreBuff);
- ScoreBuff = 0;
- cin.ignore();
- cout << endl << endl;
- }
- }
- /**
- * The PlayerDataOutput() function:
- *
- * A function that prints out all player data, first sorted in descending order, by score, and then
- * sorted in ascending order, by name, using a PlayerData class, accepted as argument.
- */
- void PlayerDataOutput(PlayerData *PData)
- {
- PlayerData *PDataS = new PlayerData();
- /**
- * Sort the data by score (descending).
- */
- SortScoresDesc(PData, PDataS);
- cout << "+-------------------------------+" << endl;
- cout << "| PLAYER DATA :: SCORE :: DESC. |" << endl;
- cout << "+-------------------------------+" << endl;
- /**
- * Print out all player data:
- */
- for(int i = 0; i < MAX_NUM_PLAYERS; i ++)
- {
- PDataS->SetCurrIndex(i);
- cout << " " << PDataS->GetCurrLastName() << ", " << PDataS->GetCurrFirstName() << ": " << PDataS->GetCurrScore() << endl;
- }
- cout << endl << endl;
- PDataS = new PlayerData();
- /**
- * Sort the data by name (ascending).
- */
- SortNamesAsc(PData, PDataS);
- cout << "+-------------------------------+" << endl;
- cout << "| PLAYER DATA :: NAME :: ASC. |" << endl;
- cout << "+-------------------------------+" << endl;
- /**
- * Print out all player data:
- */
- for(int i = 0; i < MAX_NUM_PLAYERS; i ++)
- {
- PDataS->SetCurrIndex(i);
- cout << " " << PDataS->GetCurrLastName() << ", " << PDataS->GetCurrFirstName() << ": " << PDataS->GetCurrScore() << endl;
- }
- /**
- * Clean up:
- */
- delete PDataS;
- }
- /**
- * The SortScoresDesc() function:
- *
- * A function that sorts the player data, received as a source argument, descending, by score, and
- * stores it in a location, received as a destination argument.
- */
- void SortScoresDesc(PlayerData *PDataSrc, PlayerData *PDataDest)
- {
- std::string *LastNames;
- std::string *FirstNames;
- int *Scores;
- /**
- * Get the player data:
- */
- PDataSrc->GetPlayerData(LastNames, FirstNames, Scores);
- /**
- * Note:
- * Using the selection sort algorithm.
- */
- int i;
- int j;
- int first;
- int TempScore;
- std::string TempLN;
- std::string TempFN;
- for(i = MAX_NUM_PLAYERS - 1 ; i > 0; i--)
- {
- first = 0; // Initialize to subscript of first element.
- for(j = 1; j <= i; j++) // Locate smallest between positions 1 and i.
- {
- if(Scores[j] < Scores[first])
- first = j;
- }
- TempScore = Scores[first]; // Swap smallest found with element in position i.
- TempLN = LastNames[first];
- TempFN = FirstNames[first];
- Scores[first] = Scores[i];
- LastNames[first] = LastNames[i];
- FirstNames[first] = FirstNames[i];
- Scores[i] = TempScore;
- LastNames[i] = TempLN;
- FirstNames[i] = TempFN;
- }
- /**
- * Store the sorted player data:
- */
- PDataDest->SetPlayerData(LastNames, FirstNames, Scores);
- return;
- }
- /**
- * The SortNamesAsc() function:
- *
- * A function that sorts the player data, received as a source argument, ascending, by name, and
- * stores it in a location, received as a destination argument.
- */
- void SortNamesAsc(PlayerData *PDataSrc, PlayerData *PDataDest)
- {
- std::string *LastNames;
- std::string *FirstNames;
- int *Scores;
- /**
- * Get the player data:
- */
- PDataSrc->GetPlayerData(LastNames, FirstNames, Scores);
- /**
- * Note:
- * Using the insertion sort algorithm.
- */
- int i;
- int j;
- std::string LNKey;
- std::string FNKey;
- int ScKey;
- for(j = 1; j < MAX_NUM_PLAYERS; j++) // Start with 1 (not 0).
- {
- LNKey = LastNames[j];
- FNKey = FirstNames[j];
- ScKey = Scores[j];
- for(i = j - 1; (i >= 0) && (LastNames[i] > LNKey); i--) // Smaller values move up.
- {
- LastNames[i + 1] = LastNames[i];
- FirstNames[i + 1] = FirstNames[i];
- Scores[i + 1] = Scores[i];
- }
- LastNames[i + 1] = LNKey; // Put LNKey into its proper location.
- FirstNames[i + 1] = FNKey;
- Scores[i + 1] = ScKey;
- }
- /**
- * Store the sorted player data:
- */
- PDataDest->SetPlayerData(LastNames, FirstNames, Scores);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement