Skip to content

Commit 3975f46

Browse files
committed
15.05.2014
1 parent 2bab797 commit 3975f46

File tree

8 files changed

+171
-1
lines changed

8 files changed

+171
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <algorithm>
2+
#include <iomanip> // defines setprecision manipulator
3+
#include <ios> // defines streamsize
4+
#include <iostream>
5+
#include <stdexcept>
6+
#include <string>
7+
#include <vector>
8+
#include "grade.h"
9+
#include "Student_info.h"
10+
11+
using std::cin; using std::setprecision;
12+
using std::cout; using std::sort;
13+
using std::domain_error; using std::streamsize;
14+
using std::endl; using std::string;
15+
using std::max; using std::vector;
16+
17+
int main(){
18+
vector<Student_info> students;
19+
Student_info record; // temporary storage before pushing into students vector
20+
string::size_type maxlen = 0; // to store length of the longest name for padding.
21+
//must be of string type to be used with max function
22+
23+
// read and store all the students data.
24+
// Invariant: students contains all the student records read so far
25+
while (read(cin, record)){
26+
// find length of the longest name
27+
maxlen = max(maxlen, record.name.size());
28+
students.push_back(record);
29+
}
30+
31+
// sort student records in alphabetical order
32+
sort(students.begin(), students.end(), compare);
33+
34+
// output names and grades
35+
for (vector<Student_info>::size_type i = 0; i != students.size(); ++i){
36+
//write the name, padded on the right to maxlen + 1 characters
37+
cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' ');
38+
39+
// compute and write the grade
40+
try {
41+
double final_grade = grade(students[i]); // calculate student's grade first in case domain_error is thrown
42+
streamsize prec = cout.precision();
43+
cout << setprecision(3) << final_grade
44+
<< setprecision(prec); // restore precision
45+
} catch (domain_error e){
46+
cout << e.what();
47+
}
48+
cout << endl;
49+
}
50+
51+
return 0;
52+
}
53+

Chapter04/4-6/Student_info.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// source file for Student_info-related functions
2+
#include "Student_info.h"
3+
4+
using std::istream; using std::vector;
5+
6+
bool compare(const Student_info& x, const Student_info& y){
7+
return x.name < y.name;
8+
}
9+
10+
istream& read(istream& is, Student_info& s){
11+
// read and store the student's name and midterm and final exam grades
12+
is >> s.name >> s.midterm >> s.final;
13+
14+
read_hw(is, s.homework); // read and store all the student's homework grades
15+
return is;
16+
}
17+
18+
// read homework grades from an input stream into a 'vector'
19+
istream& read_hw(istream& in, vector<double>& hw){
20+
if (in) {
21+
// get rid of previous contents
22+
hw.clear();
23+
24+
// read homework grades
25+
double x;
26+
while (in >> x)
27+
hw.push_back(x);
28+
29+
// clear the stream so that input will work for the next student
30+
in.clear();
31+
}
32+
return in;
33+
}

Chapter04/4-6/Student_info.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
// We will use these functions only if we are using the Student_info structure,
15+
// so it makes sense to package them together with the structure definition
16+
bool compare(const Student_info&, const Student_info&);
17+
std::istream& read(std::istream&, Student_info&);
18+
std::istream& read_hw(std::istream&, std::vector<double>&);
19+
#endif

Chapter04/4-6/grade.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdexcept>
2+
#include <vector>
3+
#include "grade.h"
4+
#include "median.h"
5+
#include "Student_info.h"
6+
7+
using std::domain_error; using std::vector;
8+
9+
// return calculated values
10+
double grade(double midterm, double final, double homework){
11+
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
12+
}
13+
14+
// compute a student's overall grade from midterm and final exam grades
15+
// and vector of homework grades.
16+
double grade(double midterm, double final, const vector<double>& hw){
17+
if (hw.size() == 0)
18+
throw domain_error("student has done no homework");
19+
return grade(midterm, final, median(hw));
20+
}
21+
22+
// compute grade from the Student_info reference to struct object
23+
double grade(const Student_info& s){
24+
return grade(s.midterm, s.final, s.homework);
25+
}

Chapter04/4-6/grade.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef GUARD_grade_h
2+
#define GUARD_grade_h
3+
4+
// grade.h
5+
#include <vector>
6+
#include "Student_info.h"
7+
8+
double grade(double, double, double);
9+
double grade(double, double, const std::vector<double>&);
10+
double grade(const Student_info&);
11+
12+
#endif

Chapter04/4-6/median.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// source file for the median function
2+
#include <algorithm> // to get the declaration of sort
3+
#include <stdexcept> // to get the declaration of domain_error
4+
#include <vector> // to get the declaration of vector
5+
6+
using std::domain_error; using std::sort; using std::vector;
7+
8+
// compute the median of the vector<double>
9+
// note that calling this function copies the entire argument vector
10+
double median(vector<double> vec){
11+
typedef vector<double>::size_type vec_sz;
12+
13+
vec_sz size = vec.size();
14+
if (size == 0)
15+
throw domain_error("median of an empty vector");
16+
17+
sort(vec.begin(), vec.end());
18+
19+
vec_sz mid = size / 2;
20+
21+
return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
22+
}

Chapter04/4-6/median.h

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

Chapter04/test.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)