Skip to content

Commit 0c27a2d

Browse files
author
Kevin Languasco
committed
Chapter 4 - Completed
1 parent f0fda0e commit 0c27a2d

File tree

11 files changed

+165
-65
lines changed

11 files changed

+165
-65
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.ycm_extra_conf.py
22
.color_coded
33
_vimrc_local.vim
4+
*.o
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "Student_info.h"
2+
3+
using std::istream;
4+
using std::vector;
5+
using std::cout;
6+
7+
bool compare(const Student_info& x, const Student_info& y) {
8+
return x.name < y.name;
9+
}
10+
11+
istream& read(istream& is, Student_info& s) {
12+
cout << "Please enter the name, midterm grade and final grade: ";
13+
is >> s.name >> s.midterm >> s.final;
14+
read_hw(is, s.homework);
15+
return is;
16+
}
17+
18+
istream& read_hw(istream& in, vector<double>& hw) {
19+
if (in) {
20+
cout << "if(in)... Homework grades: ";
21+
// get rid of previous contents
22+
hw.clear();
23+
// read the homework grades
24+
double x;
25+
while (in>>x)
26+
hw.push_back(x);
27+
// clear the stream so that input will work for the next student
28+
in.clear();
29+
}
30+
return in;
31+
}
32+
33+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef GUARD_Student_info
2+
#define GUARD_Student_info
3+
4+
// Student_info.h header file
5+
#include <iostream>
6+
#include <string>
7+
#include <vector>
8+
9+
struct Student_info {
10+
std::string name;
11+
double midterm, final;
12+
std::vector<double> homework;
13+
};
14+
15+
bool compare(const Student_info&, const Student_info&);
16+
std::istream& read(std::istream&, Student_info&);
17+
std::istream& read_hw(std::istream&, std::vector<double>&);
18+
#endif
237 KB
Binary file not shown.

4-organizing_programs_and_data/grades

-45.5 KB
Binary file not shown.
Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,16 @@
1-
#include <iostream>
21
#include <vector>
3-
#include <algorithm>
42
#include <stdexcept>
5-
#include <string>
6-
#include <ios>
7-
#include <iomanip>
3+
#include "grades.h"
4+
#include "median.h"
5+
#include "Student_info.h"
86

9-
using std::vector;
10-
using std::sort;
117
using std::domain_error;
12-
using std::istream;
13-
using std::string;
14-
using std::cin;
15-
using std::cout;
16-
using std::endl;
17-
using std::setprecision;
18-
using std::streamsize;
8+
using std::vector;
199

2010
double grade(double midterm, double final, double homework) {
2111
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
2212
}
2313

24-
double median(vector<double> vec) {
25-
typedef vector<double>::size_type vec_sz;
26-
vec_sz size = vec.size();
27-
if (size==0)
28-
throw domain_error("median of an empty vector");
29-
30-
sort(vec.begin(), vec.end());
31-
32-
vec_sz mid = size/2;
33-
34-
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) /2 : vec[mid];
35-
}
3614

3715
// overloading
3816
double grade(double midterm, double final, const vector<double>& hw) {
@@ -41,43 +19,8 @@ double grade(double midterm, double final, const vector<double>& hw) {
4119
return grade(midterm, final, median(hw));
4220
}
4321

44-
istream& read_hw(istream& in, vector<double>& hw) {
45-
if (in) {
46-
// get rid of previous contents
47-
hw.clear();
48-
// read the homework grades
49-
double x;
50-
while (in>>x)
51-
hw.push_back(x);
52-
// clear the stream so that input will work for the next student
53-
in.clear();
54-
}
55-
return in;
56-
}
57-
58-
int main() {
59-
cout << "Please enter your first name: ";
60-
string name;
61-
cin >> name;
62-
cout << "Hello, " << name << "!" << endl;
63-
cout << "Please enter your midterm and final exam grades: ";
64-
double midterm, final;
65-
cin >> midterm >> final;
66-
cout << "Enter all your homework grades, "
67-
"followed by end-of-file: ";
68-
vector<double> homework;
69-
read_hw(cin, homework);
70-
try {
71-
double final_grade = grade(midterm, final, homework);
72-
streamsize prec = cout.precision();
73-
cout << "Your final grade is " << setprecision(3)
74-
<< final_grade << setprecision(prec) << endl;
75-
} catch (domain_error) {
76-
cout << endl << "You must enter your grades. "
77-
"Please try again." << endl;
78-
return 1;
79-
}
80-
return 0;
22+
double grade(const Student_info& s) {
23+
return grade(s.midterm, s.final, s.homework);
8124
}
8225

8326

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef GUARD_grades_h
2+
#define GUARD_grades_h
3+
4+
#include <vector>
5+
#include "Student_info.h"
6+
7+
double grade(double, double, double);
8+
double grade(double, double, const std::vector<double>&);
9+
double grade(const Student_info&);
10+
11+
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <algorithm>
2+
#include <iomanip>
3+
#include <ios>
4+
#include <iostream>
5+
#include <stdexcept>
6+
#include <string>
7+
#include <vector>
8+
#include "grades.h"
9+
#include "Student_info.h"
10+
11+
using std::cin;
12+
using std::cout;
13+
using std::domain_error;
14+
using std::endl;
15+
using std::max;
16+
using std::setprecision;
17+
using std::sort;
18+
using std::streamsize;
19+
using std::string;
20+
using std::vector;
21+
22+
int main() {
23+
vector<Student_info> students;
24+
Student_info record;
25+
string::size_type maxlen = 0;
26+
27+
while(read(cin, record)) {
28+
maxlen = max(maxlen, record.name.size());
29+
students.push_back(record);
30+
}
31+
sort(students.begin(), students.end(), compare);
32+
33+
for (vector<Student_info>::size_type i = 0; i != students.size(); ++i) {
34+
cout << students[i].name
35+
<< string(maxlen + 1 - students[i].name.size(), ' ');
36+
try {
37+
double final_grade = grade(students[i]);
38+
streamsize prec = cout.precision();
39+
cout << setprecision(3) << final_grade
40+
<< setprecision(prec);
41+
} catch (domain_error e) {
42+
cout << e.what();
43+
}
44+
cout << endl;
45+
}
46+
return 0;
47+
}
48+

4-organizing_programs_and_data/makefile

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
CXX = clang++
2-
C = clang++
2+
CC = clang++
3+
4+
CFLAGS = -g -Wall
5+
6+
all: evaluate
7+
8+
evaluate: main.o median.o Student_info.o grades.o
9+
$(CXX) $(CFLAGS) -o evaluate main.o median.o Student_info.o grades.o
10+
11+
main.o: main.cpp grades.h Student_info.h
12+
$(CXX) $(CFLAGS) -c main.cpp
13+
14+
median.o: median.cpp median.h
15+
$(CXX) $(CFLAGS) -c median.cpp
16+
17+
Student_info.o: Student_info.cpp Student_info.h
18+
$(CXX) $(CFLAGS) -c Student_info.cpp
19+
20+
grades.o: grades.cpp grades.h median.h Student_info.h
21+
$(CXX) $(CFLAGS) -c grades.cpp
322

4-
all: grades
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <algorithm>
2+
#include <stdexcept>
3+
#include <vector>
4+
5+
using std::domain_error;
6+
using std::sort;
7+
using std::vector;
8+
9+
double median(vector<double> vec) {
10+
typedef vector<double>::size_type vec_sz;
11+
vec_sz size = vec.size();
12+
if (size==0)
13+
throw domain_error("median of an empty vector");
14+
15+
sort(vec.begin(), vec.end());
16+
17+
vec_sz mid = size/2;
18+
19+
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) /2 : vec[mid];
20+
}
21+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef GUARD_median_h
2+
#define GUARD_median_h
3+
4+
#include <vector>
5+
double median(std::vector<double>);
6+
7+
#endif

0 commit comments

Comments
 (0)