From 2c9056a23e1a55fd21a8e314c903d9325bffd62e Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 6 Sep 2025 09:37:14 +0200 Subject: Move CPP code here Signed-off-by: Carlos Maiolino --- CPP/cpp_book/chap3/.grades.cpp.un~ | Bin 0 -> 75641 bytes CPP/cpp_book/chap3/grade.cpp | 27 ++++++++++++++++++++ CPP/cpp_book/chap3/grade.h | 11 +++++++++ CPP/cpp_book/chap3/grades | Bin 0 -> 71352 bytes CPP/cpp_book/chap3/grades.cpp | 48 ++++++++++++++++++++++++++++++++++++ CPP/cpp_book/chap3/median.cpp | 21 ++++++++++++++++ CPP/cpp_book/chap3/median.h | 8 ++++++ CPP/cpp_book/chap3/student_info.cpp | 34 +++++++++++++++++++++++++ CPP/cpp_book/chap3/student_info.h | 20 +++++++++++++++ CPP/cpp_book/chap3/tags | 4 +++ 10 files changed, 173 insertions(+) create mode 100644 CPP/cpp_book/chap3/.grades.cpp.un~ create mode 100644 CPP/cpp_book/chap3/grade.cpp create mode 100644 CPP/cpp_book/chap3/grade.h create mode 100755 CPP/cpp_book/chap3/grades create mode 100644 CPP/cpp_book/chap3/grades.cpp create mode 100644 CPP/cpp_book/chap3/median.cpp create mode 100644 CPP/cpp_book/chap3/median.h create mode 100644 CPP/cpp_book/chap3/student_info.cpp create mode 100644 CPP/cpp_book/chap3/student_info.h create mode 100644 CPP/cpp_book/chap3/tags (limited to 'CPP/cpp_book/chap3') 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 Binary files /dev/null and b/CPP/cpp_book/chap3/.grades.cpp.un~ 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 +#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& 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 +#include "student_info.h" + +double grade(double midterm, double final, double homework); +double grade(double midterm, double final, const std::vector& 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 Binary files /dev/null and b/CPP/cpp_book/chap3/grades 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 // IO Manipulators +#include +#include +#include +#include +#include +#include +#include "grade.h" +#include "student_info.h" + +using std::cin; using std::string; +using std::cout; using std::endl; + +int +main(void) +{ + std::vector 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::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 +#include +#include + +double median(std::vector vec) +{ + std::vector::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 + +double median(std::vector 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& 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 +#include +#include + +struct StudentInfo { + std::string name; + double midterm; + double final; + std::vector homework; +}; + + +bool compare_students(const StudentInfo& a, const StudentInfo& b); +std::istream& read_homework(std::istream& in, std::vector& 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 vec)$/ +read_homework grades.cpp /^istream& read_homework(istream& in, std::vector