-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLab6.cpp
More file actions
127 lines (112 loc) · 2.51 KB
/
Lab6.cpp
File metadata and controls
127 lines (112 loc) · 2.51 KB
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
/*
* Lab6.cpp
*
* Created on: Oct 17, 2013
* Author: leon
*/
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
struct Grades {
string id;
map<string, int> scores;
};
inline ostream& operator<<(ostream& os, const Grades& g) {
os << g.id << endl;
os << g.scores.size() << endl;
for (auto x : g.scores) {
os << x.first << " " << x.second << endl;
}
return os;
}
inline istream& operator>>(istream& is, Grades& g) {
string name;
int score;
size_t i, count;
if (!(is >> g.id)) {
return is;
}
is >> count;
g.scores.clear();
for (i = 0; i < count; ++i) {
is >> name >> score;
g.scores.insert(pair<string, int>(name, score));
}
return is;
}
class Cmp {
private:
string courseName_;
public:
Cmp(string name) :
courseName_(name) {
}
;
bool operator()(const Grades& g1, const Grades& g2) {
// auto sc1map = g1.scores;
// auto sc2map = g2.scores;
// if (sc1map.find(courseName_) == sc1map.end()
// && sc2map.find(courseName_) != sc2map.end()) {
// return false;
// }else if (sc1map.find(courseName_) != sc1map.end()
// && sc2map.find(courseName_) == sc2map.end()){
// return true;
// }else if (sc1map.find(courseName_) == sc1map.end()
// && sc2map.find(courseName_) == sc2map.end()){
// return true;
// }else {
// return !(sc1map.find(courseName_)->second < sc2map.find(courseName_) ->second);
// }
auto s1it = g1.scores.find(courseName_);
auto s2it = g2.scores.find(courseName_);
auto s1 = s1it == g1.scores.end() ? -1 : s1it->second;
auto s2 = s2it == g2.scores.end() ? -1 : s2it->second;
return s1 > s2;
}
};
class Stats {
private:
int sum_, count_;
double average_;
string name_;
public:
Stats(string n) :
name_(n) {
sum_ = 0;
count_ = 0;
average_ = 0;
}
Stats& operator()(const Grades& g) {
if (g.scores.find(name_) != g.scores.end()) {
sum_ += g.scores.find(name_)->second;
count_++;
average_ = (double) sum_ / count_;
}
return *this;
}
int getAvg() const {
return average_;
}
};
int lab6_main(int argc, char** argv) {
vector<Grades> vog;
Grades g;
while (cin >> g) {
vog.push_back(g);
}
cerr << "begin sorting" << endl;
sort(vog.begin(), vog.end(), Cmp("COMP3512"));
cerr << "done sorting" << endl;
for (auto x : vog) {
cout << x << endl;
}
cout << endl;
cerr << "start calculate avg" << endl;
Stats a = for_each(vog.begin(), vog.end(), Stats("COMP3512"));
cout << "Average is : " << a.getAvg() << endl;
cerr << "done calculating" << endl;
cerr << "check out put" << endl;
return 0;
}