Accelerated C++ Chapter4

 1 #include <iostream>
 2 #include <stdexcept>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <iomanip>
 6 #include <ios>
 7 
 8 using namespace std;
 9 
10 double grade(double midterm, double final, double homework)
11 {
12     return 0.2 * midterm + 0.4 * final + 0.4 * homework;
13 }
14 
15 double median(vector<double> vec)
16 {
17     typedef vector<double>::size_type vec_sz;
18     vec_sz size = vec.size();
19     if(size == 0)
20         throw domain_error("median of an empty vector");
21     sort(vec.begin(), vec.end());
22     vec_sz mid = size / 2;
23     return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
24 }
25 
26 double grade(double midterm, double final, const vector<double>& hw)
27 {
28     if(hw.size() == 0)
29         throw domain_error("student has done no homework");
30     return grade(midterm, final, median(hw));
31 }
32 
33 istream& read_hw(istream& in, vector<double>& hw)
34 {
35     if(in)
36     {
37         hw.clear();
38         double x;
39         while(cin >> x)
40             hw.push_back(x);
41         in.clear();
42     }
43     return in;
44 }
45 
46 int main()
47 {
48     cout << "Please enter your first name: ";
49     string name;
50     cin >> name;
51     cout << "Hello, " << name << "!" << endl;
52 
53     cout << "Please enter your midterm and final exam grades: ";
54     double midterm, final;
55     cin >> midterm >> final;
56 
57     cout << "Enter all your homework grades, "
58             "followed by end-of-file: ";
59     vector<double> homework;
60 
61     read_hw(cin, homework);
62 
63     try{
64         double final_grade = grade(midterm, final, homework);
65         streamsize prec = cout.precision();
66         cout << "Your final grade is " << setprecision(3)
67              << final_grade << setprecision(prec) << endl;
68     } catch(domain_error) {
69         cout << endl << "You must enter your grades. "
70                 "Please try again." << endl;
71         return 1;
72     }
73 
74     return 0;
75 }

 

grade.h

 1 #ifndef GUARD_grade.h
 2 #define GUARD_grade_h
 3 
 4 #include <vector>
 5 #include "Student_info.h"
 6 #include "median.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

 

median.h

1 #ifndef GUARD_Student_info
2 #define GUARD_Student_info
3 
4 #include <vector>
5 double median(std::vector<double>);
6 
7 #endif // GUARD_Student_info

 

Student_info.h

 1 #ifndef GUARD_Student_info
 2 #define GUARD_Student_info
 3 
 4 #include <iostream>
 5 #include <string>
 6 #include <vector>
 7 
 8 struct Student_info
 9 {
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 
19 #endif

 

主文件

 1 #include <algorithm>
 2 #include <iomanip>
 3 #include <ios>
 4 #include <iostream>
 5 #include <stdexcept>
 6 #include <string>
 7 #include <vector>
 8 #include "grade.h"
 9 #include "Student_info.h"
10 #include "median.h"
11 
12 using namespace std;
13 
14 double median(vector<double> vec)
15 {
16     typedef vector<double>::size_type vec_sz;
17     vec_sz size = vec.size();
18     if(size == 0)
19         throw domain_error("median of an empty vector");
20     sort(vec.begin(), vec.end());
21     vec_sz mid = size / 2;
22     return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
23 }
24 
25 bool compare(const Student_info& x, const Student_info& y)
26 {
27     return x.name < y.name;
28 }
29 
30 istream& read(istream& is, Student_info& s)
31 {
32     is >> s.name >> s.midterm >> s.final;
33     read_hw(is, s.homework);
34     return is;
35 }
36 
37 istream& read_hw(istream& in, vector<double>& hw)
38 {
39     if(in)
40     {
41         hw.clear();
42         double x;
43         while(cin >> x)
44             hw.push_back(x);
45         in.clear();
46     }
47     return in;
48 }
49 
50 double grade(double midterm, double final, double homework)
51 {
52     return 0.2 * midterm + 0.4 * final + 0.4 * homework;
53 }
54 
55 double grade(double midterm, double final, const vector<double>& hw)
56 {
57     if(hw.size() == 0)
58         throw domain_error("student has done no homework");
59     return grade(midterm, final, median(hw));
60 }
61 
62 double grade(const Student_info& s)
63 {
64     return grade(s.midterm, s.final, s.homework);
65 }
66 
67 int main()
68 {
69     vector<Student_info> students;
70     Student_info record;
71     string::size_type maxlen = 0;
72 
73     while(read(cin, record))
74     {
75         maxlen = max(maxlen, record.name.size());
76         students.push_back(record);
77     }
78 
79     sort(students.begin(), students.end(), compare);
80     for(vector<Student_info>::size_type i = 0;
81         i != students.size(); ++i)
82     {
83         cout << students[i].name
84              << string(maxlen + 1 - students[i].name.size(), ' ');
85 
86         try{
87             double final_grade = grade(students[i]);
88             streamsize prec = cout.precision();
89             cout << setprecision(3) << final_grade << setprecision(prec);
90         } catch(domain_error e) {
91             cout << e.what();
92         }
93         cout << endl;
94     }
95     return 0;
96 }

 

posted on 2013-10-14 16:11  startfromjava  阅读(176)  评论(0)    收藏  举报

导航