1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <iomanip>
#include <string>
class GameEntry {
std::string entryName, entryDate;
int entryScore;
public:
GameEntry() : entryDate("00/00/0000"), entryScore() {}
GameEntry(std::string name, int score, std::string date)
: entryName(name), entryDate(date), entryScore(score) {}
std::string getName() { return entryName; }
std::string getDate() { return entryDate; }
int getScore() { return entryScore; }
};
class GameScore {
static const int MaxSize = 5;
std::string gameName;
GameEntry topRanked[MaxSize];
public:
GameScore(std::string game) : gameName(game) {}
void add(GameEntry entry) {
int pos = 0;
for ( ; pos < MaxSize; ++pos)
if (entry.getScore() > topRanked[pos].getScore())
break;
if (pos < MaxSize) {
for (int i = MaxSize - 1; i > pos; --i)
topRanked[i] = topRanked[i - 1];
topRanked[pos] = entry;
}
}
void print() {
std::cout << "Name: " << gameName << '\n';
for (int i = 0; i < MaxSize; ++i)
std::cout << i + 1 << ". "
<< std::setw(12) << std::left << topRanked[i].getName()
<< std::setw(12) << std::right << topRanked[i].getScore()
<< " " << topRanked[i].getDate() << '\n';
}
};
int main() {
GameEntry e1("Bob", 689, "10/17/2019"),
e2("Bill", 724, "10/24/2019"),
e3("Sam", 864, "10/30/2019"),
e4("John", 1000, "11/10/2019"),
e5("Jane", 875, "11/12/2019");
GameScore gs("Classic Pac-Man");
gs.add(e1);
gs.add(e2);
gs.add(e3);
gs.add(e4);
gs.add(e5);
gs.print();
}
|