Skip to content

Instantly share code, notes, and snippets.

@Rhomboid
Created December 5, 2013 08:42
Show Gist options
  • Select an option

  • Save Rhomboid/7802058 to your computer and use it in GitHub Desktop.

Select an option

Save Rhomboid/7802058 to your computer and use it in GitHub Desktop.
C++11 homework example (Superbowl scores)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <limits>
#include <iterator>
#include <algorithm>
#include <utility>
using namespace std;
class GameList {
struct Game {
string winner, loser;
int score;
bool operator<(const Game& rhs) { return score < rhs.score; }
friend istream& operator>>(istream& is, Game& g) {
getline(is, g.winner);
getline(is, g.loser);
is >> g.score;
return is.ignore(numeric_limits<streamsize>::max(), '\n');
}
friend ostream& operator<<(ostream& os, const Game& g) {
return os << left << setw(15) << g.winner << setw(15) << g.loser << g.score << '\n';
}
};
vector<Game> games;
public:
GameList(istream& is) : games { istream_iterator<Game>(is), istream_iterator<Game>() } {}
friend ostream& operator<<(ostream& os, const GameList& gl) {
os << left << setw(15) << "Winner" << setw(15) << "Loser" << "Score\n";
copy(begin(gl.games), end(gl.games), ostream_iterator<Game>(os));
return os << '\n';
}
double average_score() {
return accumulate(begin(games), end(games), 0.,
[](double t, const Game& g) { return t + g.score; }) / games.size();
}
size_t win_more_than(int thresh) {
return count_if(begin(games), end(games), [=](const Game& g) { return g.score > thresh; });
}
pair<string, string> most_pts() {
const Game &g = *max_element(begin(games), end(games));
return { g.winner, g.loser };
}
pair<string, string> fewest_pts() {
const Game &g = *min_element(begin(games), end(games));
return { g.winner, g.loser };
}
};
int main()
{
ifstream infile { "superbowl.dat" };
if(!infile) {
cout << "Can't read file.\n";
return 1;
}
GameList gl { infile };
cout << gl;
cout << "Average number of points scored by winning teams: " << gl.average_score() << "\n";
cout << "Number of winning teams that scored more than 40: " << gl.win_more_than(40) << "\n";
string w, l;
tie(w, l) = gl.most_pts();
cout << "Winner of game with most points scored was " << w << " and loser was " << l << "\n";
tie(w, l) = gl.fewest_pts();
cout << "Winner of game with fewest points scored was " << w << " and loser was " << l << "\n";
}
Winner Loser Score
Packers Chiefs 35
Packers Raiders 33
Jets Colts 16
Chiefs Vikings 23
Colts Cowboys 16
Cowboys Dolphins 24
Dolphins Redskins 14
Dolphins Vikings 24
Steelers Vikings 16
Steelers Cowboys 21
Raiders Vikings 32
Cowboys Broncos 27
Steelers Vikings 35
Steelers Rams 31
Raiders Eagles 27
49ers Bengals 26
Redskins Dolphins 27
Raiders Redskings 38
49ers Dolphins 38
Bears Patriots 46
Giants Broncos 39
Redskins Broncos 42
49ers Bengals 20
49ers Broncos 55
Giants Bills 20
Redskins Bills 37
Cowboys Bills 52
Cowboys Bills 30
49ers Chargers 49
Cowboys Steelers 27
Packers Patriots 35
Broncos Packers 31
Broncos Falcons 34
Rams Titans 23
Ravens Giants 34
Patriots Rams 20
Buccaneers Raiders 48
Patriots Panthers 32
Patriots Eagles 24
Steelers Seahawks 21
Colts Bears 29
Giants Patriots 17
Steelers Cardinals 27
Saints Colts 31
Packers Steelers 31
Giants Patriots 21
Ravens 49ers 34
Average number of points scored by winning teams: 30.0426
Number of winning teams that scored more than 40: 6
Winner of game with most points scored was 49ers and loser was Broncos
Winner of game with fewest points scored was Dolphins and loser was Redskins
Packers
Chiefs
35
Packers
Raiders
33
Jets
Colts
16
Chiefs
Vikings
23
Colts
Cowboys
16
Cowboys
Dolphins
24
Dolphins
Redskins
14
Dolphins
Vikings
24
Steelers
Vikings
16
Steelers
Cowboys
21
Raiders
Vikings
32
Cowboys
Broncos
27
Steelers
Vikings
35
Steelers
Rams
31
Raiders
Eagles
27
49ers
Bengals
26
Redskins
Dolphins
27
Raiders
Redskings
38
49ers
Dolphins
38
Bears
Patriots
46
Giants
Broncos
39
Redskins
Broncos
42
49ers
Bengals
20
49ers
Broncos
55
Giants
Bills
20
Redskins
Bills
37
Cowboys
Bills
52
Cowboys
Bills
30
49ers
Chargers
49
Cowboys
Steelers
27
Packers
Patriots
35
Broncos
Packers
31
Broncos
Falcons
34
Rams
Titans
23
Ravens
Giants
34
Patriots
Rams
20
Buccaneers
Raiders
48
Patriots
Panthers
32
Patriots
Eagles
24
Steelers
Seahawks
21
Colts
Bears
29
Giants
Patriots
17
Steelers
Cardinals
27
Saints
Colts
31
Packers
Steelers
31
Giants
Patriots
21
Ravens
49ers
34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment