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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
class Goalies
{
public:
// constructors
Goalies();
Goalies(const std::string& n, const std::string& t);
Goalies(const std::string& n, const std::string& t, int w, int l, int ga, int sa);
// implement setters
void setName(const std::string& n);
void setTeam(const std::string& t);
void setWins(int w);
void setLosses(int l);
void setGoalsAllowed(int ga);
void setShotsAgainst(int sa);
// implement getters
std::string getName() const;
std::string getTeam() const;
int getWins() const;
int getLosses() const;
int getGoalsAllowed() const;
int getShotsAgainst() const;
//double getSavePct() const;
private:
//attributes
std::string name;
std::string team;
int wins {};
int losses {};
int goalsAllowed {};
int shotsAgainst {};
};
Goalies::Goalies() {}
Goalies::Goalies(const std::string& n, const std::string& t) : name(n), team(t) {}
Goalies::Goalies(const std::string& n, const std::string& t, int w, int l, int ga, int sa) : name(n), team(t), wins(w), losses(l), goalsAllowed(ga), shotsAgainst(sa) {}
void Goalies::setName(const std::string& n) { name = n; }
void Goalies::setTeam(const std::string& t) { team = t; }
void Goalies::setWins(int w) { wins = w; }
void Goalies::setLosses(int l) { losses = l; }
void Goalies::setGoalsAllowed(int ga) { goalsAllowed = ga; }
void Goalies::setShotsAgainst(int sa) { shotsAgainst = sa; }
std::string Goalies::getName() const { return name; }
std::string Goalies::getTeam() const { return team; }
int Goalies::getWins() const { return wins; }
int Goalies::getLosses() const { return losses; }
int Goalies::getGoalsAllowed() const { return goalsAllowed; }
int Goalies::getShotsAgainst() const { return shotsAgainst; }
void split(const std::string& s, std::string elems[], size_t& noElems, char delim = ',')
{
const size_t max_elems {noElems};
noElems = 0;
const auto pb {[&](size_t st, size_t dp) {elems[noElems++] = s.substr(st, dp - st); }};
for (auto delimPosition = s.find(delim), tokenStart = 0U; noElems < max_elems && delimPosition != std::string::npos; )
if (pb(tokenStart, delimPosition); (delimPosition = s.find(delim, (tokenStart = ++delimPosition))) == std::string::npos)
pb(tokenStart, delimPosition);
}
std::istream& operator>>(std::istream& is, Goalies& g)
{
if (std::string line; getline(is, line)) {
constexpr size_t No_Elems {6};
size_t gotElems {No_Elems};
std::string tokens[No_Elems] {};
if (split(line, tokens, gotElems); gotElems == No_Elems) {
g.setName(tokens[0]);
g.setTeam(tokens[1]);
g.setWins(atoi(tokens[2].c_str()));
g.setLosses(atoi(tokens[3].c_str()));
g.setGoalsAllowed(atoi(tokens[4].c_str()));
g.setShotsAgainst(atoi(tokens[5].c_str()));
}
}
return is;
}
std::ostream& operator<<(std::ostream& os, const Goalies& g)
{
return os << std::left << std::setw(25) << g.getName() << std::setw(15) << g.getTeam() << std::right << std::setw(4) << g.getWins() << std::setw(10) << g.getLosses() << std::setw(15) << g.getGoalsAllowed() << std::setw(15) << g.getShotsAgainst();
}
void showNames(const Goalies goalies[], size_t no_goalies)
{
std::cout << std::left << std::setw(25) << "Name" << std::setw(15) << "Team" << std::setw(8) << "Wins" << std::setw(8) << "Losses" << std::setw(15) << "Goals Allowed" << std::setw(8) << "Shots Against\n";
for (size_t g = 0; g < no_goalies; ++g)
std::cout << goalies[g] << '\n';
}
void sortWins(Goalies goalies[], size_t no_goalies)
{
std::sort(goalies, goalies + no_goalies, [](auto a, auto b) {return a.getWins() < b.getWins(); });
}
void sortLosses(Goalies goalies[], size_t no_goalies)
{
std::sort(goalies, goalies + no_goalies, [](auto a, auto b) {return a.getLosses() < b.getLosses(); });
}
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && !(std::cin >> i)) {
std::cout << "Invalid number\n";
if (prm.empty()) std::cout << "Please re-enter: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return i;
}
int main()
{
constexpr size_t Max_Goalies {100};
std::ifstream myFile("goalie_stats.csv");
if (!myFile.is_open()) {
std::cout << "Cannot open input file\n";
return 1;
}
Goalies goalies[Max_Goalies] {};
size_t no_goalies {};
for (Goalies g; no_goalies < Max_Goalies && (myFile >> g); goalies[no_goalies++] = g);
std::cout << "Data read OK. " << no_goalies << " records\n";
int choice {};
do
{
std::cout << "\n0. to quit\n";
std::cout << "1. Sort by Wins\n";
std::cout << "2. Sort By Losses\n";
switch (choice = getInt("Enter choice: "))
{
case 0:
std::cout << "Thanks for using my program!\n";
break;
case 1:
std::cout << "Here are the Wins sorted\n\n";
sortWins(goalies, no_goalies);
showNames(goalies, no_goalies);
break;
case 2:
std::cout << "Here are the Losses sorted\n\n";
sortLosses(goalies, no_goalies);
showNames(goalies, no_goalies);
break;
default:
std::cout << "Invalid option\n";
break;
}
} while (choice != 0);
}
|