commit 5c87702a78a31d156c38ca3c6f2da7c129d1ff76 parent 4e9a4a539e3acbf168d389a69d83f350de44c917 Author: Christos Margiolis <christos@margiolis.net> Date: Fri, 22 May 2020 17:31:21 +0300 code cleanup Diffstat:
28 files changed, 255 insertions(+), 261 deletions(-)
diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp @@ -1,76 +1,81 @@ +#include <cstring> #include <iostream> #include <iomanip> #include <algorithm> #include <string> -#include <string.h> class Student { private: - char *AM; + char *id; std::string name; unsigned int semester; unsigned int pcourses; float *grades; - int size; + std::size_t size; public: - Student(const char *AM, const std::string& name); - Student(const char *AM, const std::string& name, unsigned int semester); - Student(const char *AM, const std::string& name, unsigned int semester, - unsigned int pcourses, const float *grades); + Student(const char *id, const std::string& name); + Student(const char *id, const std::string& name, + const unsigned int semester); + Student(const char *id, const std::string& name, + const unsigned int semester, + const unsigned int pcourses, + const float *grades); Student(const Student& s); ~Student(); const std::string& get_name() const; - constexpr const char *get_AM() const {return this->AM;} + constexpr const char *get_id() const {return this->id;} constexpr unsigned int get_semester() const {return this->semester;} constexpr unsigned int get_pcourses() const {return this->pcourses;} constexpr float *get_grades() const {return this->grades;} - void set_AM(const char *AM); - void set_name(const std::string& name); - void set_semester(unsigned int semester); - void set_pcourses(unsigned int pcourses); - void set_grades(float *grades); + void set_id (const char *id); + void set_name (const std::string& name); + void set_semester(const unsigned int semester); + void set_pcourses(const unsigned int pcourses); + void set_grades (const float *grades); - void add_grade(float grade); + void add_grade(const float grade); void detailed_print() const; private: - char *convert_AM(const char *AM); + char *convert_id (const char *id); float *convert_PSG(const float *grades); float calc_average() const; }; -Student::Student(const char *AM, const std::string& name) - :AM(convert_AM(AM)), name(name), semester(1), pcourses(0), grades(nullptr) {} +Student::Student(const char *id, const std::string& name) + :id(convert_id(id)), name(name), semester(1), pcourses(0), grades(nullptr) {} -Student::Student(const char *AM, const std::string& name, unsigned int semester) - :AM(convert_AM(AM)), name(name), semester(semester), pcourses(0), grades(nullptr) {} +Student::Student(const char *id, const std::string& name, + const unsigned int semester) + :id(convert_id(id)), name(name), semester(semester), pcourses(0), grades(nullptr) {} -Student::Student(const char *AM, const std::string& name, unsigned int semester, - unsigned int pcourses, const float *grades) - :AM(convert_AM(AM)), name(name), semester(semester), pcourses(pcourses), grades(convert_PSG(grades)) {} +Student::Student(const char *id, const std::string& name, + const unsigned int semester, + const unsigned int pcourses, const float *grades) + :id(convert_id(id)), name(name), semester(semester), pcourses(pcourses), grades(convert_PSG(grades)) {} Student::Student(const Student& s) :name(s.name), semester(s.semester), pcourses(s.pcourses) { - int sl = strlen(s.AM); - AM = new char[sl + 1]; - memcpy(AM, s.AM, sizeof(s.AM) + (sl+1)); + int sl = std::strlen(s.id); + id = new char[sl + 1]; + std::memcpy(id, s.id, sizeof(s.id) + (sl+1)); this->grades = new float[s.pcourses]; - memcpy(grades, s.grades, sizeof(float) * s.pcourses); + std::memcpy(grades, s.grades, sizeof(float) * s.pcourses); } Student::~Student() { - if (this->AM != nullptr) + if (this->id != nullptr) { - delete[] this->AM; - this->AM = nullptr; + delete[] this->id; + this->id = nullptr; } - if (this->AM != nullptr) + if (this->id != nullptr) { delete[] this->grades; this->grades = nullptr; @@ -84,10 +89,10 @@ Student::get_name() const } void -Student::set_AM(const char *AM) +Student::set_id(const char *id) { - if (this->AM != nullptr) delete[] this->AM; - this->AM = convert_AM(AM); + if (this->id != nullptr) delete[] this->id; + this->id = convert_id(id); } void @@ -97,29 +102,29 @@ Student::set_name(const std::string& name) } void -Student::set_semester(unsigned int semester) +Student::set_semester(const unsigned int semester) { this->semester = semester; } void -Student::set_pcourses(unsigned int pcourses) +Student::set_pcourses(const unsigned int pcourses) { this->pcourses = pcourses; } void -Student::set_grades(float *grades) +Student::set_grades(const float *grades) { this->grades = convert_PSG(grades); } char * -Student::convert_AM(const char *AM) +Student::convert_id(const char *id) { - int len = strlen(AM); + int len = std::strlen(id); char *tmp = new char[len+1]; - memcpy(tmp, AM, len+1); + std::memcpy(tmp, id, len+1); return tmp; } @@ -129,7 +134,7 @@ Student::convert_PSG(const float *grades) if (pcourses > 0 && grades != nullptr) { float *tmp = new float[pcourses]; - memcpy(tmp, grades, sizeof(float) * pcourses); + std::memcpy(tmp, grades, sizeof(float) * pcourses); if (this->grades != nullptr) delete[] this->grades; return tmp; } @@ -137,12 +142,12 @@ Student::convert_PSG(const float *grades) } void -Student::add_grade(float grade) +Student::add_grade(const float grade) { float *tmp = new float[pcourses+1]; if (grades != nullptr) { - memcpy(tmp, grades, sizeof(float) * pcourses); + std::memcpy(tmp, grades, sizeof(float) * pcourses); delete[] grades; } tmp[pcourses] = grade; @@ -178,8 +183,8 @@ Student::calc_average() const else return 0.0f; } -std::ostream& operator<< (std::ostream& stream, const Student& s); -static void cont(void); +static std::ostream& operator<< (std::ostream& stream, const Student& s); +static void cont(); static void constructor1(const Student& s1); static void ostream_overload(const Student& s1); static void constructor2(const Student& s2); @@ -192,17 +197,13 @@ static void addgrd(Student& s3); int main(int argc, char **argv) { - std::string n1 = "Name Surname"; - std::string n2 = "Name Surnamington"; - std::string n3 = "Name Surnaming"; - - Student *s1 = new Student("12345678", n1); + Student *s1 = new Student("12345678", std::string("Name Surname")); system("clear || cls"); constructor1(*s1); cont(); ostream_overload(*s1); cont(); delete s1; - Student *s2 = new Student("92345678", n2, 2); + Student *s2 = new Student("92345678", std::string("Name Surnamington"), 2); constructor2(*s2); cont(); Student *copystud = new Student(*s2); @@ -211,7 +212,7 @@ main(int argc, char **argv) delete s2; float *grd = new float[4]{9.4f, 8.4f, 5.5f, 6.3f}; - Student *s3 = new Student("72345678", n3, 2, 4, grd); + Student *s3 = new Student("72345678", std::string("Name Surnaming"), 2, 4, grd); delete[] grd; constructor3(*s3); cont(); detprint(*s3); cont(); @@ -226,31 +227,31 @@ std::ostream& operator<< (std::ostream& stream, const Student& s) { return stream << - "AM: " << s.get_AM() << std::endl << + "ID: " << s.get_id() << std::endl << "Name: " << s.get_name() << std::endl << "Semester: " << s.get_semester() << std::endl; } -static void -cont(void) +void +cont() { std::cout << std::endl; std::cout << "Press <ENTER> to continue. . ."; if (std::cin.get()) system("clear || cls"); } -static void +void constructor1(const Student& s1) { - std::cout << "Constructor for s1 (AM, Name)" << std::endl; + std::cout << "Constructor for s1 (ID, Name)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << s1.get_AM() << std::endl; + std::cout << "ID: " << s1.get_id() << std::endl; std::cout << "Name: " << s1.get_name() << std::endl; std::cout << "Semester (default value): " << s1.get_semester() << std::endl; std::cout << "Courses passed (default value): " << s1.get_pcourses() << std::endl; } -static void +void ostream_overload(const Student& s1) { std::cout << "std::ostream overload" << std::endl; @@ -258,34 +259,34 @@ ostream_overload(const Student& s1) std::cout << s1 << std::endl; } -static void +void constructor2(const Student& s2) { - std::cout << "Constructor for s2 (AM, Name, Semester)" << std::endl; + std::cout << "Constructor for s2 (ID, Name, Semester)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << s2.get_AM() << std::endl; + std::cout << "ID: " << s2.get_id() << std::endl; std::cout << "Name: " << s2.get_name() << std::endl; std::cout << "Semester: " << s2.get_semester() << std::endl; std::cout << "Courses passed (default value): " << s2.get_pcourses() << std::endl; } -static void +void copy_constructor(const Student& copystud) { std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << copystud.get_AM() << std::endl; + std::cout << "ID: " << copystud.get_id() << std::endl; std::cout << "Name: " << copystud.get_name() << std::endl; std::cout << "Semester: " << copystud.get_semester() << std::endl; std::cout << "Courses passed (default value): " << copystud.get_pcourses() << std::endl; } -static void +void constructor3(const Student& s3) { - std::cout << "Constructor for s3 (AM, Name, Semester, Courses passed, Grades)" << std::endl; + std::cout << "Constructor for s3 (ID, Name, Semester, Courses passed, Grades)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << s3.get_AM() << std::endl; + std::cout << "ID: " << s3.get_id() << std::endl; std::cout << "Name: " << s3.get_name() << std::endl; std::cout << "Semester: " << s3.get_semester() << std::endl; std::cout << "Courses passed: " << s3.get_pcourses() << std::endl; @@ -302,7 +303,7 @@ constructor3(const Student& s3) } } -static void +void detprint (const Student& s3) { std::cout << "Detailed print of s3's grades" << std::endl; @@ -310,10 +311,10 @@ detprint (const Student& s3) s3.detailed_print(); } -static void +void setters(Student& s3) { - s3.set_AM("010101"); + s3.set_id("010101"); s3.set_name("AAAA"); s3.set_semester(100); s3.set_pcourses(2); @@ -324,7 +325,7 @@ setters(Student& s3) std::cout << "Setters example using s3" << std::endl; std::cout << "----------------------------" << std::endl; std::cout << "Input: 010101" << std::endl;; - std::cout << "New AM: " << s3.get_AM() << std::endl; + std::cout << "New ID: " << s3.get_id() << std::endl; std::cout << std::endl; std::cout << "Input: AAAA" << std::endl; std::cout << "New name: " << s3.get_name() << std::endl; @@ -349,7 +350,7 @@ setters(Student& s3) } } -static void +void addgrd(Student& s3) { s3.add_grade(7.5f); diff --git a/assignment-2.3-operoverloading/bin/operoverloading b/assignment-2.3-operoverloading/bin/operoverloading Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/main.o b/assignment-2.3-operoverloading/obj/main.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/student.o b/assignment-2.3-operoverloading/obj/student.o Binary files differ. diff --git a/assignment-2.3-operoverloading/src/main.cpp b/assignment-2.3-operoverloading/src/main.cpp @@ -1,7 +1,7 @@ #include "student.h" -std::ostream& operator<< (std::ostream& stream, const Student& s); -static void cont(void); +static std::ostream& operator<< (std::ostream& stream, const Student& s); +static void cont(); static void constructor1(const Student& s1); static void ostream_overload(const Student& s1); static void constructor2(const Student& s2); @@ -63,31 +63,31 @@ std::ostream& operator<< (std::ostream& stream, const Student& s) { return stream << - "AM: " << s.get_AM() << std::endl << + "ID: " << s.get_id() << std::endl << "Name: " << s.get_name() << std::endl << "Semester: " << s.get_semester(); } -static void -cont(void) +void +cont() { std::cout << std::endl; std::cout << "Press <ENTER> to continue. . ."; if (std::cin.get()) system("clear || cls"); } -static void +void constructor1(const Student& s1) { - std::cout << "Constructor for s1 (AM, Name)" << std::endl; + std::cout << "Constructor for s1 (ID, Name)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << s1.get_AM() << std::endl; + std::cout << "ID: " << s1.get_id() << std::endl; std::cout << "Name: " << s1.get_name() << std::endl; std::cout << "Semester (default value): " << s1.get_semester() << std::endl; std::cout << "Courses passed (default value): " << s1.get_pcourses() << std::endl; } -static void +void ostream_overload(const Student& s1) { std::cout << "std::ostream overload" << std::endl; @@ -95,29 +95,29 @@ ostream_overload(const Student& s1) std::cout << s1 << std::endl; } -static void +void constructor2(const Student& s2) { - std::cout << "Constructor for s2 (AM, Name, Semester)" << std::endl; + std::cout << "Constructor for s2 (ID, Name, Semester)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << s2.get_AM() << std::endl; + std::cout << "ID: " << s2.get_id() << std::endl; std::cout << "Name: " << s2.get_name() << std::endl; std::cout << "Semester: " << s2.get_semester() << std::endl; std::cout << "Courses passed (default value): " << s2.get_pcourses() << std::endl; } -static void +void copy_constructor(const Student& copystud) { std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << copystud.get_AM() << std::endl; + std::cout << "ID: " << copystud.get_id() << std::endl; std::cout << "Name: " << copystud.get_name() << std::endl; std::cout << "Semester: " << copystud.get_semester() << std::endl; std::cout << "Courses passed (default value): " << copystud.get_pcourses() << std::endl; } -static void +void conditional_overload(const Student& s1, const Student& s2, const Student& copystud) { std::cout << "Conditional operator overloading" << std::endl; @@ -131,24 +131,24 @@ conditional_overload(const Student& s1, const Student& s2, const Student& copyst std::cout << std::endl; } -static void +void equals_overload(const Student& s1, Student& s2) { s2 = s1; std::cout << "= operator overload (s2 = s1)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << s2.get_AM() << std::endl; + std::cout << "ID: " << s2.get_id() << std::endl; std::cout << "Name: " << s2.get_name() << std::endl; std::cout << "Semester: " << s2.get_semester() << std::endl; std::cout << "Courses passed (default value): " << s2.get_pcourses() << std::endl; } -static void +void constructor3(const Student& s3) { - std::cout << "Constructor for s3 (AM, Name, Semester, Courses passed, Grades)" << std::endl; + std::cout << "Constructor for s3 (ID, Name, Semester, Courses passed, Grades)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "AM: " << s3.get_AM() << std::endl; + std::cout << "ID: " << s3.get_id() << std::endl; std::cout << "Name: " << s3.get_name() << std::endl; std::cout << "Semester: " << s3.get_semester() << std::endl; std::cout << "Courses passed: " << s3.get_pcourses() << std::endl; @@ -165,7 +165,7 @@ constructor3(const Student& s3) } } -static void +void detprint(const Student& s3) { std::cout << "Detailed print of s3's grades" << std::endl; @@ -173,10 +173,10 @@ detprint(const Student& s3) s3.detailed_print(); } -static void +void setters(Student& s3) { - s3.set_AM("010101"); + s3.set_id("010101"); s3.set_name("AAAA"); s3.set_semester(100); s3.set_pcourses(2); @@ -187,7 +187,7 @@ setters(Student& s3) std::cout << "Setters example using s3" << std::endl; std::cout << "----------------------------" << std::endl; std::cout << "Input: 010101 " << std::endl; - std::cout << "New AM: " << s3.get_AM() << std::endl; + std::cout << "New ID: " << s3.get_id() << std::endl; std::cout << std::endl; std::cout << "Input: AAAA " << std::endl; std::cout << "New name: " << s3.get_name() << std::endl; @@ -212,7 +212,7 @@ setters(Student& s3) } } -static void +void addgrd(Student& s3) { s3.add_grade(7.5f); @@ -229,7 +229,7 @@ addgrd(Student& s3) } } -static void +void submcourses(Student& s3) { Course *a = new Course(std::string("11231"), std::string("Discrete Math"), 1); @@ -257,7 +257,7 @@ submcourses(Student& s3) } } -static void +void plusequals_overload(Student& s3, Course *c) { s3 += c; @@ -276,7 +276,7 @@ plusequals_overload(Student& s3, Course *c) } } -static void +void getters(const Course& c) { std::cout << "Getters example using Course object" << std::endl; @@ -287,7 +287,7 @@ getters(const Course& c) std::cout << "Course semester: " << c.get_csemester() << std::endl; } -static void +void setters(Course& c) { c.set_code(std::string("14556")); diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -1,22 +1,25 @@ #include "student.h" -Student::Student(const char *AM, const std::string& name) - :AM(convert_AM(AM)), name(name), semester(1), pcourses(0) +Student::Student(const char *id, const std::string& name) + :id(convid(id)), name(name), semester(1), pcourses(0) { nsc = 0; sc = nullptr; } -Student::Student(const char *AM, const std::string& name, unsigned int semester) - :AM(convert_AM(AM)), name(name), semester(semester), pcourses(0) +Student::Student(const char *id, const std::string& name, + const unsigned int semester) + :id(convid(id)), name(name), semester(semester), pcourses(0) { nsc = 0; sc = nullptr; } -Student::Student(const char *AM, const std::string& name, unsigned int semester, - unsigned int pcourses, const float *grades) - :AM(convert_AM(AM)), name(name), semester(semester), pcourses(pcourses), grades(convert_PSG(grades)) +Student::Student(const char *id, const std::string& name, + const unsigned int semester, + const unsigned int pcourses, const float *grades) + :id(convid(id)), name(name), semester(semester), pcourses(pcourses), + grades(convpsg(grades)) { nsc = 0; sc = nullptr; @@ -25,11 +28,11 @@ Student::Student(const char *AM, const std::string& name, unsigned int semester, Student::Student(const Student& s) :name(s.name), semester(s.semester), pcourses(s.pcourses) { - int sl = strlen(s.AM); - this->AM = new char[sl + 1]; - memcpy(AM, s.AM, sizeof(s.AM) + (sl+1)); + int sl = std::strlen(s.id); + this->id = new char[sl + 1]; + std::memcpy(id, s.id, sizeof(s.id) + (sl+1)); this->grades = new float[s.pcourses]; - memcpy(grades, s.grades, sizeof(float) * s.pcourses); + std::memcpy(grades, s.grades, sizeof(float) * s.pcourses); if (s.nsc <= 0) { @@ -39,16 +42,16 @@ Student::Student(const Student& s) else { this->sc = new Course *[s.nsc]; - memcpy(sc, s.sc, sizeof(s.sc) * s.nsc); + std::memcpy(sc, s.sc, sizeof(s.sc) * s.nsc); } } Student::~Student() { - if (this->AM != nullptr) + if (this->id != nullptr) { - delete[] this->AM; - this->AM = nullptr; + delete[] this->id; + this->id = nullptr; } if (this->grades != nullptr) { @@ -68,7 +71,7 @@ Student::operator+= (Course *c) Course **tmp = new Course *[nsc+1]; if (sc != nullptr) { - memcpy(tmp, sc, sizeof(Course *) * nsc); + std::memcpy(tmp, sc, sizeof(Course *) * nsc); delete[] sc; } tmp[nsc] = c; @@ -80,11 +83,11 @@ Student Student::operator= (const Student& s) { if (this == &s) return *this; - this->AM = convert_AM(s.AM); + this->id = convid(s.id); this->name = s.name; this->semester = s.semester; this->pcourses = s.pcourses; - if (s.grades != nullptr) this->grades = convert_PSG(s.grades); + if (s.grades != nullptr) this->grades = convpsg(s.grades); if (s.sc != nullptr) { this->nsc = s.nsc; @@ -100,10 +103,10 @@ Student::get_name() const } void -Student::set_AM(const char *AM) +Student::set_id(const char *id) { - if (this->AM != nullptr) delete[] this->AM; - this->AM = convert_AM(AM); + if (this->id != nullptr) delete[] this->id; + this->id = convid(id); } void @@ -113,25 +116,25 @@ Student::set_name(const std::string& name) } void -Student::set_semester(unsigned int semester) +Student::set_semester(const unsigned int semester) { this->semester = semester; } void -Student::set_pcourses(unsigned int pcourses) +Student::set_pcourses(const unsigned int pcourses) { this->pcourses = pcourses; } void -Student::set_grades(float *grades) +Student::set_grades(const float *grades) { - this->grades = convert_PSG(grades); + this->grades = convpsg(grades); } void -Student::set_num_submitted_courses(unsigned nsc) +Student::set_num_submitted_courses(const unsigned nsc) { this->nsc = nsc; } @@ -143,26 +146,26 @@ Student::set_submitted_courses(Course **sc) { if (this->sc != nullptr) delete[] this->sc; this->sc = new Course *[nsc]; - memcpy(this->sc, sc, sizeof(Course *) * nsc); + std::memcpy(this->sc, sc, sizeof(Course *) * nsc); } } char * -Student::convert_AM(const char *AM) +Student::convid(const char *id) { - int len = strlen(AM); + int len = std::strlen(id); char *tmp = new char[len+1]; - memcpy(tmp, AM, len+1); + std::memcpy(tmp, id, len+1); return tmp; } float * -Student::convert_PSG(const float *grades) +Student::convpsg(const float *grades) { if (pcourses > 0 && grades != nullptr) { float *tmp = new float[pcourses]; - memcpy(tmp, grades, sizeof(float) * pcourses); + std::memcpy(tmp, grades, sizeof(float) * pcourses); if (this->grades != nullptr) delete[] this->grades; return tmp; } @@ -170,12 +173,12 @@ Student::convert_PSG(const float *grades) } void -Student::add_grade(float grade) +Student::add_grade(const float grade) { float *tmp = new float[pcourses+1]; if (grades != nullptr) { - memcpy(tmp, grades, sizeof(float) * pcourses); + std::memcpy(tmp, grades, sizeof(float) * pcourses); delete[] grades; } tmp[pcourses] = grade; @@ -193,12 +196,12 @@ Student::detailed_print() const std::cout << "Course " << i+1 << ": "; std::cout << grades[i] << std::endl; } - std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl; + std::cout << "Average grade: " << std::setprecision(2) << avg() << std::endl; } } float -Student::calc_average() const +Student::avg() const { if (grades != nullptr) { diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -1,30 +1,32 @@ #ifndef STUDENT_H #define STUDENT_H +#include <cstring> #include <iostream> #include <iomanip> #include <string> -#include <string.h> #include "course.h" class Student { private: - char *AM; // AM + char *id; // id std::string name; // Name Course **sc; // Submitted courses unsigned int nsc; // Number of submitted courses unsigned int semester; // Current semester unsigned int pcourses; // Passed courses float *grades; // Grades - int size; + std::size_t size; public: - Student(const char *AM, const std::string& name); - Student(const char *AM, const std::string& name, unsigned int semester); - Student(const char *AM, const std::string& name, unsigned int semester, - unsigned int pcourses, const float *grades); + Student(const char *id, const std::string& name); + Student(const char *id, const std::string& name, + const unsigned int semester); + Student(const char *id, const std::string& name, + const unsigned int semester, + const unsigned int pcourses, const float *grades); Student(const Student& s); ~Student(); @@ -39,28 +41,28 @@ class Student constexpr bool operator>= (const Student& s) const {return this->semester >= s.semester;} const std::string& get_name() const; - constexpr const char *get_AM() const {return this->AM;} + constexpr const char *get_id() const {return this->id;} constexpr unsigned int get_semester() const {return this->semester;} constexpr unsigned int get_pcourses() const {return this->pcourses;} constexpr float *get_grades() const {return (this->pcourses > 0) ? this->grades : nullptr;} constexpr Course **get_submitted_courses() const {return this->sc;} constexpr unsigned int get_num_submitted_courses() const {return this->nsc;} - void set_AM(const char *AM); - void set_name(const std::string& name); - void set_semester(unsigned int semester); - void set_pcourses(unsigned int pcourses); - void set_grades(float *grades); - void set_num_submitted_courses(unsigned int nsc); + void set_id (const char *id); + void set_name (const std::string& name); + void set_semester(const unsigned int semester); + void set_pcourses(const unsigned int pcourses); + void set_grades (const float *grades); + void set_num_submitted_courses(const unsigned int nsc); void set_submitted_courses(Course **sc); - void add_grade(float grade); + void add_grade(const float grade); void detailed_print() const; private: - char *convert_AM(const char *AM); - float *convert_PSG(const float *grades); - float calc_average() const; + char *convid(const char *id); + float *convpsg(const float *grades); + float avg() const; }; #endif /* STUDENT_H */ diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance Binary files differ. diff --git a/assignment-2.4-inheritance/obj/main.o b/assignment-2.4-inheritance/obj/main.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/review.o b/assignment-2.4-inheritance/obj/review.o Binary files differ. diff --git a/assignment-2.4-inheritance/res/appdata_out.csv b/assignment-2.4-inheritance/res/appdata_out.csv @@ -2,5 +2,5 @@ Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions Game,0001,LoL,NiceOS 4.5,Riot,155,MOBA,Yes,N/A Game,0002,CS:GO,GoodOS 3.0,Valve,14,Shooter,Yes,N/A Office,0003,LibreOffice,EpicOS 2.0,FreeSoftware,0,N/A,N/A,.doc|.xls|.odt|.ods|.ppt| -Office,1254,zathura,NiceOS 1.1,Company,0,N/A,N/A,.exe|.bin|.dat| +Office,1254,zathura,NiceOS 1.1,Company,0,N/A,N/A,.tex|.epub|.ms| Game,0005,minesweeper,NiceOS 0.5,Chris,0,Puzzle,Yes,N/A diff --git a/assignment-2.4-inheritance/res/revs_out.csv b/assignment-2.4-inheritance/res/revs_out.csv @@ -1,5 +1,3 @@ AppName,Stars,Username,Comment LoL,3,randomuser1,Nice -LibreOffice,2,user1,Not so good -LibreOffice,4,user2,Good app minesweeper,5,gamer,Good game diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -7,7 +7,7 @@ App::App(const char *serialnum, const std::string& name, const std::string& os, Manufacturer *manf, int price) :serialnum(convsn(serialnum)), name(name), os(os), manf(manf), price(price) { - if (strcmp(serialnum, "") == 0) + if (!std::strcmp(serialnum, "")) throw std::runtime_error("Empty app serial number."); if (name.empty()) throw std::runtime_error("Empty app name."); @@ -34,7 +34,6 @@ App::~App() { if (rev != nullptr) { - // double free bug delete rev; rev = nullptr; } @@ -46,7 +45,7 @@ App::~App() char * App::convsn(const char *serialnum) { - int len = strlen(serialnum); + int len = std::strlen(serialnum); char *tmp = new char[len + 1]; std::copy(serialnum, serialnum + len + 1, tmp); return tmp; diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -130,9 +130,9 @@ AppSystem::removebad(Manufacturer *man) apps.erase(std::remove_if(apps.begin(), apps.end(), [&](App *app) { Manufacturer m = app->get_manf(); - if (strcmp(m.get_name(), man->get_name()) == 0) + if (!std::strcmp(m.get_name(), man->get_name())) delete app; - return strcmp(m.get_name(), man->get_name()) == 0; + return !std::strcmp(m.get_name(), man->get_name()); }), apps.end()); } @@ -142,9 +142,9 @@ AppSystem::removebad(const char *manfname) apps.erase(std::remove_if(apps.begin(), apps.end(), [&](App *app) { Manufacturer m = app->get_manf(); - if (strcmp(m.get_name(), manfname) == 0) + if (!std::strcmp(m.get_name(), manfname)) delete app; - return strcmp(m.get_name(), manfname) == 0; + return !std::strcmp(m.get_name(), manfname); }), apps.end()); } diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -88,9 +88,10 @@ void edit(AppSystem& sys) { sys.newrev("minecurses", new Review(5, "gamer", "Good game")); - sys.newrevs("LibreOffice", + sys.newrevs("Vim", {new Review(2, "user1", "Not so good"), - new Review(4, "user2", "Good app")}); + new Review(4, "user2", "Good app"), + new Review(5, "user3", "Very good :)")}); sys.chserialnum("zathura", "1254"); sys.chname("minecurses", "minesweeper"); sys.chos("Vim", "GoodOS"); @@ -99,7 +100,7 @@ edit(AppSystem& sys) sys.chprice("LoL", 155); sys.chgenre("CS:GO", "Shooter"); sys.chonline("minesweeper", true); - sys.chexts("zathura", {".exe", ".bin", ".dat"}); + sys.chexts("zathura", {".tex", ".epub", ".ms"}); } void @@ -117,7 +118,7 @@ getapps(const AppSystem& sys) for (const auto& fapp : fapps) std::cout << fapp->get_name() << std::endl; std::cout << std::endl; - std::cout << "Games with >5 rating: " << std::endl << std::endl; + std::cout << "Games with >4 rating: " << std::endl << std::endl; for (const auto& ggame : ggames) std::cout << ggame->get_name() << std::endl; } diff --git a/assignment-2.4-inheritance/src/manufacturer.cpp b/assignment-2.4-inheritance/src/manufacturer.cpp @@ -7,9 +7,9 @@ Manufacturer::Manufacturer(const char *serialnum, const char *name, const std::string& email) :serialnum(convstr(serialnum)), name(convstr(name)), email(email) { - if (strcmp(serialnum, "") == 0) + if (!std::strcmp(serialnum, "")) throw std::runtime_error("Empty manufacturer serial number."); - if (strcmp(name, "") == 0) + if (!std::strcmp(name, "")) throw std::runtime_error("Empty manufacturer name."); if (email.empty() || email.find("@") == std::string::npos) { @@ -30,7 +30,7 @@ Manufacturer::~Manufacturer() char * Manufacturer::convstr(const char *str) { - int len = strlen(str); + int len = std::strlen(str); char *tmp = new char[len + 1]; std::copy(str, str + len + 1, tmp); return tmp; diff --git a/assignment-2.4-inheritance/src/review.cpp b/assignment-2.4-inheritance/src/review.cpp @@ -7,7 +7,7 @@ Review::Review(int stars, const std::string& username, const std::string& commen :stars(stars), username(username), comment(comment) { if (stars < 0 || stars > 5) - throw std::runtime_error("Wrong star value."); + throw std::runtime_error("Wrong star value. Must be in [0, 5]"); } Review::Review(const Review& r) diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/datahandler.o b/assignment-2.5-spreadsheets/obj/datahandler.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/xstring.o b/assignment-2.5-spreadsheets/obj/xstring.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/res/report.csv b/assignment-2.5-spreadsheets/res/report.csv @@ -1,4 +1,4 @@ -AM;Last name;First name;New course code;New course name;Old course code;Old course name;Grade +ID;Last name;First name;New course code;New course name;Old course code;Old course name;Grade SR000001;ΕΠΩΝΥΜΟ 0001;ΟΝΟΜΑ 0001;ICE1-1003;ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ;ICE1-1003;ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΕΠΙΣΤΗΜΗ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ;0.4 SR000001;ΕΠΩΝΥΜΟ 0001;ΟΝΟΜΑ 0001;ICE1-2006;ΘΕΩΡΙΑ ΚΥΚΛΩΜΑΤΩΝ;ICE1-2006;ΘΕΩΡΙΑ ΚΥΚΛΩΜΑΤΩΝ;0.3 SR000001;ΕΠΩΝΥΜΟ 0001;ΟΝΟΜΑ 0001;ICE1-4003;ΛΕΙΤΟΥΡΓΙΚΑ ΣΥΣΤΗΜΑΤΑ ΙΙ;ICE1-4003;ΛΕΙΤΟΥΡΓΙΚΑ ΣΥΣΤΗΜΑΤΑ ΙΙ;0.3 diff --git a/assignment-2.5-spreadsheets/src/datahandler.cpp b/assignment-2.5-spreadsheets/src/datahandler.cpp @@ -30,26 +30,26 @@ DataHandler::store_data() lab::xstring skip; lab::getline(f, skip); - lab::xstring AM, code, grade; - lab::getline(f, AM, ';'); + lab::xstring id, code, grade; + lab::getline(f, id, ';'); lab::getline(f, code, ';'); lab::getline(f, grade); - std::map<lab::xstring, Student *>::const_iterator its = studs.find(AM); + std::map<lab::xstring, Student *>::const_iterator its = studs.find(id); std::map<lab::xstring, Course *>::const_iterator itc = courses.find(code); if (its != studs.end() && itc != courses.end()) grds.insert(std::make_pair(courses[code], std::atof(grade.cstr()))); while (f.good()) { - lab::xstring currAM = AM; - while (currAM == AM) + lab::xstring currid = id; + while (currid == id) { - lab::getline(f, AM, ';'); + lab::getline(f, id, ';'); lab::getline(f, code, ';'); lab::getline(f, grade); if (f.eof()) break; - if (!analyze(currAM, AM, code, std::atof(grade.cstr()))) break; + if (!analyze(currid, id, code, std::atof(grade.cstr()))) break; } } } @@ -65,18 +65,18 @@ DataHandler::store_data() bool DataHandler::analyze( - const lab::xstring& currAM, - lab::xstring& AM, + const lab::xstring& currid, + lab::xstring& id, lab::xstring& code, float grade) { - std::map<lab::xstring, Student *>::const_iterator its = studs.find(AM); + std::map<lab::xstring, Student *>::const_iterator its = studs.find(id); std::map<lab::xstring, Course *>::const_iterator itc = courses.find(code); if (its != studs.end() && itc != courses.end()) { - if (currAM != AM) + if (currid != id) { - data.insert(std::make_pair(studs[currAM], grds)); + data.insert(std::make_pair(studs[currid], grds)); grds.clear(); grds.insert(std::make_pair(courses[code], grade)); return false; @@ -85,10 +85,10 @@ DataHandler::analyze( } else if (its == studs.end()) { - if (std::find(errs.begin(), errs.end(), AM) == errs.end()) + if (std::find(errs.begin(), errs.end(), id) == errs.end()) { - errs.push_back(AM); - errlog.write(ErrLog::ErrType::STUDENT_MISSING, AM); + errs.push_back(id); + errlog.write(ErrLog::ErrType::STUDENT_MISSING, id); errcount++; } } @@ -104,15 +104,15 @@ DataHandler::analyze( if (its != studs.end() && itc != courses.end()) { - miss(AM, code, grade); - diffr(AM, code, grade); + miss(id, code, grade); + diffr(id, code, grade); } return true; } void -DataHandler::miss(lab::xstring AM, lab::xstring code, float grade) +DataHandler::miss(lab::xstring id, lab::xstring code, float grade) { if (code.front() == 'P') { @@ -125,9 +125,9 @@ DataHandler::miss(lab::xstring AM, lab::xstring code, float grade) found = true; if (!found) { - missing.push_back(AM + ";" + - studs[AM]->get_lname() + ";" + - studs[AM]->get_fname() + ";" + + missing.push_back(id + ";" + + studs[id]->get_lname() + ";" + + studs[id]->get_fname() + ";" + courses[eqvs[code]]->get_code() + ";" + courses[eqvs[code]]->get_name() + ";" + eqvs[code] + ";" + @@ -140,13 +140,13 @@ DataHandler::miss(lab::xstring AM, lab::xstring code, float grade) } void -DataHandler::diffr(lab::xstring AM, lab::xstring code, float grade) +DataHandler::diffr(lab::xstring id, lab::xstring code, float grade) { std::map<Course *, float>::const_iterator it = grds.find(courses[code]); if (it != grds.end() && it->second != grade) { errlog.write(ErrLog::ErrType::DIFFERENT_GRADES, - lab::xstring(AM + " in " + code + ": " + + lab::xstring(id + " in " + code + ": " + lab::to_xstr<float>("%.1f", it->second) + " | " + lab::to_xstr<float>("%.1f", grade))); errcount++; @@ -166,7 +166,7 @@ DataHandler::make_report() const if (f.is_open()) { std::cout << "Making report." << std::endl; - f << "AM;Last name;First name;New course code;New course name;" << + f << "ID;Last name;First name;New course code;New course name;" << "Old course code;Old course name;Grade" << std::endl; for (const auto& m : missing) f << m << std::endl; f.close(); @@ -202,7 +202,7 @@ DataHandler::summary() const bool DataHandler::valid_path(const char *fpath) const { - return (strstr(fpath, ".csv") != nullptr); + return (std::strstr(fpath, ".csv") != nullptr); } const lab::xstring diff --git a/assignment-2.5-spreadsheets/src/datahandler.h b/assignment-2.5-spreadsheets/src/datahandler.h @@ -40,12 +40,12 @@ class DataHandler private: bool analyze( - const lab::xstring& currAM, - lab::xstring& AM, + const lab::xstring& currid, + lab::xstring& id, lab::xstring& code, float grade); - void miss(lab::xstring AM, lab::xstring code, float grade); - void diffr(lab::xstring AM, lab::xstring code, float grade); + void miss(lab::xstring id, lab::xstring code, float grade); + void diffr(lab::xstring id, lab::xstring code, float grade); bool valid_path(const char *fpath) const; const lab::xstring err_csv (const char *fpath) const; const lab::xstring err_read (const char *fpath) const; @@ -80,12 +80,12 @@ DataHandler::import_data(const char *fpath) } else if constexpr (std::is_same<T, Student>::value) { - lab::xstring AM, lname, fname; - lab::getline(f, AM, ';'); + lab::xstring id, lname, fname; + lab::getline(f, id, ';'); lab::getline(f, lname, ';'); lab::getline(f, fname); if (f.eof()) break; - studs.insert(std::make_pair(AM, new Student(AM, lname, fname))); + studs.insert(std::make_pair(id, new Student(id, lname, fname))); } else if constexpr (std::is_same<T, equivalencies>::value) { diff --git a/assignment-2.5-spreadsheets/src/errlog.h b/assignment-2.5-spreadsheets/src/errlog.h @@ -8,6 +8,7 @@ class ErrLog { private: const char *fpath = "res/errlog.csv"; + void fclear() const; public: enum class ErrType { @@ -16,13 +17,10 @@ class ErrLog DIFFERENT_GRADES }; - public: ErrLog(); - void write(ErrType type, const lab::xstring& s) const; private: - void fclear() const; }; #endif /* ERRLOG_H */ diff --git a/assignment-2.5-spreadsheets/src/student.cpp b/assignment-2.5-spreadsheets/src/student.cpp @@ -1,6 +1,6 @@ #include "student.h" -Student::Student(const lab::xstring& AM, +Student::Student(const lab::xstring& id, const lab::xstring& lname, const lab::xstring& fname) - :AM(AM), lname(lname), fname(fname) {} + :id(id), lname(lname), fname(fname) {} diff --git a/assignment-2.5-spreadsheets/src/student.h b/assignment-2.5-spreadsheets/src/student.h @@ -6,16 +6,16 @@ class Student { private: - lab::xstring AM; + lab::xstring id; lab::xstring lname; lab::xstring fname; public: - Student(const lab::xstring& AM, + Student(const lab::xstring& id, const lab::xstring& lname, const lab::xstring& fname); - constexpr const lab::xstring& get_AM() const {return AM;} + constexpr const lab::xstring& get_id() const {return id;} constexpr const lab::xstring& get_lname() const {return lname;} constexpr const lab::xstring& get_fname() const {return fname;} }; diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -9,7 +9,7 @@ xstring::xstring() } xstring::xstring(const char *s) - :str(conv(s)), len(strlen(s)) {} + :str(conv(s)), len(std::strlen(s)) {} xstring::xstring(const xstring& s) { @@ -113,7 +113,7 @@ xstring::append(const xstring& s) if (!s.empty()) { resize(s.len); - strcat(str, s.str); + std::strcat(str, s.str); } len = length(); return *this; @@ -124,8 +124,8 @@ xstring::append(const char *s) { if (!strempty(s)) { - resize(strlen(s)); - strcat(str, s); + resize(std::strlen(s)); + std::strcat(str, s); } len = length(); return *this; @@ -168,10 +168,10 @@ xstring::insert(const char *s, std::size_t i) char *tmp2 = new char[len - i + 1]; std::copy(str, str + i + 1, tmp1); std::copy(str + i, str + len + 1, tmp2); - resize(strlen(s)); + resize(std::strlen(s)); std::copy(tmp1, tmp1 + i + 1, str); - std::copy(s, s + strlen(s) + 1, str + i); - std::copy(tmp2, tmp2 + len - i + 1, str + strlen(s) + i); + std::copy(s, s + std::strlen(s) + 1, str + i); + std::copy(tmp2, tmp2 + len - i + 1, str + std::strlen(s) + i); len = length(); str[len] = '\0'; delete[] tmp1; @@ -210,32 +210,25 @@ xstring::replace(std::size_t i, char c) if (i < len) str[i] = c; } -bool -xstring::find(const xstring& s) const +void +xstring::clear() { - return (strstr(this->cstr(), s.cstr()) != nullptr); + if (!this->empty()) delete[] str; + str = new char[1]; + str[0] = '\0'; + len = 0; } bool -xstring::find(const char *s) const -{ - return (strstr(this->cstr(), s) != nullptr); -} - -char& -xstring::back() const +xstring::find(const xstring& s) const { - if (!this->empty()) return str[len-1]; - else return str[0]; + return (std::strstr(this->cstr(), s.cstr()) != nullptr); } -void -xstring::clear() +bool +xstring::find(const char *s) const { - if (!this->empty()) delete[] str; - str = new char[1]; - str[0] = '\0'; - len = 0; + return (std::strstr(this->cstr(), s) != nullptr); } void @@ -256,7 +249,7 @@ xstring::resize(std::size_t n) char * xstring::conv(const char *s) const { - std::size_t l = strlen(s); + std::size_t l = std::strlen(s); char *tmp = new char[l + 1]; std::copy(s, s + l+1, tmp); tmp[l] = '\0'; diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -28,29 +28,29 @@ class xstring xstring& operator+= (const char *s); xstring& operator+= (char c); constexpr bool operator== (const xstring& s) const - {return strcmp(this->str, s.str) == 0;} + {return std::strcmp(this->str, s.str) == 0;} constexpr bool operator== (const char *s) const - {return strcmp(this->str, s) == 0;} + {return std::strcmp(this->str, s) == 0;} constexpr bool operator!= (const xstring& s) const - {return strcmp(this->str, s.str) != 0;} + {return std::strcmp(this->str, s.str) != 0;} constexpr bool operator!= (const char *s) const - {return strcmp(this->str, s) != 0;} + {return std::strcmp(this->str, s) != 0;} constexpr bool operator< (const xstring& s) const - {return strcmp(this->str, s.str) < 0;} + {return std::strcmp(this->str, s.str) < 0;} constexpr bool operator< (const char *s) const - {return strcmp(this->str, s) < 0;} + {return std::strcmp(this->str, s) < 0;} constexpr bool operator<= (const xstring& s) const - {return strcmp(this->str, s.str) <= 0;} + {return std::strcmp(this->str, s.str) <= 0;} constexpr bool operator<= (const char *s) const - {return strcmp(this->str, s) <= 0;} + {return std::strcmp(this->str, s) <= 0;} constexpr bool operator> (const xstring& s) const - {return strcmp(this->str, s.str) > 0;} + {return std::strcmp(this->str, s.str) > 0;} constexpr bool operator> (const char *s) const - {return strcmp(this->str, s) > 0;} + {return std::strcmp(this->str, s) > 0;} constexpr bool operator>= (const xstring& s) const - {return strcmp(this->str, s.str) >= 0;} + {return std::strcmp(this->str, s.str) >= 0;} constexpr bool operator>= (const char *s) const - {return strcmp(this->str, s) >= 0;} + {return std::strcmp(this->str, s) >= 0;} char& operator[] (std::size_t i) const; friend std::ostream& operator<< (std::ostream& stream, const xstring& s); friend std::istream& operator>> (std::istream& stream, const xstring& s); @@ -60,23 +60,22 @@ class xstring xstring& append(char c); xstring& insert(const xstring& s, std::size_t i); xstring& insert(const char *s, std::size_t i); - void push_back(char c); void pop_back(); void replace(std::size_t i, char c); + void clear(); bool find(const xstring& s) const; - bool find(const char *s) const; - constexpr char *cstr() const {return str;} + bool find(const char *s) const; + constexpr char *cstr() const {return str;} constexpr char& front() const {return str[0];} - char& back() const; - constexpr std::size_t length() const {return strlen(str);} - constexpr bool empty() const {return len == 0;} - void clear(); + constexpr char& back() const {return (!this->empty()) ? str[len-1] : str[0];}; + constexpr bool empty() const {return len == 0;} + constexpr std::size_t length() const {return std::strlen(str);} private: void resize(std::size_t n); char *conv(const char *s) const; - constexpr bool strempty(const char *s) const {return strlen(s) == 0;} + constexpr bool strempty(const char *s) const {return std::strlen(s) == 0;} }; std::istream& getline(std::istream& stream, xstring& s, char delim = '\n'); @@ -88,7 +87,7 @@ template<typename T> xstring to_xstr(T val) { char buf[25]; - snprintf(buf, sizeof(buf), getformat<T>(), val); + std::snprintf(buf, sizeof(buf), getformat<T>(), val); return xstring(buf); } @@ -96,7 +95,7 @@ template<typename T> xstring to_xstr(const char *fs, T val) { char buf[25]; - snprintf(buf, sizeof(buf), fs, val); + std::snprintf(buf, sizeof(buf), fs, val); return xstring(buf); }