-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
145 lines (128 loc) · 3.73 KB
/
main.cpp
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
#include <iostream>
#include <iomanip> // for setw
#include <vector>
#include <list>
#include <algorithm> // for max and sort
#include <stdexcept> // for domain_error
#include <assert.h>
using namespace std;
// Functions from OrganizingPrograms
double median(const vector<double>& v) {
if (v.size() == 0)
throw domain_error("can't compute median of empty vector");
vector<double> v2(v);
sort(v2.begin(), v2.end());
int mid = v2.size() / 2;
double median;
if (v2.size() % 2 == 0)
median = (v2[mid - 1] + v2[mid]) / 2;
else
median = v2[mid];
return median;
}
double grade(double midterm, double final,
const vector<double>& homework) {
try {
return 0.2 * midterm + 0.4 * final + 0.4 * median(homework);
} catch (domain_error& e) {
throw domain_error("need at least one homework");
}
return 0; // to quiet the warning
}
// Note, changed to add an "if" and clear.
istream& read_homework(istream& in, vector<double>& homework) {
if (in) {
double x;
homework.clear();
while (cin >> x)
homework.push_back(x);
in.clear(); // clear the error state
}
return in;
}
// New structure for grouping student information.
struct student_info {
string name;
double midterm, final;
vector<double> homework;
};
istream& read(istream& in, student_info& s) {
in >> s.name >> s.midterm >> s.final;
return read_homework(in, s.homework);
}
double grade(const student_info& s) {
return grade(s.midterm, s.final, s.homework);
}
bool compare_names(const student_info& s1, const student_info& s2) {
return s1.name < s2.name;
}
bool failing_grade(const student_info& s) {
return grade(s) < 60;
}
// first try, not space efficient
vector<student_info>
extract_fails1(vector<student_info>& students) {
vector<student_info> pass, fail;
for (int i = 0; i != students.size(); ++i) {
if (failing_grade(students[i]))
fail.push_back(students[i]);
else
pass.push_back(students[i]);
}
students = pass; // copies pass into students
return fail;
}
// second try, not time efficient
vector<student_info>
extract_fails2(vector<student_info>& students) {
vector<student_info> fail;
int i = 0;
while (i != students.size()) {
if (failing_grade(students[i])) {
fail.push_back(students[i]);
students.erase(students.begin() + i);
} else
++i;
}
return fail;
}
// solution: use list instead of vector
list<student_info>
extract_fails(list<student_info>& students) {
list<student_info> fail;
list<student_info>::iterator iter = students.begin();
while (iter != students.end()) {
if (failing_grade(*iter)) {
fail.push_back(*iter);
iter = students.erase(iter);
} else
++iter;
}
return fail;
}
// need to update the following to use extract_fails.
int main() {
list<student_info> students;
student_info student;
string::size_type maxlen = 0;
while (read(cin, student)) {
maxlen = max(maxlen, student.name.size());
students.push_back(student);
}
extract_fails(students);
//sort(students.begin(), students.end(), compare_names);
students.sort(compare_names);
for (list<student_info>::iterator iter = students.begin();
iter != students.end(); ++iter) {
cout << setw(maxlen+1) << iter->name;
try {
double course_grade = grade(*iter);
streamsize old_prec = cout.precision(3);
cout << " " << course_grade << endl;
cout.precision(old_prec);
} catch (const domain_error& e) {
cout << e.what();
}
}
return 0;
}