summaryrefslogtreecommitdiff
path: root/CPP/cpp_book/chap3
diff options
context:
space:
mode:
Diffstat (limited to 'CPP/cpp_book/chap3')
-rw-r--r--CPP/cpp_book/chap3/.grades.cpp.un~bin0 -> 75641 bytes
-rw-r--r--CPP/cpp_book/chap3/grade.cpp27
-rw-r--r--CPP/cpp_book/chap3/grade.h11
-rwxr-xr-xCPP/cpp_book/chap3/gradesbin0 -> 71352 bytes
-rw-r--r--CPP/cpp_book/chap3/grades.cpp48
-rw-r--r--CPP/cpp_book/chap3/median.cpp21
-rw-r--r--CPP/cpp_book/chap3/median.h8
-rw-r--r--CPP/cpp_book/chap3/student_info.cpp34
-rw-r--r--CPP/cpp_book/chap3/student_info.h20
-rw-r--r--CPP/cpp_book/chap3/tags4
10 files changed, 173 insertions, 0 deletions
diff --git a/CPP/cpp_book/chap3/.grades.cpp.un~ b/CPP/cpp_book/chap3/.grades.cpp.un~
new file mode 100644
index 0000000..4d93a46
--- /dev/null
+++ b/CPP/cpp_book/chap3/.grades.cpp.un~
Binary files differ
diff --git a/CPP/cpp_book/chap3/grade.cpp b/CPP/cpp_book/chap3/grade.cpp
new file mode 100644
index 0000000..0a08217
--- /dev/null
+++ b/CPP/cpp_book/chap3/grade.cpp
@@ -0,0 +1,27 @@
+#include <vector>
+#include "student_info.h"
+#include "median.h"
+#include "grade.h"
+
+double grade(double midterm, double final, double homework)
+{
+ return (midterm * 0.2 +
+ final * 0.4 +
+ homework * 0.4);
+}
+
+/* Overloads above grade() */
+double grade(double midterm, double final, const std::vector<double>& hw)
+{
+ if (hw.size() == 0)
+ throw std::domain_error("Student has no homework");
+
+ return grade(midterm, final, median(hw));
+}
+
+double grade(const StudentInfo& s)
+{
+ return grade(s.midterm, s.final, s.homework);
+}
+
+
diff --git a/CPP/cpp_book/chap3/grade.h b/CPP/cpp_book/chap3/grade.h
new file mode 100644
index 0000000..7cd73d1
--- /dev/null
+++ b/CPP/cpp_book/chap3/grade.h
@@ -0,0 +1,11 @@
+#ifndef GRADE_H
+#define GRADE_H
+
+#include <vector>
+#include "student_info.h"
+
+double grade(double midterm, double final, double homework);
+double grade(double midterm, double final, const std::vector<double>& hw);
+double grade(const StudentInfo& s);
+
+#endif /* GRADE_H */
diff --git a/CPP/cpp_book/chap3/grades b/CPP/cpp_book/chap3/grades
new file mode 100755
index 0000000..f086657
--- /dev/null
+++ b/CPP/cpp_book/chap3/grades
Binary files differ
diff --git a/CPP/cpp_book/chap3/grades.cpp b/CPP/cpp_book/chap3/grades.cpp
new file mode 100644
index 0000000..3c8f19a
--- /dev/null
+++ b/CPP/cpp_book/chap3/grades.cpp
@@ -0,0 +1,48 @@
+#include <iomanip> // IO Manipulators
+#include <ios>
+#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+#include <stdexcept>
+#include "grade.h"
+#include "student_info.h"
+
+using std::cin; using std::string;
+using std::cout; using std::endl;
+
+int
+main(void)
+{
+ std::vector<StudentInfo> students;
+ StudentInfo record;
+ string::size_type maxlen = 0;
+
+ while (read_student(cin, record)) {
+ maxlen = std::max(maxlen, record.name.size());
+ students.push_back(record);
+ }
+
+ sort(students.begin(), students.end(), compare_students);
+
+ for (std::vector<StudentInfo>::size_type i = 0;
+ i != students.size();
+ i++) {
+
+ cout << students[i].name
+ << string(maxlen + 1 - students[i].name.size(), ' ');
+
+ try {
+ double final_grade = grade(students[i]);
+
+ std::streamsize prec = cout.precision();
+ cout << std::setprecision(3) << final_grade
+ << std::setprecision(prec);
+ } catch (std::domain_error& e){
+ cout << e.what();
+ }
+
+ cout << endl;
+ }
+ return 0;
+}
diff --git a/CPP/cpp_book/chap3/median.cpp b/CPP/cpp_book/chap3/median.cpp
new file mode 100644
index 0000000..e9932e8
--- /dev/null
+++ b/CPP/cpp_book/chap3/median.cpp
@@ -0,0 +1,21 @@
+#include <vector>
+#include <algorithm>
+#include <stdexcept>
+
+double median(std::vector<double> vec)
+{
+ std::vector<double>::size_type mid, size;
+
+ size = vec.size();
+
+ if (size == 0)
+ throw std::domain_error("median of an empty vector");
+
+ sort(vec.begin(), vec.end());
+
+ mid = size / 2;
+
+ return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2
+ : vec[mid];
+
+}
diff --git a/CPP/cpp_book/chap3/median.h b/CPP/cpp_book/chap3/median.h
new file mode 100644
index 0000000..f4e6580
--- /dev/null
+++ b/CPP/cpp_book/chap3/median.h
@@ -0,0 +1,8 @@
+#ifndef MEDIAN_H
+#define MEDIAN_H
+
+#include <vector>
+
+double median(std::vector<double> vec);
+
+#endif /* MEDIAN_H */
diff --git a/CPP/cpp_book/chap3/student_info.cpp b/CPP/cpp_book/chap3/student_info.cpp
new file mode 100644
index 0000000..a55338b
--- /dev/null
+++ b/CPP/cpp_book/chap3/student_info.cpp
@@ -0,0 +1,34 @@
+#include "student_info.h"
+
+bool compare_students(const StudentInfo& a, const StudentInfo& b)
+{
+ return a.name < b.name;
+}
+
+std::istream& read_homework(std::istream& in, std::vector<double>& hw)
+{
+ if (in) {
+ hw.clear(); // Empty the vector.
+
+ double x;
+ /*
+ * Loop invariant:
+ * - 'homework' contains all the homework grades read so far
+ */
+ while (in >> x)
+ hw.push_back(x);
+
+ in.clear(); // Clear the stream in case
+ // it will be used again.
+ }
+ return in;
+}
+
+std::istream& read_student(std::istream& is, StudentInfo& s)
+{
+ /* Read and store student's name, midterm and final */
+ is >> s.name >> s.midterm >> s.final;
+
+ read_homework(is, s.homework);
+ return is;
+}
diff --git a/CPP/cpp_book/chap3/student_info.h b/CPP/cpp_book/chap3/student_info.h
new file mode 100644
index 0000000..697f3c0
--- /dev/null
+++ b/CPP/cpp_book/chap3/student_info.h
@@ -0,0 +1,20 @@
+#ifndef STUDENT_INFO_H
+#define STUDENT_INFO_H
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+struct StudentInfo {
+ std::string name;
+ double midterm;
+ double final;
+ std::vector<double> homework;
+};
+
+
+bool compare_students(const StudentInfo& a, const StudentInfo& b);
+std::istream& read_homework(std::istream& in, std::vector<double>& hw);
+std::istream& read_student(std::istream& is, StudentInfo& s);
+
+#endif /* STUDENT_INFO_H */
diff --git a/CPP/cpp_book/chap3/tags b/CPP/cpp_book/chap3/tags
new file mode 100644
index 0000000..acf7309
--- /dev/null
+++ b/CPP/cpp_book/chap3/tags
@@ -0,0 +1,4 @@
+Mgrades.cpp grades.cpp /^main(void)$/
+grade grades.cpp /^double grade(double midterm, double final, double /
+median grades.cpp /^double median(std::vector<double> vec)$/
+read_homework grades.cpp /^istream& read_homework(istream& in, std::vector<do/