uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 251245a0adcdbbda78ce8cb8b2f6a2b55d0accc6
parent e68707766bf0e7cd870629e304deff389d54dee7
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon, 25 May 2020 14:14:52 +0300

tabs -> spaces

Diffstat:
Massignment-2.2-classes/classes.cpp | 448++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.3-operoverloading/src/course.cpp | 14+++++++-------
Massignment-2.3-operoverloading/src/course.h | 28++++++++++++++--------------
Massignment-2.3-operoverloading/src/main.cpp | 386++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.3-operoverloading/src/student.cpp | 232++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.3-operoverloading/src/student.h | 92++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.4-inheritance/src/app.cpp | 72++++++++++++++++++++++++++++++++++++------------------------------------
Massignment-2.4-inheritance/src/app.h | 72++++++++++++++++++++++++++++++++++++------------------------------------
Massignment-2.4-inheritance/src/appsystem.cpp | 182++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.4-inheritance/src/appsystem.h | 476++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.4-inheritance/src/game.cpp | 78+++++++++++++++++++++++++++++++++++++++---------------------------------------
Massignment-2.4-inheritance/src/game.h | 28++++++++++++++--------------
Massignment-2.4-inheritance/src/main.cpp | 150++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.4-inheritance/src/manufacturer.cpp | 46+++++++++++++++++++++++-----------------------
Massignment-2.4-inheritance/src/manufacturer.h | 36++++++++++++++++++------------------
Massignment-2.4-inheritance/src/office.cpp | 84++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.4-inheritance/src/office.h | 24++++++++++++------------
Massignment-2.4-inheritance/src/review.cpp | 10+++++-----
Massignment-2.4-inheritance/src/review.h | 34+++++++++++++++++-----------------
Massignment-2.5-spreadsheets/src/course.h | 16++++++++--------
Massignment-2.5-spreadsheets/src/datahandler.cpp | 340++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.5-spreadsheets/src/datahandler.h | 188++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.5-spreadsheets/src/errlog.cpp | 38+++++++++++++++++++-------------------
Massignment-2.5-spreadsheets/src/errlog.h | 24++++++++++++------------
Massignment-2.5-spreadsheets/src/main.cpp | 16++++++++--------
Massignment-2.5-spreadsheets/src/student.h | 24++++++++++++------------
Massignment-2.5-spreadsheets/src/xstring.cpp | 258++++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.5-spreadsheets/src/xstring.h | 168++++++++++++++++++++++++++++++++++++++++----------------------------------------
28 files changed, 1782 insertions(+), 1782 deletions(-)

diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp @@ -6,181 +6,181 @@ class Student { - private: - char *id; - std::string name; - unsigned int semester; - unsigned int pcourses; - float *grades; - std::size_t size; - - public: - 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_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_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(const float grade); - void detailed_print() const; - - private: - char *convert_id (const char *id); - float *convert_PSG(const float *grades); - float calc_average() const; + private: + char *id; + std::string name; + unsigned int semester; + unsigned int pcourses; + float *grades; + std::size_t size; + + public: + 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_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_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(const float grade); + void detailed_print() const; + + private: + char *convert_id (const char *id); + float *convert_PSG(const float *grades); + float calc_average() const; }; Student::Student(const char *id, const std::string& name) - :id(convert_id(id)), name(name), semester(1), pcourses(0), grades(nullptr) {} + :id(convert_id(id)), name(name), semester(1), 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) {} + const unsigned int semester) + :id(convert_id(id)), name(name), semester(semester), pcourses(0), grades(nullptr) {} 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)) {} + 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) + :name(s.name), semester(s.semester), pcourses(s.pcourses) { - 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]; - std::memcpy(grades, s.grades, sizeof(float) * s.pcourses); + 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]; + std::memcpy(grades, s.grades, sizeof(float) * s.pcourses); } Student::~Student() { - if (this->id != nullptr) - { - delete[] this->id; - this->id = nullptr; - } - if (this->id != nullptr) - { - delete[] this->grades; - this->grades = nullptr; - } + if (this->id != nullptr) + { + delete[] this->id; + this->id = nullptr; + } + if (this->id != nullptr) + { + delete[] this->grades; + this->grades = nullptr; + } } const std::string& Student::get_name() const { - return this->name; + return this->name; } void Student::set_id(const char *id) { - if (this->id != nullptr) delete[] this->id; - this->id = convert_id(id); + if (this->id != nullptr) delete[] this->id; + this->id = convert_id(id); } void Student::set_name(const std::string& name) { - this->name = name; + this->name = name; } void Student::set_semester(const unsigned int semester) { - this->semester = semester; + this->semester = semester; } void Student::set_pcourses(const unsigned int pcourses) { - this->pcourses = pcourses; + this->pcourses = pcourses; } void Student::set_grades(const float *grades) { - this->grades = convert_PSG(grades); + this->grades = convert_PSG(grades); } char * Student::convert_id(const char *id) { - int len = std::strlen(id); - char *tmp = new char[len+1]; - std::memcpy(tmp, id, len+1); - return tmp; + int len = std::strlen(id); + char *tmp = new char[len+1]; + std::memcpy(tmp, id, len+1); + return tmp; } float * Student::convert_PSG(const float *grades) { - if (pcourses > 0 && grades != nullptr) - { - float *tmp = new float[pcourses]; - std::memcpy(tmp, grades, sizeof(float) * pcourses); - if (this->grades != nullptr) delete[] this->grades; - return tmp; - } - else return nullptr; + if (pcourses > 0 && grades != nullptr) + { + float *tmp = new float[pcourses]; + std::memcpy(tmp, grades, sizeof(float) * pcourses); + if (this->grades != nullptr) delete[] this->grades; + return tmp; + } + else return nullptr; } void Student::add_grade(const float grade) { - float *tmp = new float[pcourses+1]; - if (grades != nullptr) - { - std::memcpy(tmp, grades, sizeof(float) * pcourses); - delete[] grades; - } - tmp[pcourses] = grade; - grades = tmp; - pcourses++; + float *tmp = new float[pcourses+1]; + if (grades != nullptr) + { + std::memcpy(tmp, grades, sizeof(float) * pcourses); + delete[] grades; + } + tmp[pcourses] = grade; + grades = tmp; + pcourses++; } void Student::detailed_print() const { - if (grades != nullptr) - { - for (unsigned int i = 0; i < pcourses; i++) - { - std::cout << "Subject " << i+1 << ": "; - std::cout << grades[i] << std::endl; - } - std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl; - } + if (grades != nullptr) + { + for (unsigned int i = 0; i < pcourses; i++) + { + std::cout << "Subject " << i+1 << ": "; + std::cout << grades[i] << std::endl; + } + std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl; + } } float Student::calc_average() const { - if (grades != nullptr) - { - float sum = 0; - for (unsigned int i = 0; i < pcourses; i++) - sum += grades[i]; - float average = sum / pcourses; - return average; - } - else return 0.0f; + if (grades != nullptr) + { + float sum = 0; + for (unsigned int i = 0; i < pcourses; i++) + sum += grades[i]; + float average = sum / pcourses; + return average; + } + else return 0.0f; } static std::ostream& operator<< (std::ostream& stream, const Student& s); @@ -197,172 +197,172 @@ static void addgrd(Student& s3); int main(int argc, char **argv) { - 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", std::string("Name Surnamington"), 2); - constructor2(*s2); cont(); - - Student *copystud = new Student(*s2); - copy_constructor(*copystud); cont(); - delete copystud; - delete s2; - - float *grd = new float[4]{9.4f, 8.4f, 5.5f, 6.3f}; - Student *s3 = new Student("72345678", std::string("Name Surnaming"), 2, 4, grd); - delete[] grd; - constructor3(*s3); cont(); - detprint(*s3); cont(); - setters(*s3); - addgrd(*s3); cont(); - delete s3; - - return 0; + 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", std::string("Name Surnamington"), 2); + constructor2(*s2); cont(); + + Student *copystud = new Student(*s2); + copy_constructor(*copystud); cont(); + delete copystud; + delete s2; + + float *grd = new float[4]{9.4f, 8.4f, 5.5f, 6.3f}; + Student *s3 = new Student("72345678", std::string("Name Surnaming"), 2, 4, grd); + delete[] grd; + constructor3(*s3); cont(); + detprint(*s3); cont(); + setters(*s3); + addgrd(*s3); cont(); + delete s3; + + return 0; } std::ostream& operator<< (std::ostream& stream, const Student& s) { - return stream << - "ID: " << s.get_id() << std::endl << - "Name: " << s.get_name() << std::endl << - "Semester: " << s.get_semester() << std::endl; + return stream << + "ID: " << s.get_id() << std::endl << + "Name: " << s.get_name() << std::endl << + "Semester: " << s.get_semester() << std::endl; } void cont() { - std::cout << std::endl; - std::cout << "Press <ENTER> to continue. . ."; - if (std::cin.get()) system("clear || cls"); + std::cout << std::endl; + std::cout << "Press <ENTER> to continue. . ."; + if (std::cin.get()) system("clear || cls"); } void constructor1(const Student& s1) { - std::cout << "Constructor for s1 (ID, Name)" << std::endl; - std::cout << "----------------------------" << 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; + std::cout << "Constructor for s1 (ID, Name)" << std::endl; + std::cout << "----------------------------" << 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; } void ostream_overload(const Student& s1) { - std::cout << "std::ostream overload" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << s1 << std::endl; + std::cout << "std::ostream overload" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << s1 << std::endl; } void constructor2(const Student& s2) { - std::cout << "Constructor for s2 (ID, Name, Semester)" << std::endl; - std::cout << "----------------------------" << 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; + std::cout << "Constructor for s2 (ID, Name, Semester)" << std::endl; + std::cout << "----------------------------" << 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; } 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 << "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; + std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl; + std::cout << "----------------------------" << 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; } void constructor3(const Student& s3) { - std::cout << "Constructor for s3 (ID, Name, Semester, Courses passed, Grades)" << std::endl; - std::cout << "----------------------------" << 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; - - float *gr = s3.get_grades(); - if (gr != nullptr) - { - std::cout << "Grades: "; - for (unsigned int i = 0; i < s3.get_pcourses(); i++) - { - if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; - } - } + std::cout << "Constructor for s3 (ID, Name, Semester, Courses passed, Grades)" << std::endl; + std::cout << "----------------------------" << 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; + + float *gr = s3.get_grades(); + if (gr != nullptr) + { + std::cout << "Grades: "; + for (unsigned int i = 0; i < s3.get_pcourses(); i++) + { + if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl; + } + } } void detprint (const Student& s3) { - std::cout << "Detailed print of s3's grades" << std::endl; - std::cout << "----------------------------" << std::endl; - s3.detailed_print(); + std::cout << "Detailed print of s3's grades" << std::endl; + std::cout << "----------------------------" << std::endl; + s3.detailed_print(); } void setters(Student& s3) { - s3.set_id("010101"); - s3.set_name("AAAA"); - s3.set_semester(100); - s3.set_pcourses(2); - float *gg = new float[2]{0.1f, 2.2f}; - s3.set_grades(gg); - delete[] gg; - - std::cout << "Setters example using s3" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Input: 010101" << 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; - std::cout << std::endl; - std::cout << "Input: 100" << std::endl; - std::cout << "New semester: " << s3.get_semester() << std::endl; - std::cout << std::endl; - std::cout << "Input: 2" << std::endl; - std::cout << "New courses passed: " << s3.get_pcourses() << std::endl; - std::cout << std::endl; - std::cout << "Input: 0.1, 2.2" << std::endl; - std::cout << "New grades: "; - - float *gr = s3.get_grades(); - if (gr != nullptr) - { - for (unsigned int i = 0; i < s3.get_pcourses(); i++) - { - if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl << std::endl; - } - } + s3.set_id("010101"); + s3.set_name("AAAA"); + s3.set_semester(100); + s3.set_pcourses(2); + float *gg = new float[2]{0.1f, 2.2f}; + s3.set_grades(gg); + delete[] gg; + + std::cout << "Setters example using s3" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: 010101" << 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; + std::cout << std::endl; + std::cout << "Input: 100" << std::endl; + std::cout << "New semester: " << s3.get_semester() << std::endl; + std::cout << std::endl; + std::cout << "Input: 2" << std::endl; + std::cout << "New courses passed: " << s3.get_pcourses() << std::endl; + std::cout << std::endl; + std::cout << "Input: 0.1, 2.2" << std::endl; + std::cout << "New grades: "; + + float *gr = s3.get_grades(); + if (gr != nullptr) + { + for (unsigned int i = 0; i < s3.get_pcourses(); i++) + { + if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl << std::endl; + } + } } void addgrd(Student& s3) { - s3.add_grade(7.5f); - float *gr = s3.get_grades(); - if (gr != nullptr) - { - std::cout << "Input: 7.5" << std::endl; - std::cout << "Updated grades: "; - for (unsigned int i = 0; i < s3.get_pcourses(); i++) - { - if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; - } - } + s3.add_grade(7.5f); + float *gr = s3.get_grades(); + if (gr != nullptr) + { + std::cout << "Input: 7.5" << std::endl; + std::cout << "Updated grades: "; + for (unsigned int i = 0; i < s3.get_pcourses(); i++) + { + if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl; + } + } } diff --git a/assignment-2.3-operoverloading/src/course.cpp b/assignment-2.3-operoverloading/src/course.cpp @@ -4,37 +4,37 @@ Course::Course() {} Course::Course(const std::string& code, const std::string& cname, unsigned int csemester) - :code(code), cname(cname), csemester(csemester) {} + :code(code), cname(cname), csemester(csemester) {} Course::Course(const Course& c) - :code(c.code), cname(c.cname), csemester(c.csemester) {} + :code(c.code), cname(c.cname), csemester(c.csemester) {} const std::string& Course::get_code() const { - return this->code; + return this->code; } const std::string& Course::get_cname() const { - return this->cname; + return this->cname; } void Course::set_code(const std::string& code) { - this->code = code; + this->code = code; } void Course::set_cname(const std::string& cname) { - this->cname = cname; + this->cname = cname; } void Course::set_csemester(unsigned int csemester) { - this->csemester = csemester; + this->csemester = csemester; } diff --git a/assignment-2.3-operoverloading/src/course.h b/assignment-2.3-operoverloading/src/course.h @@ -6,23 +6,23 @@ class Course { - private: - std::string code; // Course code - std::string cname; // Course name - unsigned int csemester; // Corresponding semester of course + private: + std::string code; // Course code + std::string cname; // Course name + unsigned int csemester; // Corresponding semester of course - public: - Course(); - Course(const std::string& code, const std::string& cname, unsigned int csemester); - Course(const Course& s); + public: + Course(); + Course(const std::string& code, const std::string& cname, unsigned int csemester); + Course(const Course& s); - const std::string& get_code() const; - const std::string& get_cname() const; - constexpr unsigned int get_csemester() const {return csemester;} + const std::string& get_code() const; + const std::string& get_cname() const; + constexpr unsigned int get_csemester() const {return csemester;} - void set_code(const std::string& code); - void set_cname(const std::string& cname); - void set_csemester(unsigned int csemester); + void set_code(const std::string& code); + void set_cname(const std::string& cname); + void set_csemester(unsigned int csemester); }; #endif /* COURSE_H */ diff --git a/assignment-2.3-operoverloading/src/main.cpp b/assignment-2.3-operoverloading/src/main.cpp @@ -20,288 +20,288 @@ static void setters(Course& c); int main(int argc, char **argv) { - Student *s1 = new Student("12345678", std::string("Name Surname")); - system("clear || cls"); - constructor1(*s1); cont(); - ostream_overload(*s1); cont(); - - Student *s2 = new Student("92345678", std::string("Name Surnamington"), 2); - constructor2(*s2); cont(); + Student *s1 = new Student("12345678", std::string("Name Surname")); + system("clear || cls"); + constructor1(*s1); cont(); + ostream_overload(*s1); cont(); + + Student *s2 = new Student("92345678", std::string("Name Surnamington"), 2); + constructor2(*s2); cont(); - Student *copystud = new Student(*s2); - copy_constructor(*copystud); cont(); - conditional_overload(*s1, *s2, *copystud); cont(); - equals_overload(*s1, *s2); cont(); - delete s1; - delete copystud; - delete s2; + Student *copystud = new Student(*s2); + copy_constructor(*copystud); cont(); + conditional_overload(*s1, *s2, *copystud); cont(); + equals_overload(*s1, *s2); cont(); + delete s1; + delete copystud; + delete s2; - float *grd = new float[4]{9.4f, 8.4f, 5.5f, 6.3f}; - Student *s3 = new Student("72345678", std::string("Name Surnaming"), 2, 4, grd); - delete[] grd; - constructor3(*s3); cont(); - detprint(*s3); cont(); - setters(*s3); - addgrd(*s3); cont(); - submcourses(*s3); cont(); - Student *s4 = new Student(*s3); + float *grd = new float[4]{9.4f, 8.4f, 5.5f, 6.3f}; + Student *s3 = new Student("72345678", std::string("Name Surnaming"), 2, 4, grd); + delete[] grd; + constructor3(*s3); cont(); + detprint(*s3); cont(); + setters(*s3); + addgrd(*s3); cont(); + submcourses(*s3); cont(); + Student *s4 = new Student(*s3); - Course *oop = new Course(std::string("356431"), std::string("OOP"), 2); - plusequals_overload(*s3, oop); cont(); + Course *oop = new Course(std::string("356431"), std::string("OOP"), 2); + plusequals_overload(*s3, oop); cont(); - getters(*oop); cont(); - setters(*oop); + getters(*oop); cont(); + setters(*oop); - delete s4; - delete s3; - delete oop; + delete s4; + delete s3; + delete oop; - return 0; + return 0; } std::ostream& operator<< (std::ostream& stream, const Student& s) { - return stream << - "ID: " << s.get_id() << std::endl << - "Name: " << s.get_name() << std::endl << - "Semester: " << s.get_semester(); + return stream << + "ID: " << s.get_id() << std::endl << + "Name: " << s.get_name() << std::endl << + "Semester: " << s.get_semester(); } void cont() { - std::cout << std::endl; - std::cout << "Press <ENTER> to continue. . ."; - if (std::cin.get()) system("clear || cls"); + std::cout << std::endl; + std::cout << "Press <ENTER> to continue. . ."; + if (std::cin.get()) system("clear || cls"); } void constructor1(const Student& s1) { - std::cout << "Constructor for s1 (ID, Name)" << std::endl; - std::cout << "----------------------------" << 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; + std::cout << "Constructor for s1 (ID, Name)" << std::endl; + std::cout << "----------------------------" << 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; } void ostream_overload(const Student& s1) { - std::cout << "std::ostream overload" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << s1 << std::endl; + std::cout << "std::ostream overload" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << s1 << std::endl; } void constructor2(const Student& s2) { - std::cout << "Constructor for s2 (ID, Name, Semester)" << std::endl; - std::cout << "----------------------------" << 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; + std::cout << "Constructor for s2 (ID, Name, Semester)" << std::endl; + std::cout << "----------------------------" << 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; } 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 << "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; + std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl; + std::cout << "----------------------------" << 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; } void conditional_overload(const Student& s1, const Student& s2, const Student& copystud) { - std::cout << "Conditional operator overloading" << std::endl; - std::cout << "----------------------------" << std::endl; - if (s2 == copystud) std::cout << "s2 == copystud" << std::endl; - if (s1 != s2) std::cout << "s1 != s2" << std::endl; - if (s1 < s2) std::cout << "s1 < s2" << std::endl; - if (s1 <= s2) std::cout << "s1 <= s2" << std::endl; - if (s2 > s1) std::cout << "s2 > s1" << std::endl; - if (s2 >= s1) std::cout << "s2 >= s1" << std::endl; - std::cout << std::endl; + std::cout << "Conditional operator overloading" << std::endl; + std::cout << "----------------------------" << std::endl; + if (s2 == copystud) std::cout << "s2 == copystud" << std::endl; + if (s1 != s2) std::cout << "s1 != s2" << std::endl; + if (s1 < s2) std::cout << "s1 < s2" << std::endl; + if (s1 <= s2) std::cout << "s1 <= s2" << std::endl; + if (s2 > s1) std::cout << "s2 > s1" << std::endl; + if (s2 >= s1) std::cout << "s2 >= s1" << std::endl; + std::cout << std::endl; } void equals_overload(const Student& s1, Student& s2) { - s2 = s1; - std::cout << "= operator overload (s2 = s1)" << std::endl; - std::cout << "----------------------------" << 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; + s2 = s1; + std::cout << "= operator overload (s2 = s1)" << std::endl; + std::cout << "----------------------------" << 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; } void constructor3(const Student& s3) { - std::cout << "Constructor for s3 (ID, Name, Semester, Courses passed, Grades)" << std::endl; - std::cout << "----------------------------" << 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; - std::cout << "Grades: "; + std::cout << "Constructor for s3 (ID, Name, Semester, Courses passed, Grades)" << std::endl; + std::cout << "----------------------------" << 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; + std::cout << "Grades: "; - float *gr = s3.get_grades(); - if (gr != nullptr) - { - for (unsigned int i = 0; i < s3.get_pcourses(); i++) - { - if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; - } - } + float *gr = s3.get_grades(); + if (gr != nullptr) + { + for (unsigned int i = 0; i < s3.get_pcourses(); i++) + { + if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl; + } + } } void detprint(const Student& s3) { - std::cout << "Detailed print of s3's grades" << std::endl; - std::cout << "----------------------------" << std::endl; - s3.detailed_print(); + std::cout << "Detailed print of s3's grades" << std::endl; + std::cout << "----------------------------" << std::endl; + s3.detailed_print(); } void setters(Student& s3) { - s3.set_id("010101"); - s3.set_name("AAAA"); - s3.set_semester(100); - s3.set_pcourses(2); - float *gg = new float[2]{0.1f, 2.2f}; - s3.set_grades(gg); - delete[] gg; + s3.set_id("010101"); + s3.set_name("AAAA"); + s3.set_semester(100); + s3.set_pcourses(2); + float *gg = new float[2]{0.1f, 2.2f}; + s3.set_grades(gg); + delete[] gg; - std::cout << "Setters example using s3" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Input: 010101 " << 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; - std::cout << std::endl; - std::cout << "Input: 100" << std::endl; - std::cout << "New semester: " << s3.get_semester() << std::endl; - std::cout << std::endl; - std::cout << "Input: 2" << std::endl; - std::cout << "New courses passed: " << s3.get_pcourses() << std::endl; - std::cout << std::endl; - std::cout << "Input: 0.1, 2.2" << std::endl; - std::cout << "New grades: "; + std::cout << "Setters example using s3" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: 010101 " << 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; + std::cout << std::endl; + std::cout << "Input: 100" << std::endl; + std::cout << "New semester: " << s3.get_semester() << std::endl; + std::cout << std::endl; + std::cout << "Input: 2" << std::endl; + std::cout << "New courses passed: " << s3.get_pcourses() << std::endl; + std::cout << std::endl; + std::cout << "Input: 0.1, 2.2" << std::endl; + std::cout << "New grades: "; - float *gr = s3.get_grades(); - if (gr != nullptr) - { - for (unsigned int i = 0; i < s3.get_pcourses(); i++) - { - if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl << std::endl; - } - } + float *gr = s3.get_grades(); + if (gr != nullptr) + { + for (unsigned int i = 0; i < s3.get_pcourses(); i++) + { + if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl << std::endl; + } + } } void addgrd(Student& s3) { - s3.add_grade(7.5f); - float *gr = s3.get_grades(); - if (gr != nullptr) - { - std::cout << "Input: 7.5" << std::endl; - std::cout << "Updated new grades: "; - for (unsigned int i = 0; i < s3.get_pcourses(); i++) - { - if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; - } - } + s3.add_grade(7.5f); + float *gr = s3.get_grades(); + if (gr != nullptr) + { + std::cout << "Input: 7.5" << std::endl; + std::cout << "Updated new grades: "; + for (unsigned int i = 0; i < s3.get_pcourses(); i++) + { + if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", "; + else std::cout << gr[i] << std::endl; + } + } } void submcourses(Student& s3) { - Course *a = new Course(std::string("11231"), std::string("Discrete Math"), 1); - Course *b = new Course(std::string("56562"), std::string("Physics"), 1); - Course **c = new Course *[2]{a, b}; - s3.set_num_submitted_courses(2); - s3.set_submitted_courses(c); + Course *a = new Course(std::string("11231"), std::string("Discrete Math"), 1); + Course *b = new Course(std::string("56562"), std::string("Physics"), 1); + Course **c = new Course *[2]{a, b}; + s3.set_num_submitted_courses(2); + s3.set_submitted_courses(c); - std::cout << "Submitted courses example" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Input: 2" << std::endl; - std::cout << "Number of submitted courses: " << s3.get_num_submitted_courses(); - std::cout << std::endl << std::endl; - - Course **sc = s3.get_submitted_courses(); - if (sc != nullptr) - { - std::cout << "Input: Discrete Math, Physics" << std::endl; - std::cout << "Submitted courses: "; - for (unsigned int i = 0; i < s3.get_num_submitted_courses(); i++) - { - if (i != s3.get_num_submitted_courses()-1) std::cout << sc[i][0].get_cname() << ", "; - else std::cout << sc[i][0].get_cname() << std::endl; - } - } + std::cout << "Submitted courses example" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: 2" << std::endl; + std::cout << "Number of submitted courses: " << s3.get_num_submitted_courses(); + std::cout << std::endl << std::endl; + + Course **sc = s3.get_submitted_courses(); + if (sc != nullptr) + { + std::cout << "Input: Discrete Math, Physics" << std::endl; + std::cout << "Submitted courses: "; + for (unsigned int i = 0; i < s3.get_num_submitted_courses(); i++) + { + if (i != s3.get_num_submitted_courses()-1) std::cout << sc[i][0].get_cname() << ", "; + else std::cout << sc[i][0].get_cname() << std::endl; + } + } } void plusequals_overload(Student& s3, Course *c) { - s3 += c; - Course **sc = s3.get_submitted_courses(); - if (sc != nullptr) - { - std::cout << "+= operator overload" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Input: OOP" << std::endl;; - std::cout << "Updated submitted courses: "; - for (unsigned int i = 0; i < s3.get_num_submitted_courses(); i++) - { - if (i != s3.get_num_submitted_courses()-1) std::cout << sc[i][0].get_cname() << ", "; - else std::cout << sc[i][0].get_cname() << std::endl; - } - } + s3 += c; + Course **sc = s3.get_submitted_courses(); + if (sc != nullptr) + { + std::cout << "+= operator overload" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: OOP" << std::endl;; + std::cout << "Updated submitted courses: "; + for (unsigned int i = 0; i < s3.get_num_submitted_courses(); i++) + { + if (i != s3.get_num_submitted_courses()-1) std::cout << sc[i][0].get_cname() << ", "; + else std::cout << sc[i][0].get_cname() << std::endl; + } + } } void getters(const Course& c) { - std::cout << "Getters example using Course object" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Constructor: Course(\"356431\", \"OOP\", 2)" << std::endl; - std::cout << "Code: " << c.get_code() << std::endl; - std::cout << "Course name: " << c.get_cname() << std::endl; - std::cout << "Course semester: " << c.get_csemester() << std::endl; + std::cout << "Getters example using Course object" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Constructor: Course(\"356431\", \"OOP\", 2)" << std::endl; + std::cout << "Code: " << c.get_code() << std::endl; + std::cout << "Course name: " << c.get_cname() << std::endl; + std::cout << "Course semester: " << c.get_csemester() << std::endl; } void setters(Course& c) { - c.set_code(std::string("14556")); - c.set_cname(std::string("Calculus I")); - c.set_csemester(1); + c.set_code(std::string("14556")); + c.set_cname(std::string("Calculus I")); + c.set_csemester(1); - std::cout << "Setters example using Course object" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Input: 14556" << std::endl; - std::cout << "New code: " << c.get_code() << std::endl; - std::cout << std::endl; - std::cout << "Input: Calculus I" << std::endl; - std::cout << "New course name: " << c.get_cname() << std::endl; - std::cout << std::endl; - std::cout << "Input: 1" << std::endl; - std::cout << "New course semester: " << c.get_csemester() << std::endl; + std::cout << "Setters example using Course object" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: 14556" << std::endl; + std::cout << "New code: " << c.get_code() << std::endl; + std::cout << std::endl; + std::cout << "Input: Calculus I" << std::endl; + std::cout << "New course name: " << c.get_cname() << std::endl; + std::cout << std::endl; + std::cout << "Input: 1" << std::endl; + std::cout << "New course semester: " << c.get_csemester() << std::endl; } diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -1,205 +1,205 @@ #include "student.h" Student::Student(const char *id, const std::string& name) - :id(convid(id)), name(name), semester(1), pcourses(0), - sc(nullptr), nsc(0) {} + :id(convid(id)), name(name), semester(1), pcourses(0), + sc(nullptr), nsc(0) {} Student::Student(const char *id, const std::string& name, - const unsigned int semester) - :id(convid(id)), name(name), semester(semester), pcourses(0), - sc(nullptr), nsc(0) {} + const unsigned int semester) + :id(convid(id)), name(name), semester(semester), pcourses(0), + sc(nullptr), nsc(0) {} 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)), sc(nullptr), nsc(0) {} + const unsigned int semester, + const unsigned int pcourses, const float *grades) + :id(convid(id)), name(name), semester(semester), pcourses(pcourses), + grades(convpsg(grades)), sc(nullptr), nsc(0) {} Student::Student(const Student& s) - :name(s.name), semester(s.semester), pcourses(s.pcourses) -{ - 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]; - std::memcpy(grades, s.grades, sizeof(float) * s.pcourses); - - if (s.nsc <= 0) - { - nsc = 0; - sc = nullptr; - } - else - { - this->sc = new Course *[s.nsc]; - std::memcpy(sc, s.sc, sizeof(s.sc) * s.nsc); - } + :name(s.name), semester(s.semester), pcourses(s.pcourses) +{ + 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]; + std::memcpy(grades, s.grades, sizeof(float) * s.pcourses); + + if (s.nsc <= 0) + { + nsc = 0; + sc = nullptr; + } + else + { + this->sc = new Course *[s.nsc]; + std::memcpy(sc, s.sc, sizeof(s.sc) * s.nsc); + } } Student::~Student() { - if (this->id != nullptr) - { - delete[] this->id; - this->id = nullptr; - } - if (this->grades != nullptr) - { - delete[] this->grades; - this->grades = nullptr; - } - if (this->sc != nullptr) - { - delete[] this->sc; - this->sc = nullptr; - } + if (this->id != nullptr) + { + delete[] this->id; + this->id = nullptr; + } + if (this->grades != nullptr) + { + delete[] this->grades; + this->grades = nullptr; + } + if (this->sc != nullptr) + { + delete[] this->sc; + this->sc = nullptr; + } } void Student::operator+= (Course *c) { - Course **tmp = new Course *[nsc+1]; - if (sc != nullptr) - { - std::memcpy(tmp, sc, sizeof(Course *) * nsc); - delete[] sc; - } - tmp[nsc] = c; - sc = tmp; - nsc++; + Course **tmp = new Course *[nsc+1]; + if (sc != nullptr) + { + std::memcpy(tmp, sc, sizeof(Course *) * nsc); + delete[] sc; + } + tmp[nsc] = c; + sc = tmp; + nsc++; } Student Student::operator= (const Student& s) { - if (this == &s) return *this; - this->id = convid(s.id); - this->name = s.name; - this->semester = s.semester; - this->pcourses = s.pcourses; - if (s.grades != nullptr) this->grades = convpsg(s.grades); - if (s.sc != nullptr) - { - this->nsc = s.nsc; - this->sc = s.sc; - } - return *this; + if (this == &s) return *this; + this->id = convid(s.id); + this->name = s.name; + this->semester = s.semester; + this->pcourses = s.pcourses; + if (s.grades != nullptr) this->grades = convpsg(s.grades); + if (s.sc != nullptr) + { + this->nsc = s.nsc; + this->sc = s.sc; + } + return *this; } const std::string& Student::get_name() const { - return this->name; + return this->name; } void Student::set_id(const char *id) { - if (this->id != nullptr) delete[] this->id; - this->id = convid(id); + if (this->id != nullptr) delete[] this->id; + this->id = convid(id); } void Student::set_name(const std::string& name) { - this->name = name; + this->name = name; } void Student::set_semester(const unsigned int semester) { - this->semester = semester; + this->semester = semester; } void Student::set_pcourses(const unsigned int pcourses) { - this->pcourses = pcourses; + this->pcourses = pcourses; } void Student::set_grades(const float *grades) { - this->grades = convpsg(grades); + this->grades = convpsg(grades); } void Student::set_num_submitted_courses(const unsigned nsc) { - this->nsc = nsc; + this->nsc = nsc; } void Student::set_submitted_courses(Course **sc) { - if (sc != nullptr) - { - if (this->sc != nullptr) delete[] this->sc; - this->sc = new Course *[nsc]; - std::memcpy(this->sc, sc, sizeof(Course *) * nsc); - } + if (sc != nullptr) + { + if (this->sc != nullptr) delete[] this->sc; + this->sc = new Course *[nsc]; + std::memcpy(this->sc, sc, sizeof(Course *) * nsc); + } } char * Student::convid(const char *id) { - int len = std::strlen(id); - char *tmp = new char[len+1]; - std::memcpy(tmp, id, len+1); - return tmp; + int len = std::strlen(id); + char *tmp = new char[len+1]; + std::memcpy(tmp, id, len+1); + return tmp; } float * Student::convpsg(const float *grades) { - if (pcourses > 0 && grades != nullptr) - { - float *tmp = new float[pcourses]; - std::memcpy(tmp, grades, sizeof(float) * pcourses); - if (this->grades != nullptr) delete[] this->grades; - return tmp; - } - else return nullptr; + if (pcourses > 0 && grades != nullptr) + { + float *tmp = new float[pcourses]; + std::memcpy(tmp, grades, sizeof(float) * pcourses); + if (this->grades != nullptr) delete[] this->grades; + return tmp; + } + else return nullptr; } void Student::add_grade(const float grade) { - float *tmp = new float[pcourses+1]; - if (grades != nullptr) - { - std::memcpy(tmp, grades, sizeof(float) * pcourses); - delete[] grades; - } - tmp[pcourses] = grade; - grades = tmp; - pcourses++; + float *tmp = new float[pcourses+1]; + if (grades != nullptr) + { + std::memcpy(tmp, grades, sizeof(float) * pcourses); + delete[] grades; + } + tmp[pcourses] = grade; + grades = tmp; + pcourses++; } void Student::detailed_print() const { - if (grades != nullptr) - { - for (unsigned int i = 0; i < pcourses; i++) - { - std::cout << "Course " << i+1 << ": "; - std::cout << grades[i] << std::endl; - } - std::cout << "Average grade: " << std::setprecision(2) << avg() << std::endl; - } + if (grades != nullptr) + { + for (unsigned int i = 0; i < pcourses; i++) + { + std::cout << "Course " << i+1 << ": "; + std::cout << grades[i] << std::endl; + } + std::cout << "Average grade: " << std::setprecision(2) << avg() << std::endl; + } } float Student::avg() const { - if (grades != nullptr) - { - float sum = 0; - for (unsigned int i = 0; i < pcourses; i++) - sum += grades[i]; - float average = sum / pcourses; - return average; - } - else return 0.0f; + if (grades != nullptr) + { + float sum = 0; + for (unsigned int i = 0; i < pcourses; i++) + sum += grades[i]; + float average = sum / pcourses; + return average; + } + else return 0.0f; } diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -10,59 +10,59 @@ class Student { - private: - char *id; // id - std::string name; // Name - unsigned int semester; // Current semester - unsigned int pcourses; // Passed courses - float *grades; // Grades - std::size_t size; - Course **sc; // Submitted courses - unsigned int nsc; // Number of submitted courses + private: + char *id; // id + std::string name; // Name + unsigned int semester; // Current semester + unsigned int pcourses; // Passed courses + float *grades; // Grades + std::size_t size; + Course **sc; // Submitted courses + unsigned int nsc; // Number of submitted courses - public: - 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(); + public: + 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(); - void operator+= (Course *c); - Student operator= (const Student& s); + void operator+= (Course *c); + Student operator= (const Student& s); - constexpr bool operator== (const Student& s) const {return this->semester == s.semester;} - constexpr bool operator!= (const Student& s) const {return this->semester != s.semester;} - constexpr bool operator< (const Student& s) const {return this->semester < s.semester;} - constexpr bool operator<= (const Student& s) const {return this->semester <= s.semester;} - constexpr bool operator> (const Student& s) const {return this->semester > s.semester;} - constexpr bool operator>= (const Student& s) const {return this->semester >= s.semester;} + constexpr bool operator== (const Student& s) const {return this->semester == s.semester;} + constexpr bool operator!= (const Student& s) const {return this->semester != s.semester;} + constexpr bool operator< (const Student& s) const {return this->semester < s.semester;} + constexpr bool operator<= (const Student& s) const {return this->semester <= s.semester;} + constexpr bool operator> (const Student& s) const {return this->semester > s.semester;} + constexpr bool operator>= (const Student& s) const {return this->semester >= s.semester;} - const std::string& get_name() const; - 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;} + const std::string& get_name() const; + 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_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 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(const float grade); - void detailed_print() const; + void add_grade(const float grade); + void detailed_print() const; - private: - char *convid(const char *id); - float *convpsg(const float *grades); - float avg() const; + private: + char *convid(const char *id); + float *convpsg(const float *grades); + float avg() const; }; #endif /* STUDENT_H */ diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -1,59 +1,59 @@ #include "app.h" App::App() - :serialnum(nullptr), name(""), os(""), manf(nullptr), price(0) {} + :serialnum(nullptr), name(""), os(""), manf(nullptr), price(0) {} 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) + const std::string& os, Manufacturer *manf, int price) + :serialnum(convsn(serialnum)), name(name), os(os), manf(manf), price(price) { - if (!std::strcmp(serialnum, "")) - throw std::runtime_error("Empty app serial number."); - if (name.empty()) - throw std::runtime_error("Empty app name."); - if (os.empty()) - throw std::runtime_error("Empty OS name."); - if (price < 0) - throw std::runtime_error("Price can't have negative value."); + if (!std::strcmp(serialnum, "")) + throw std::runtime_error("Empty app serial number."); + if (name.empty()) + throw std::runtime_error("Empty app name."); + if (os.empty()) + throw std::runtime_error("Empty OS name."); + if (price < 0) + throw std::runtime_error("Price can't have negative value."); } App::App(const App& a) - :serialnum(a.serialnum), name(a.name), os(a.os), manf(a.manf), - price(a.price) {} + :serialnum(a.serialnum), name(a.name), os(a.os), manf(a.manf), + price(a.price) {} App::~App() { - if (serialnum != nullptr) - { - delete[] serialnum; - serialnum = nullptr; - } - if (!reviews.empty()) - { - for (auto&& rev : reviews) - { - if (rev != nullptr) - { - delete rev; - rev = nullptr; - } - } - reviews.clear(); - } + if (serialnum != nullptr) + { + delete[] serialnum; + serialnum = nullptr; + } + if (!reviews.empty()) + { + for (auto&& rev : reviews) + { + if (rev != nullptr) + { + delete rev; + rev = nullptr; + } + } + reviews.clear(); + } } char * App::convsn(const char *serialnum) { - int len = std::strlen(serialnum); - char *tmp = new char[len + 1]; - std::copy(serialnum, serialnum + len + 1, tmp); - return tmp; + int len = std::strlen(serialnum); + char *tmp = new char[len + 1]; + std::copy(serialnum, serialnum + len + 1, tmp); + return tmp; } void App::set_serialnum(const char *serialnum) { - if (this->serialnum != nullptr) delete[] this->serialnum; - this->serialnum = convsn(serialnum); + if (this->serialnum != nullptr) delete[] this->serialnum; + this->serialnum = convsn(serialnum); } diff --git a/assignment-2.4-inheritance/src/app.h b/assignment-2.4-inheritance/src/app.h @@ -13,42 +13,42 @@ class App { - protected: - const char *serialnum; - std::string name; - std::string os; - Manufacturer *manf; - std::vector<Review *> reviews; - int price; - - public: - App(); - App(const char *serialnum, const std::string& name, - const std::string& os, Manufacturer *manf, int price); - App(const App& app); - virtual ~App(); - - void addrev(Review *rev) {reviews.push_back(rev);} - void addrevs(const std::vector<Review *> revs) - {reviews.insert(reviews.end(), revs.begin(), revs.end());} - - inline const std::string& get_name() const {return name;} - inline const std::string& get_os() const {return os;} - inline const Manufacturer get_manf() const {return Manufacturer(*manf);} - inline constexpr const char *get_serialnum() const {return serialnum;} - inline constexpr const std::vector<Review *>& get_revs() const {return reviews;} - inline constexpr int get_price() const {return price;} - - void set_serialnum(const char *serialnum); - inline void set_name(const std::string& name) {this->name = name;} - inline void set_os(const std::string& os) {this->os = os;} - inline void set_manf(Manufacturer *manf) {this->manf = manf;} - inline void set_price(int price) {this->price = price;} - - virtual void print(std::ostream& stream) const = 0; - - private: - char *convsn(const char *serialnum); + protected: + const char *serialnum; + std::string name; + std::string os; + Manufacturer *manf; + std::vector<Review *> reviews; + int price; + + public: + App(); + App(const char *serialnum, const std::string& name, + const std::string& os, Manufacturer *manf, int price); + App(const App& app); + virtual ~App(); + + void addrev(Review *rev) {reviews.push_back(rev);} + void addrevs(const std::vector<Review *> revs) + {reviews.insert(reviews.end(), revs.begin(), revs.end());} + + inline const std::string& get_name() const {return name;} + inline const std::string& get_os() const {return os;} + inline const Manufacturer get_manf() const {return Manufacturer(*manf);} + inline constexpr const char *get_serialnum() const {return serialnum;} + inline constexpr const std::vector<Review *>& get_revs() const {return reviews;} + inline constexpr int get_price() const {return price;} + + void set_serialnum(const char *serialnum); + inline void set_name(const std::string& name) {this->name = name;} + inline void set_os(const std::string& os) {this->os = os;} + inline void set_manf(Manufacturer *manf) {this->manf = manf;} + inline void set_price(int price) {this->price = price;} + + virtual void print(std::ostream& stream) const = 0; + + private: + char *convsn(const char *serialnum); }; #endif /* APP_H */ diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -4,205 +4,205 @@ AppSystem::AppSystem() {} AppSystem::~AppSystem() { - dealloc<App>(apps); - dealloc<Manufacturer>(manfs); + dealloc<App>(apps); + dealloc<Manufacturer>(manfs); } AppSystem& AppSystem::operator+= (App *app) { - apps.push_back(app); - return *this; + apps.push_back(app); + return *this; } AppSystem& AppSystem::operator+= (Manufacturer *man) { - manfs.push_back(man); - return *this; + manfs.push_back(man); + return *this; } const std::vector<std::string> AppSystem::parse_office_exts(std::ifstream& f) { - std::vector<std::string> exts; - std::string ext; - std::getline(f, ext); - std::istringstream iss(ext); - while (std::getline(iss, ext, '|')) exts.push_back(ext); - return exts; + std::vector<std::string> exts; + std::string ext; + std::getline(f, ext); + std::istringstream iss(ext); + while (std::getline(iss, ext, '|')) exts.push_back(ext); + return exts; } void AppSystem::write_office_exts(Office *of, std::ofstream& f) { - std::vector<std::string> exts = of->get_exts(); - for (const auto& ext : exts) - f << ext << '|'; + std::vector<std::string> exts = of->get_exts(); + for (const auto& ext : exts) + f << ext << '|'; } void AppSystem::newrev(const std::string& appname, Review *rev) { - for (auto&& app : apps) - if (app->get_name() == appname) - app->addrev(rev); + for (auto&& app : apps) + if (app->get_name() == appname) + app->addrev(rev); } void AppSystem::newrevs(const std::string& appname, const std::vector<Review *> revs) { - for (auto&& app : apps) - if (app->get_name() == appname) - app->addrevs(revs); + for (auto&& app : apps) + if (app->get_name() == appname) + app->addrevs(revs); } void AppSystem::chserialnum(const std::string& appname, const char *serialnum) { - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_serialnum(serialnum); + for (auto&& app : apps) + if (app->get_name() == appname) + app->set_serialnum(serialnum); } void AppSystem::chname(const std::string& appname, const std::string& name) { - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_name(name); + for (auto&& app : apps) + if (app->get_name() == appname) + app->set_name(name); } void AppSystem::chos(const std::string& appname, const std::string& os) { - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_os(os); + for (auto&& app : apps) + if (app->get_name() == appname) + app->set_os(os); } void AppSystem::chmanf(const std::string& appname, Manufacturer *manf) { - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_manf(manf); + for (auto&& app : apps) + if (app->get_name() == appname) + app->set_manf(manf); } void AppSystem::chprice(const std::string& appname, int price) { - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_price(price); + for (auto&& app : apps) + if (app->get_name() == appname) + app->set_price(price); } void AppSystem::chgenre(const std::string& appname, const std::string& genre) { - for (const auto& app : apps) - if (Game *o = dynamic_cast<Game *>(app)) - if (o->get_name() == appname) - o->set_genre(genre); + for (const auto& app : apps) + if (Game *o = dynamic_cast<Game *>(app)) + if (o->get_name() == appname) + o->set_genre(genre); } void AppSystem::chonline(const std::string& appname, bool online) { - for (const auto& app : apps) - if (Game *o = dynamic_cast<Game *>(app)) - if (o->get_name() == appname) - o->set_online(online); + for (const auto& app : apps) + if (Game *o = dynamic_cast<Game *>(app)) + if (o->get_name() == appname) + o->set_online(online); } void AppSystem::chexts(const std::string& appname, const std::vector<std::string> exts) { - for (const auto& app : apps) - if (Office *o = dynamic_cast<Office *>(app)) - if (o->get_name() == appname) - o->set_exts(exts); + for (const auto& app : apps) + if (Office *o = dynamic_cast<Office *>(app)) + if (o->get_name() == appname) + o->set_exts(exts); } void AppSystem::removebad(Manufacturer *man) { - apps.erase(std::remove_if(apps.begin(), apps.end(), [&](App *app) - { - Manufacturer m = app->get_manf(); - if (!std::strcmp(m.get_name(), man->get_name())) - delete app; - return !std::strcmp(m.get_name(), man->get_name()); - }), apps.end()); + apps.erase(std::remove_if(apps.begin(), apps.end(), [&](App *app) + { + Manufacturer m = app->get_manf(); + if (!std::strcmp(m.get_name(), man->get_name())) + delete app; + return !std::strcmp(m.get_name(), man->get_name()); + }), apps.end()); } void AppSystem::removebad(const char *manfname) { - apps.erase(std::remove_if(apps.begin(), apps.end(), [&](App *app) - { - Manufacturer m = app->get_manf(); - if (!std::strcmp(m.get_name(), manfname)) - delete app; - return !std::strcmp(m.get_name(), manfname); - }), apps.end()); + apps.erase(std::remove_if(apps.begin(), apps.end(), [&](App *app) + { + Manufacturer m = app->get_manf(); + if (!std::strcmp(m.get_name(), manfname)) + delete app; + return !std::strcmp(m.get_name(), manfname); + }), apps.end()); } const std::vector<Office *> AppSystem::get_freeapps() const { - std::vector<Office *> fapps; - for (const auto& app : apps) - if (Office *o = dynamic_cast<Office *>(app)) - if (o->get_price() == 0) - fapps.push_back(o); - return fapps; + std::vector<Office *> fapps; + for (const auto& app : apps) + if (Office *o = dynamic_cast<Office *>(app)) + if (o->get_price() == 0) + fapps.push_back(o); + return fapps; } const std::vector<Game *> AppSystem::get_goodgames() const { - std::vector<Game *> ggames; - for (const auto& app : apps) - { - if (Game *o = dynamic_cast<Game *>(app)) - { - std::vector<Review *> revs = o->get_revs(); - if (!revs.empty()) - { - int sum = 0, count = 0; - for (const auto& rev : revs) - { - sum += rev->get_stars(); - count++; - } - if (sum / count > 4) ggames.push_back(o); - } - } - } - return ggames; + std::vector<Game *> ggames; + for (const auto& app : apps) + { + if (Game *o = dynamic_cast<Game *>(app)) + { + std::vector<Review *> revs = o->get_revs(); + if (!revs.empty()) + { + int sum = 0, count = 0; + for (const auto& rev : revs) + { + sum += rev->get_stars(); + count++; + } + if (sum / count > 4) ggames.push_back(o); + } + } + } + return ggames; } bool AppSystem::valid_path(const std::string& strpath) { - return (strpath.find(".csv") != std::string::npos); + return (strpath.find(".csv") != std::string::npos); } const std::string AppSystem::err_csv(const std::string& strpath) { - return "Error. File must be of format \'.csv\'. (" + strpath + ")."; + return "Error. File must be of format \'.csv\'. (" + strpath + ")."; } const std::string AppSystem::err_read(const std::string& strpath) { - return "Error reading file \'" + strpath + "\'."; + return "Error reading file \'" + strpath + "\'."; } const std::string AppSystem::err_write(const std::string& strpath) { - return "Error writing to file \'" + strpath + "\'."; + return "Error writing to file \'" + strpath + "\'."; } diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -11,268 +11,268 @@ class AppSystem { - private: - std::vector<App *> apps; - std::vector<Manufacturer *> manfs; + private: + std::vector<App *> apps; + std::vector<Manufacturer *> manfs; - public: - AppSystem(); - ~AppSystem(); + public: + AppSystem(); + ~AppSystem(); - AppSystem& operator+= (App *app); - AppSystem& operator+= (Manufacturer *manf); + AppSystem& operator+= (App *app); + AppSystem& operator+= (Manufacturer *manf); - template<typename T> bool import_data(const char *fpath); - template<typename T> bool export_data(const char *fpath); - void newrev (const std::string& appname, Review *rev); - void newrevs (const std::string& appname, const std::vector<Review *> revs); - void chserialnum(const std::string& appname, const char *serialnum); - void chname (const std::string& appname, const std::string& name); - void chos (const std::string& appname, const std::string& os); - void chmanf (const std::string& appname, Manufacturer *manf); - void chprice (const std::string& appname, int price); - void chgenre (const std::string& appname, const std::string& genre); - void chonline (const std::string& appname, bool online); - void chexts (const std::string& appname, const std::vector<std::string> exts); - void removebad (Manufacturer *man); - void removebad (const char *manfname); - - inline constexpr const std::vector<App *>& get_apps() const {return apps;} - inline constexpr const std::vector<Manufacturer *>& get_manfs() const {return manfs;} - const std::vector<Office *> get_freeapps() const; - const std::vector<Game *> get_goodgames() const; + template<typename T> bool import_data(const char *fpath); + template<typename T> bool export_data(const char *fpath); + void newrev (const std::string& appname, Review *rev); + void newrevs (const std::string& appname, const std::vector<Review *> revs); + void chserialnum(const std::string& appname, const char *serialnum); + void chname (const std::string& appname, const std::string& name); + void chos (const std::string& appname, const std::string& os); + void chmanf (const std::string& appname, Manufacturer *manf); + void chprice (const std::string& appname, int price); + void chgenre (const std::string& appname, const std::string& genre); + void chonline (const std::string& appname, bool online); + void chexts (const std::string& appname, const std::vector<std::string> exts); + void removebad (Manufacturer *man); + void removebad (const char *manfname); + + inline constexpr const std::vector<App *>& get_apps() const {return apps;} + inline constexpr const std::vector<Manufacturer *>& get_manfs() const {return manfs;} + const std::vector<Office *> get_freeapps() const; + const std::vector<Game *> get_goodgames() const; - private: - template<typename T> bool parse(std::ifstream& f); - const std::vector<std::string> parse_office_exts(std::ifstream& f); - const std::vector<Review *> - parse_reviews(const std::string& appname, const char *rpath); - void write_office_exts(Office *of, std::ofstream& f); - bool valid_path(const std::string& strpath); - const std::string err_csv(const std::string& strpath); - const std::string err_read(const std::string& strpath); - const std::string err_write(const std::string& strpath); - template<typename T> void dealloc(std::vector<T *>& vec); + private: + template<typename T> bool parse(std::ifstream& f); + const std::vector<std::string> parse_office_exts(std::ifstream& f); + const std::vector<Review *> + parse_reviews(const std::string& appname, const char *rpath); + void write_office_exts(Office *of, std::ofstream& f); + bool valid_path(const std::string& strpath); + const std::string err_csv(const std::string& strpath); + const std::string err_read(const std::string& strpath); + const std::string err_write(const std::string& strpath); + template<typename T> void dealloc(std::vector<T *>& vec); }; template<typename T> bool AppSystem::parse(std::ifstream& f) { - try - { - if constexpr (std::is_same_v<T, Manufacturer>) - { - std::string sn, name, email; - std::getline(f, sn, ','); - std::getline(f, name, ','); - std::getline(f, email); - if (f.eof()) return true; - manfs.push_back(new Manufacturer(sn.c_str(), name.c_str(), email)); - } - else if constexpr (std::is_same_v<T, Office>) - { - std::string sn, name, os, manf, price, skip1, skip2; - std::getline(f, sn, ','); - std::getline(f, name, ','); - std::getline(f, os, ','); - std::getline(f, manf, ','); - std::getline(f, price, ','); - std::getline(f, skip1, ','); - std::getline(f, skip2, ','); - if (f.eof()) return true; - std::vector<std::string> exts = parse_office_exts(f); - - if (!manfs.empty()) - { - for (const auto& man : manfs) - { - if (man->get_name() == manf) - { - apps.push_back(new Office(sn.c_str(), name, os, - man, std::stoi(price), exts)); - break; - } - } - } - } - else if constexpr (std::is_same_v<T, Game>) - { - std::string sn, name, os, manf, price, genre, online; - std::string skip; - std::getline(f, sn, ','); - std::getline(f, name, ','); - std::getline(f, os, ','); - std::getline(f, manf, ','); - std::getline(f, price, ','); - std::getline(f, genre, ','); - std::getline(f, online, ','); - std::getline(f, skip); - if (f.eof()) return true; - bool onl = online == "Yes"; - - if (!manfs.empty()) - { - for (const auto& man : manfs) - { - if (man->get_name() == manf) - { - apps.push_back(new Game(sn.c_str(), name, os, - man, std::stoi(price), genre, onl)); - break; - } - } - } - } - } - catch (const std::ifstream::failure& e) - { - std::cerr << "Error parsing data." << std::endl << - e.what() << std::endl; - return false; - } - return true; + try + { + if constexpr (std::is_same_v<T, Manufacturer>) + { + std::string sn, name, email; + std::getline(f, sn, ','); + std::getline(f, name, ','); + std::getline(f, email); + if (f.eof()) return true; + manfs.push_back(new Manufacturer(sn.c_str(), name.c_str(), email)); + } + else if constexpr (std::is_same_v<T, Office>) + { + std::string sn, name, os, manf, price, skip1, skip2; + std::getline(f, sn, ','); + std::getline(f, name, ','); + std::getline(f, os, ','); + std::getline(f, manf, ','); + std::getline(f, price, ','); + std::getline(f, skip1, ','); + std::getline(f, skip2, ','); + if (f.eof()) return true; + std::vector<std::string> exts = parse_office_exts(f); + + if (!manfs.empty()) + { + for (const auto& man : manfs) + { + if (man->get_name() == manf) + { + apps.push_back(new Office(sn.c_str(), name, os, + man, std::stoi(price), exts)); + break; + } + } + } + } + else if constexpr (std::is_same_v<T, Game>) + { + std::string sn, name, os, manf, price, genre, online; + std::string skip; + std::getline(f, sn, ','); + std::getline(f, name, ','); + std::getline(f, os, ','); + std::getline(f, manf, ','); + std::getline(f, price, ','); + std::getline(f, genre, ','); + std::getline(f, online, ','); + std::getline(f, skip); + if (f.eof()) return true; + bool onl = online == "Yes"; + + if (!manfs.empty()) + { + for (const auto& man : manfs) + { + if (man->get_name() == manf) + { + apps.push_back(new Game(sn.c_str(), name, os, + man, std::stoi(price), genre, onl)); + break; + } + } + } + } + } + catch (const std::ifstream::failure& e) + { + std::cerr << "Error parsing data." << std::endl << + e.what() << std::endl; + return false; + } + return true; } template<typename T> bool AppSystem::import_data(const char *fpath) { - std::ifstream f; - f.exceptions(std::ifstream::badbit); - try - { - std::string strpath(fpath); - if (!valid_path(strpath)) - throw std::runtime_error(err_csv(strpath)); - f.open(fpath); - if (f.is_open()) - { - std::cout << "Importing data from \'" << fpath << "\'." << std::endl; - std::string skip; - std::getline(f, skip); - while (f.good()) - { - if constexpr (std::is_same_v<T, Manufacturer>) - { - if (!parse<Manufacturer>(f)) break; - } - else if constexpr (std::is_same_v<T, App>) - { - std::string type; - std::getline(f, type, ','); - if (type == "Game") - if (!parse<Game>(f)) break; - if (type == "Office") - if (!parse<Office>(f)) break; - } - else if constexpr (std::is_same_v<T, Review>) - { - std::string appname, stars, username, comment; - std::getline(f, appname, ','); - std::getline(f, stars, ','); - std::getline(f, username, ','); - std::getline(f, comment); - if (f.eof()) return true; - for (auto&& app : apps) - if (appname == app->get_name()) - app->addrev(new Review(std::stoi(stars), username, comment)); - } - } - } - f.close(); - } - catch (const std::ifstream::failure& e) - { - std::cerr << err_read(std::string(fpath)) << std::endl << e.what() << std::endl; - return false; - } - return true; + std::ifstream f; + f.exceptions(std::ifstream::badbit); + try + { + std::string strpath(fpath); + if (!valid_path(strpath)) + throw std::runtime_error(err_csv(strpath)); + f.open(fpath); + if (f.is_open()) + { + std::cout << "Importing data from \'" << fpath << "\'." << std::endl; + std::string skip; + std::getline(f, skip); + while (f.good()) + { + if constexpr (std::is_same_v<T, Manufacturer>) + { + if (!parse<Manufacturer>(f)) break; + } + else if constexpr (std::is_same_v<T, App>) + { + std::string type; + std::getline(f, type, ','); + if (type == "Game") + if (!parse<Game>(f)) break; + if (type == "Office") + if (!parse<Office>(f)) break; + } + else if constexpr (std::is_same_v<T, Review>) + { + std::string appname, stars, username, comment; + std::getline(f, appname, ','); + std::getline(f, stars, ','); + std::getline(f, username, ','); + std::getline(f, comment); + if (f.eof()) return true; + for (auto&& app : apps) + if (appname == app->get_name()) + app->addrev(new Review(std::stoi(stars), username, comment)); + } + } + } + f.close(); + } + catch (const std::ifstream::failure& e) + { + std::cerr << err_read(std::string(fpath)) << std::endl << e.what() << std::endl; + return false; + } + return true; } template<typename T> bool AppSystem::export_data(const char *fpath) { - std::ofstream f; - f.exceptions(std::ofstream::failbit | std::ofstream::badbit); - try - { - std::string strpath(fpath); - if (!valid_path(strpath)) - throw std::runtime_error(err_csv(strpath)); - f.open(fpath); - std::cout << "Exporting data to \'" << fpath << "\'." << std::endl; - - if constexpr (std::is_same_v<T, Manufacturer>) - { - f << "SN,Name,Email\n"; - for (const auto& manf : manfs) - f << manf->get_serialnum() << ',' << - manf->get_name() << ',' << - manf->get_email() << std::endl; - } - else if constexpr (std::is_same_v<T, App>) - { - f << "Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions\n"; - for (const auto& app : apps) - { - Manufacturer manf = app->get_manf(); - Game *o = dynamic_cast<Game *>(app); - f << (o ? "Game" : "Office") << ','; - f << app->get_serialnum() << ',' << - app->get_name() << ',' << - app->get_os() << ',' << - manf.get_name() << ',' << - app->get_price() << ',' << - (o ? o->get_genre() :"N/A") << ',' << - (o ? (o->get_online() ? "Yes" : "No") : "N/A") << ','; - if (o) f << "N/A" << std::endl; - else - { - Office *of = dynamic_cast<Office *>(app); - write_office_exts(of, f); - f << std::endl; - } - } - } - else if constexpr (std::is_same_v<T, Review>) - { - f << "AppName,Stars,Username,Comment\n"; - for (const auto& app : apps) - { - const std::vector<Review *> revs = app->get_revs(); - if (!revs.empty()) - for (const auto& rev : revs) - f << - app->get_name() << ',' << - rev->get_stars() << ',' << - rev->get_username() << ',' << - rev->get_comment() << std::endl; - } - } - f.close(); - } - catch (const std::ofstream::failure& e) - { - std::cerr << err_write(std::string(fpath)) << std::endl << e.what() << std::endl; - return false; - } - return true; + std::ofstream f; + f.exceptions(std::ofstream::failbit | std::ofstream::badbit); + try + { + std::string strpath(fpath); + if (!valid_path(strpath)) + throw std::runtime_error(err_csv(strpath)); + f.open(fpath); + std::cout << "Exporting data to \'" << fpath << "\'." << std::endl; + + if constexpr (std::is_same_v<T, Manufacturer>) + { + f << "SN,Name,Email\n"; + for (const auto& manf : manfs) + f << manf->get_serialnum() << ',' << + manf->get_name() << ',' << + manf->get_email() << std::endl; + } + else if constexpr (std::is_same_v<T, App>) + { + f << "Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions\n"; + for (const auto& app : apps) + { + Manufacturer manf = app->get_manf(); + Game *o = dynamic_cast<Game *>(app); + f << (o ? "Game" : "Office") << ','; + f << app->get_serialnum() << ',' << + app->get_name() << ',' << + app->get_os() << ',' << + manf.get_name() << ',' << + app->get_price() << ',' << + (o ? o->get_genre() :"N/A") << ',' << + (o ? (o->get_online() ? "Yes" : "No") : "N/A") << ','; + if (o) f << "N/A" << std::endl; + else + { + Office *of = dynamic_cast<Office *>(app); + write_office_exts(of, f); + f << std::endl; + } + } + } + else if constexpr (std::is_same_v<T, Review>) + { + f << "AppName,Stars,Username,Comment\n"; + for (const auto& app : apps) + { + const std::vector<Review *> revs = app->get_revs(); + if (!revs.empty()) + for (const auto& rev : revs) + f << + app->get_name() << ',' << + rev->get_stars() << ',' << + rev->get_username() << ',' << + rev->get_comment() << std::endl; + } + } + f.close(); + } + catch (const std::ofstream::failure& e) + { + std::cerr << err_write(std::string(fpath)) << std::endl << e.what() << std::endl; + return false; + } + return true; } template<typename T> void AppSystem::dealloc(std::vector<T *>& vec) { - if (!vec.empty()) - { - for (auto&& v : vec) - { - if (v != nullptr) - { - delete v; - v = nullptr; - } - } - vec.clear(); - } + if (!vec.empty()) + { + for (auto&& v : vec) + { + if (v != nullptr) + { + delete v; + v = nullptr; + } + } + vec.clear(); + } } #endif /* APPSYSTEM_H */ diff --git a/assignment-2.4-inheritance/src/game.cpp b/assignment-2.4-inheritance/src/game.cpp @@ -1,53 +1,53 @@ #include "game.h" Game::Game() - :App(), genre(""), online(false) {} + :App(), genre(""), online(false) {} Game::Game(const char *serialnum, const std::string& name, - const std::string& os, Manufacturer *manf, int price, - const std::string& genre, bool online) - :App(serialnum, name, os, manf, price), genre(genre), online(online) + const std::string& os, Manufacturer *manf, int price, + const std::string& genre, bool online) + :App(serialnum, name, os, manf, price), genre(genre), online(online) { - if (genre.empty() || genre == "") - throw std::runtime_error("Empty game genre."); + if (genre.empty() || genre == "") + throw std::runtime_error("Empty game genre."); } Game::Game(const Game& g) - :App(g.serialnum, g.name, g.os, g.manf, g.price), genre(g.genre), - online(g.online) {} + :App(g.serialnum, g.name, g.os, g.manf, g.price), genre(g.genre), + online(g.online) {} void Game::print(std::ostream& stream) const { - stream << - std::left << std::setw(7) << serialnum << - std::left << std::setw(20) << name << - std::left << std::setw(20) << os << - std::left << std::setw(7) << price; - - stream << - std::left << std::setw(7) << manf->get_serialnum() << - std::left << std::setw(15) << manf->get_name() << - std::left << std::setw(25) << manf->get_email(); - - stream << - std::left << std::setw(10) << genre << - std::left << std::setw(10) << (online ? "Yes" : "No"); - stream << "N/A"; - - if (!reviews.empty()) - { - stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; - stream << - std::left << std::setw(7) << "Stars" << - std::left << std::setw(25) << "Username" << - std::left << "Comment" << std::endl << std::endl; - - for (auto& rev : reviews) - stream << - std::left << std::setw(7) << rev->get_stars() << - std::left << std::setw(25) << rev->get_username() << - std::left << rev->get_comment() << std::endl; - } - stream << std::endl; + stream << + std::left << std::setw(7) << serialnum << + std::left << std::setw(20) << name << + std::left << std::setw(20) << os << + std::left << std::setw(7) << price; + + stream << + std::left << std::setw(7) << manf->get_serialnum() << + std::left << std::setw(15) << manf->get_name() << + std::left << std::setw(25) << manf->get_email(); + + stream << + std::left << std::setw(10) << genre << + std::left << std::setw(10) << (online ? "Yes" : "No"); + stream << "N/A"; + + if (!reviews.empty()) + { + stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; + stream << + std::left << std::setw(7) << "Stars" << + std::left << std::setw(25) << "Username" << + std::left << "Comment" << std::endl << std::endl; + + for (auto& rev : reviews) + stream << + std::left << std::setw(7) << rev->get_stars() << + std::left << std::setw(25) << rev->get_username() << + std::left << rev->get_comment() << std::endl; + } + stream << std::endl; } diff --git a/assignment-2.4-inheritance/src/game.h b/assignment-2.4-inheritance/src/game.h @@ -5,22 +5,22 @@ class Game: public App { - private: - std::string genre; - bool online; + private: + std::string genre; + bool online; - public: - Game(); - Game(const char *serialnum, const std::string& name, - const std::string& os, Manufacturer *manf, int price, - const std::string& genre, bool online); - Game(const Game& g); + public: + Game(); + Game(const char *serialnum, const std::string& name, + const std::string& os, Manufacturer *manf, int price, + const std::string& genre, bool online); + Game(const Game& g); - inline const std::string get_genre() const {return genre;} - inline constexpr bool get_online() const {return online;} - inline void set_genre(const std::string& genre) {this->genre = genre;} - inline void set_online(bool online) {this->online = online;} - virtual void print(std::ostream& stream) const override; + inline const std::string get_genre() const {return genre;} + inline constexpr bool get_online() const {return online;} + inline void set_genre(const std::string& genre) {this->genre = genre;} + inline void set_online(bool online) {this->online = online;} + virtual void print(std::ostream& stream) const override; }; #endif /* GAME_H */ diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -10,115 +10,115 @@ static void getapps(const AppSystem& sys); int main(int argc, char **argv) { - AppSystem sys; - system("clear || cls"); - if (!sys.import_data<Manufacturer>("res/manfdata.csv")) return -1; - if (!sys.import_data<App>("res/appdata.csv")) return -1; - if (!sys.import_data<Review>("res/revs.csv")) return -1; + AppSystem sys; + system("clear || cls"); + if (!sys.import_data<Manufacturer>("res/manfdata.csv")) return -1; + if (!sys.import_data<App>("res/appdata.csv")) return -1; + if (!sys.import_data<Review>("res/revs.csv")) return -1; - cont("Imported data"); - std::cout << sys << std::endl; - - pluseqs(sys); - cont("Additional data"); - std::cout << sys << std::endl; + cont("Imported data"); + std::cout << sys << std::endl; + + pluseqs(sys); + cont("Additional data"); + std::cout << sys << std::endl; - edit(sys); - cont("Editing data"); - std::cout << sys << std::endl; + edit(sys); + cont("Editing data"); + std::cout << sys << std::endl; - remove(sys); - cont("Removing data"); - std::cout << sys << std::endl; + remove(sys); + cont("Removing data"); + std::cout << sys << std::endl; - cont(""); - getapps(sys); + cont(""); + getapps(sys); - cont(""); - if (!sys.export_data<Manufacturer>("res/manfdata_out.csv")) return -1; - if (!sys.export_data<App>("res/appdata_out.csv")) return -1; - if (!sys.export_data<Review>("res/revs_out.csv")) return -1; - std::cout << std::endl << "Thank you :)" << std::endl; + cont(""); + if (!sys.export_data<Manufacturer>("res/manfdata_out.csv")) return -1; + if (!sys.export_data<App>("res/appdata_out.csv")) return -1; + if (!sys.export_data<Review>("res/revs_out.csv")) return -1; + std::cout << std::endl << "Thank you :)" << std::endl; - return 0; + return 0; } std::ostream& operator<< (std::ostream& stream, const AppSystem& sys) { - stream << - std::left << std::setw(7) << "SN" << - std::left << std::setw(20) << "Name" << - std::left << std::setw(20) << "OS" << - std::left << std::setw(7) << "Price" << - std::left << std::setw(7) << "SN" << - std::left << std::setw(15) << "Manufacturer" << - std::left << std::setw(25) << "Email" << - std::left << std::setw(10) << "Genre" << - std::left << std::setw(10) << "Online" << - std::left << std::setw(25) << "Extensions" << std::endl << std::endl; - std::vector<App *> apps = sys.get_apps(); - for (const auto& app : apps) - app->print(stream); - return stream; + stream << + std::left << std::setw(7) << "SN" << + std::left << std::setw(20) << "Name" << + std::left << std::setw(20) << "OS" << + std::left << std::setw(7) << "Price" << + std::left << std::setw(7) << "SN" << + std::left << std::setw(15) << "Manufacturer" << + std::left << std::setw(25) << "Email" << + std::left << std::setw(10) << "Genre" << + std::left << std::setw(10) << "Online" << + std::left << std::setw(25) << "Extensions" << std::endl << std::endl; + std::vector<App *> apps = sys.get_apps(); + for (const auto& app : apps) + app->print(stream); + return stream; } void cont(const char *s) { - std::cout << std::endl; - std::cout << "Press <ENTER> to continue. . ."; - if (std::cin.get()) system("clear || cls"); - if (strlen(s) > 0) std::cout << s << std::endl << std::endl; + std::cout << std::endl; + std::cout << "Press <ENTER> to continue. . ."; + if (std::cin.get()) system("clear || cls"); + if (strlen(s) > 0) std::cout << s << std::endl << std::endl; } void pluseqs(AppSystem& sys) { - Manufacturer *comp = new Manufacturer("0004", "Company", "comp@comp.com"); - Manufacturer *chris = new Manufacturer("0005", "Chris", "chris@chris.com"); - sys += comp; - sys += chris; - std::vector<std::string> ext = {".pdf", ".md"}; - sys += new Office("0004", "zathura", "NiceOS 1.1", comp, 0, ext); - sys += new Game("0005", "minecurses", "NiceOS 0.5", chris, 0, "Puzzle", false); + Manufacturer *comp = new Manufacturer("0004", "Company", "comp@comp.com"); + Manufacturer *chris = new Manufacturer("0005", "Chris", "chris@chris.com"); + sys += comp; + sys += chris; + std::vector<std::string> ext = {".pdf", ".md"}; + sys += new Office("0004", "zathura", "NiceOS 1.1", comp, 0, ext); + sys += new Game("0005", "minecurses", "NiceOS 0.5", chris, 0, "Puzzle", false); } void edit(AppSystem& sys) { - sys.newrev("minecurses", new Review(5, "gamer", "Good game")); - sys.newrevs("Vim", - {new Review(2, "user1", "Not so good"), - new Review(4, "user2", "Good app"), - new Review(5, "user3", "Very good :)")}); - sys.chserialnum("zathura", "1254"); - sys.chname("minecurses", "minesweeper"); - sys.chos("Vim", "GoodOS"); - sys.chmanf("LibreOffice", - new Manufacturer("0006", "FreeSoftware", "freesoft@freesoft.com")); - sys.chprice("LoL", 155); - sys.chgenre("CS:GO", "Shooter"); - sys.chonline("minesweeper", true); - sys.chexts("zathura", {".tex", ".epub", ".ms"}); + sys.newrev("minecurses", new Review(5, "gamer", "Good game")); + sys.newrevs("Vim", + {new Review(2, "user1", "Not so good"), + new Review(4, "user2", "Good app"), + new Review(5, "user3", "Very good :)")}); + sys.chserialnum("zathura", "1254"); + sys.chname("minecurses", "minesweeper"); + sys.chos("Vim", "GoodOS"); + sys.chmanf("LibreOffice", + new Manufacturer("0006", "FreeSoftware", "freesoft@freesoft.com")); + sys.chprice("LoL", 155); + sys.chgenre("CS:GO", "Shooter"); + sys.chonline("minesweeper", true); + sys.chexts("zathura", {".tex", ".epub", ".ms"}); } void remove(AppSystem& sys) { - sys.removebad("GNU"); + sys.removebad("GNU"); } void getapps(const AppSystem& sys) { - const std::vector<Office *>& fapps = sys.get_freeapps(); - const std::vector<Game *>& ggames = sys.get_goodgames(); - std::cout << "Free office apps: " << std::endl << std::endl; - for (const auto& fapp : fapps) - std::cout << fapp->get_name() << std::endl; - std::cout << std::endl; - std::cout << "Games with >4 rating: " << std::endl << std::endl; - for (const auto& ggame : ggames) - std::cout << ggame->get_name() << std::endl; + const std::vector<Office *>& fapps = sys.get_freeapps(); + const std::vector<Game *>& ggames = sys.get_goodgames(); + std::cout << "Free office apps: " << std::endl << std::endl; + for (const auto& fapp : fapps) + std::cout << fapp->get_name() << std::endl; + std::cout << 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 @@ -1,51 +1,51 @@ #include "manufacturer.h" Manufacturer::Manufacturer() - :serialnum(nullptr), name(nullptr), email("") {} + :serialnum(nullptr), name(nullptr), email("") {} Manufacturer::Manufacturer(const char *serialnum, const char *name, - const std::string& email) - :serialnum(convstr(serialnum)), name(convstr(name)), email(email) + const std::string& email) + :serialnum(convstr(serialnum)), name(convstr(name)), email(email) { - if (!std::strcmp(serialnum, "")) - throw std::runtime_error("Empty manufacturer serial number."); - if (!std::strcmp(name, "")) - throw std::runtime_error("Empty manufacturer name."); - if (email.empty() || email.find("@") == std::string::npos) - { - std::string err = "Invalid email. Empty email/Missing \'@\'"; - throw std::runtime_error(err); - } + if (!std::strcmp(serialnum, "")) + throw std::runtime_error("Empty manufacturer serial number."); + if (!std::strcmp(name, "")) + throw std::runtime_error("Empty manufacturer name."); + if (email.empty() || email.find("@") == std::string::npos) + { + std::string err = "Invalid email. Empty email/Missing \'@\'"; + throw std::runtime_error(err); + } } Manufacturer::Manufacturer(const Manufacturer& m) - :serialnum(convstr(m.serialnum)), name(convstr(m.name)), email(m.email) {} + :serialnum(convstr(m.serialnum)), name(convstr(m.name)), email(m.email) {} Manufacturer::~Manufacturer() { - if (serialnum != nullptr) delete[] serialnum; - if (name != nullptr) delete[] name; + if (serialnum != nullptr) delete[] serialnum; + if (name != nullptr) delete[] name; } char * Manufacturer::convstr(const char *str) { - int len = std::strlen(str); - char *tmp = new char[len + 1]; - std::copy(str, str + len + 1, tmp); - return tmp; + int len = std::strlen(str); + char *tmp = new char[len + 1]; + std::copy(str, str + len + 1, tmp); + return tmp; } void Manufacturer::set_serialnum(const char *serialnum) { - if (this->serialnum != nullptr) delete[] serialnum; - this->serialnum = convstr(serialnum); + if (this->serialnum != nullptr) delete[] serialnum; + this->serialnum = convstr(serialnum); } void Manufacturer::set_name(const char *name) { - if (this->name != nullptr) delete[] name; - this->name = convstr(name); + if (this->name != nullptr) delete[] name; + this->name = convstr(name); } diff --git a/assignment-2.4-inheritance/src/manufacturer.h b/assignment-2.4-inheritance/src/manufacturer.h @@ -7,28 +7,28 @@ class Manufacturer { - private: - const char *serialnum; - const char *name; - std::string email; + private: + const char *serialnum; + const char *name; + std::string email; - public: - Manufacturer(); - Manufacturer(const char *serialnum, const char *name, - const std::string& email); - Manufacturer(const Manufacturer& m); - ~Manufacturer(); + public: + Manufacturer(); + Manufacturer(const char *serialnum, const char *name, + const std::string& email); + Manufacturer(const Manufacturer& m); + ~Manufacturer(); - inline const std::string& get_email() const {return email;} - inline constexpr const char *get_serialnum() const {return serialnum;} - inline constexpr const char *get_name() const {return name;} + inline const std::string& get_email() const {return email;} + inline constexpr const char *get_serialnum() const {return serialnum;} + inline constexpr const char *get_name() const {return name;} - void set_serialnum(const char *serialnum); - void set_name(const char *name); - inline void set_email(const std::string& email) {this->email = email;} + void set_serialnum(const char *serialnum); + void set_name(const char *name); + inline void set_email(const std::string& email) {this->email = email;} - private: - char *convstr(const char *str); + private: + char *convstr(const char *str); }; #endif /* MANUFACTURER_H */ diff --git a/assignment-2.4-inheritance/src/office.cpp b/assignment-2.4-inheritance/src/office.cpp @@ -1,63 +1,63 @@ #include "office.h" Office::Office() - :App() {} + :App() {} Office::Office(const char *serialnum, const std::string& name, - const std::string& os, Manufacturer *manf, - int price, const std::vector<std::string>& ext) - :App(serialnum, name, os, manf, price), extensions(ext) {} + const std::string& os, Manufacturer *manf, + int price, const std::vector<std::string>& ext) + :App(serialnum, name, os, manf, price), extensions(ext) {} Office::Office(const Office& o) - :App(o.serialnum, o.name, o.os, o.manf, o.price) {} + :App(o.serialnum, o.name, o.os, o.manf, o.price) {} Office::~Office() { - if (!extensions.empty()) extensions.clear(); + if (!extensions.empty()) extensions.clear(); } void Office::set_exts(const std::vector<std::string>& extensions) { - if (!this->extensions.empty()) this->extensions.clear(); - this->extensions = extensions; + if (!this->extensions.empty()) this->extensions.clear(); + this->extensions = extensions; } void Office::print(std::ostream& stream) const { - stream << - std::left << std::setw(7) << serialnum << - std::left << std::setw(20) << name << - std::left << std::setw(20) << os << - std::left << std::setw(7) << price; - - stream << - std::left << std::setw(7) << manf->get_serialnum() << - std::left << std::setw(15) << manf->get_name() << - std::left << std::setw(25) << manf->get_email(); - - stream << - std::left << std::setw(10) << "N/A" << - std::left << std::setw(10) << "N/A"; - - for (auto& ext : extensions) - stream << ext << " "; - - std::vector<Review *> revs = get_revs(); - if (!reviews.empty()) - { - stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; - stream << - std::left << std::setw(7) << "Stars" << - std::left << std::setw(25) << "Username" << - std::left << "Comment" << std::endl << std::endl; - - for (auto& rev : reviews) - stream << - std::left << std::setw(7) << rev->get_stars() << - std::left << std::setw(25) << rev->get_username() << - std::left << rev->get_comment() << std::endl; - } - stream << std::endl; + stream << + std::left << std::setw(7) << serialnum << + std::left << std::setw(20) << name << + std::left << std::setw(20) << os << + std::left << std::setw(7) << price; + + stream << + std::left << std::setw(7) << manf->get_serialnum() << + std::left << std::setw(15) << manf->get_name() << + std::left << std::setw(25) << manf->get_email(); + + stream << + std::left << std::setw(10) << "N/A" << + std::left << std::setw(10) << "N/A"; + + for (auto& ext : extensions) + stream << ext << " "; + + std::vector<Review *> revs = get_revs(); + if (!reviews.empty()) + { + stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; + stream << + std::left << std::setw(7) << "Stars" << + std::left << std::setw(25) << "Username" << + std::left << "Comment" << std::endl << std::endl; + + for (auto& rev : reviews) + stream << + std::left << std::setw(7) << rev->get_stars() << + std::left << std::setw(25) << rev->get_username() << + std::left << rev->get_comment() << std::endl; + } + stream << std::endl; } diff --git a/assignment-2.4-inheritance/src/office.h b/assignment-2.4-inheritance/src/office.h @@ -5,20 +5,20 @@ class Office: public App { - private: - std::vector<std::string> extensions; + private: + std::vector<std::string> extensions; - public: - Office(); - Office(const char *serialnum, const std::string& name, - const std::string& os, Manufacturer *manf, - int price, const std::vector<std::string>& ext); - Office(const Office& o); - ~Office(); + public: + Office(); + Office(const char *serialnum, const std::string& name, + const std::string& os, Manufacturer *manf, + int price, const std::vector<std::string>& ext); + Office(const Office& o); + ~Office(); - const std::vector<std::string> get_exts() const {return extensions;} - void set_exts(const std::vector<std::string>& extensions); - virtual void print(std::ostream& stream) const override; + const std::vector<std::string> get_exts() const {return extensions;} + void set_exts(const std::vector<std::string>& extensions); + virtual void print(std::ostream& stream) const override; }; #endif /* OFFICE_H */ diff --git a/assignment-2.4-inheritance/src/review.cpp b/assignment-2.4-inheritance/src/review.cpp @@ -1,14 +1,14 @@ #include "review.h" Review::Review() - :stars(0), username(""), comment("") {} + :stars(0), username(""), comment("") {} Review::Review(int stars, const std::string& username, const std::string& comment) - :stars(stars), username(username), comment(comment) + :stars(stars), username(username), comment(comment) { - if (stars < 0 || stars > 5) - throw std::runtime_error("Wrong star value. Must be in [0, 5]"); + if (stars < 0 || stars > 5) + throw std::runtime_error("Wrong star value. Must be in [0, 5]"); } Review::Review(const Review& r) - :stars(r.stars), username(r.username), comment(r.comment) {} + :stars(r.stars), username(r.username), comment(r.comment) {} diff --git a/assignment-2.4-inheritance/src/review.h b/assignment-2.4-inheritance/src/review.h @@ -6,26 +6,26 @@ class Review { - private: - int stars; - std::string username; - std::string comment; + private: + int stars; + std::string username; + std::string comment; - public: - Review(); - Review(int stars, const std::string& username, - const std::string& comment); - Review(const Review& r); + public: + Review(); + Review(int stars, const std::string& username, + const std::string& comment); + Review(const Review& r); - inline const std::string& get_username() const {return username;} - inline const std::string& get_comment() const {return comment;} - inline constexpr int get_stars() const {return stars;} + inline const std::string& get_username() const {return username;} + inline const std::string& get_comment() const {return comment;} + inline constexpr int get_stars() const {return stars;} - inline void set_stars(int stars) {this->stars = stars;} - inline void set_username(const std::string& username) - {this->username = username;} - inline void set_comment(const std::string& comment) - {this->comment = comment;} + inline void set_stars(int stars) {this->stars = stars;} + inline void set_username(const std::string& username) + {this->username = username;} + inline void set_comment(const std::string& comment) + {this->comment = comment;} }; #endif /* REVIEW_H */ diff --git a/assignment-2.5-spreadsheets/src/course.h b/assignment-2.5-spreadsheets/src/course.h @@ -5,16 +5,16 @@ class Course { - private: - lab::xstring code; - lab::xstring name; + private: + lab::xstring code; + lab::xstring name; - public: - Course(const lab::xstring& code, const lab::xstring& name) - :code(code), name(name) {} + public: + Course(const lab::xstring& code, const lab::xstring& name) + :code(code), name(name) {} - inline constexpr const lab::xstring& get_code() const {return code;} - inline constexpr const lab::xstring& get_name() const {return name;} + inline constexpr const lab::xstring& get_code() const {return code;} + inline constexpr const lab::xstring& get_name() const {return name;} }; #endif /* COURSE_H */ diff --git a/assignment-2.5-spreadsheets/src/datahandler.cpp b/assignment-2.5-spreadsheets/src/datahandler.cpp @@ -1,228 +1,228 @@ #include "datahandler.h" DataHandler::DataHandler() - :misscount(0), errcount(0) {} + :misscount(0), errcount(0) {} DataHandler::~DataHandler() { - dealloc<Course>(courses); - dealloc<Student>(studs); - if (!grds.empty()) grds.clear(); - if (!data.empty()) data.clear(); - if (!eqvs.empty()) eqvs.clear(); - if (!errs.empty()) errs.clear(); - if (!missing.empty()) missing.clear(); + dealloc<Course>(courses); + dealloc<Student>(studs); + if (!grds.empty()) grds.clear(); + if (!data.empty()) data.clear(); + if (!eqvs.empty()) eqvs.clear(); + if (!errs.empty()) errs.clear(); + if (!missing.empty()) missing.clear(); } bool DataHandler::store_data() { - std::ifstream f; - f.exceptions(std::ifstream::badbit); - try - { - if (!valid_path(datapath)) - throw std::runtime_error(err_csv(datapath).cstr()); - f.open(datapath); - if (f.is_open()) - { - std::cout << "Importing data from \'" << datapath << "\'." << std::endl; - std::cout << std::endl; - std::cout << "Making data structures and analyzing data." << std::endl; - - lab::xstring skip; - lab::getline(f, skip); - - 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(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 currid = id; - while (currid == id) - { - lab::getline(f, id, ';'); - lab::getline(f, code, ';'); - lab::getline(f, grade); - if (f.eof()) break; - if (!analyze(currid, id, code, std::atof(grade.cstr()))) break; - } - } - } - f.close(); - } - catch (const std::ifstream::failure& e) - { - std::cerr << err_read(datapath) << std::endl << e.what() << std::endl; - return false; - } - return true; + std::ifstream f; + f.exceptions(std::ifstream::badbit); + try + { + if (!valid_path(datapath)) + throw std::runtime_error(err_csv(datapath).cstr()); + f.open(datapath); + if (f.is_open()) + { + std::cout << "Importing data from \'" << datapath << "\'." << std::endl; + std::cout << std::endl; + std::cout << "Making data structures and analyzing data." << std::endl; + + lab::xstring skip; + lab::getline(f, skip); + + 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(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 currid = id; + while (currid == id) + { + lab::getline(f, id, ';'); + lab::getline(f, code, ';'); + lab::getline(f, grade); + if (f.eof()) break; + if (!analyze(currid, id, code, std::atof(grade.cstr()))) break; + } + } + } + f.close(); + } + catch (const std::ifstream::failure& e) + { + std::cerr << err_read(datapath) << std::endl << e.what() << std::endl; + return false; + } + return true; } bool DataHandler::analyze( - const lab::xstring& currid, - lab::xstring& id, - lab::xstring& code, - float grade) + const lab::xstring& currid, + lab::xstring& id, + lab::xstring& code, + float grade) { - 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 (currid != id) - { - data.insert(std::make_pair(studs[currid], grds)); - grds.clear(); - grds.insert(std::make_pair(courses[code], grade)); - return false; - } - grds.insert(std::make_pair(courses[code], grade)); - } - else if (its == studs.end()) - { - if (std::find(errs.begin(), errs.end(), id) == errs.end()) - { - errs.push_back(id); - errlog.write(ErrLog::ErrType::STUDENT_MISSING, id); - errcount++; - } - } - else if (itc == courses.end()) - { - if (std::find(errs.begin(), errs.end(), code) == errs.end()) - { - errs.push_back(code); - errlog.write(ErrLog::ErrType::COURSE_MISSING, code); - errcount++; - } - } - - if (its != studs.end() && itc != courses.end()) - { - miss(id, code, grade); - diffr(id, code, grade); - } - - return true; + 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 (currid != id) + { + data.insert(std::make_pair(studs[currid], grds)); + grds.clear(); + grds.insert(std::make_pair(courses[code], grade)); + return false; + } + grds.insert(std::make_pair(courses[code], grade)); + } + else if (its == studs.end()) + { + if (std::find(errs.begin(), errs.end(), id) == errs.end()) + { + errs.push_back(id); + errlog.write(ErrLog::ErrType::STUDENT_MISSING, id); + errcount++; + } + } + else if (itc == courses.end()) + { + if (std::find(errs.begin(), errs.end(), code) == errs.end()) + { + errs.push_back(code); + errlog.write(ErrLog::ErrType::COURSE_MISSING, code); + errcount++; + } + } + + if (its != studs.end() && itc != courses.end()) + { + miss(id, code, grade); + diffr(id, code, grade); + } + + return true; } void DataHandler::miss(lab::xstring id, lab::xstring code, float grade) { - if (code.front() == 'P') - { - std::map<lab::xstring, lab::xstring>::const_iterator it = eqvs.find(code); - if (it != eqvs.end()) - { - bool found = false; - for (const auto& grd : grds) - if (grd.first->get_code() == eqvs[code]) - found = true; - if (!found) - { - missing.push_back(id + ";" + - studs[id]->get_lname() + ";" + - studs[id]->get_fname() + ";" + - courses[eqvs[code]]->get_code() + ";" + - courses[eqvs[code]]->get_name() + ";" + - eqvs[code] + ";" + - courses[code]->get_name() + ";" + - lab::to_xstr<float>("%.1f", grade)); - misscount++; - } - } - } + if (code.front() == 'P') + { + std::map<lab::xstring, lab::xstring>::const_iterator it = eqvs.find(code); + if (it != eqvs.end()) + { + bool found = false; + for (const auto& grd : grds) + if (grd.first->get_code() == eqvs[code]) + found = true; + if (!found) + { + missing.push_back(id + ";" + + studs[id]->get_lname() + ";" + + studs[id]->get_fname() + ";" + + courses[eqvs[code]]->get_code() + ";" + + courses[eqvs[code]]->get_name() + ";" + + eqvs[code] + ";" + + courses[code]->get_name() + ";" + + lab::to_xstr<float>("%.1f", grade)); + misscount++; + } + } + } } void 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(id + " in " + code + ": " + - lab::to_xstr<float>("%.1f", it->second) + " | " + - lab::to_xstr<float>("%.1f", grade))); - errcount++; - } + 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(id + " in " + code + ": " + + lab::to_xstr<float>("%.1f", it->second) + " | " + + lab::to_xstr<float>("%.1f", grade))); + errcount++; + } } bool DataHandler::make_report() const { - std::ofstream f; - f.exceptions(std::ofstream::failbit | std::ofstream::badbit); - try - { - if (!valid_path(reppath)) - throw std::runtime_error(err_csv(reppath).cstr()); - f.open(reppath); - if (f.is_open()) - { - std::cout << "Making report." << std::endl; - 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(); - } - } - catch (const std::ofstream::failure& e) - { - std::cerr << err_write(reppath) << std::endl << e.what() << std::endl; - return false; - } - return true; + std::ofstream f; + f.exceptions(std::ofstream::failbit | std::ofstream::badbit); + try + { + if (!valid_path(reppath)) + throw std::runtime_error(err_csv(reppath).cstr()); + f.open(reppath); + if (f.is_open()) + { + std::cout << "Making report." << std::endl; + 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(); + } + } + catch (const std::ofstream::failure& e) + { + std::cerr << err_write(reppath) << std::endl << e.what() << std::endl; + return false; + } + return true; } void DataHandler::summary() const { - int datacount = 0; - for (const auto& dat : data) - datacount += dat.second.size(); - - std::cout << std::endl; - std::cout << "Students: " << studs.size() << std::endl; - std::cout << "Courses: " << courses.size() << std::endl; - std::cout << "Equivalencies: " << eqvs.size() << std::endl; - std::cout << "Total grades stored: " << datacount << std::endl; - std::cout << "Grades missing: " << misscount << std::endl; - std::cout << "Errors: " << errcount << std::endl; - std::cout << std::endl; - std::cout << "Thank you :)" << std::endl; + int datacount = 0; + for (const auto& dat : data) + datacount += dat.second.size(); + + std::cout << std::endl; + std::cout << "Students: " << studs.size() << std::endl; + std::cout << "Courses: " << courses.size() << std::endl; + std::cout << "Equivalencies: " << eqvs.size() << std::endl; + std::cout << "Total grades stored: " << datacount << std::endl; + std::cout << "Grades missing: " << misscount << std::endl; + std::cout << "Errors: " << errcount << std::endl; + std::cout << std::endl; + std::cout << "Thank you :)" << std::endl; } bool DataHandler::valid_path(const char *fpath) const { - return (std::strstr(fpath, ".csv") != nullptr); + return (std::strstr(fpath, ".csv") != nullptr); } const lab::xstring DataHandler::err_csv(const char *fpath) const { - return lab::xstring("Error. File must be of format \'.csv\'. ()"). - insert(fpath, 39); + return lab::xstring("Error. File must be of format \'.csv\'. ()"). + insert(fpath, 39); } const lab::xstring DataHandler::err_read(const char *fpath) const { - return lab::xstring("Error reading file \'\'.").insert(fpath, 20); + return lab::xstring("Error reading file \'\'.").insert(fpath, 20); } const lab::xstring DataHandler::err_write(const char *fpath) const { - return lab::xstring("Error writing to file \'\'.").insert(fpath, 23); + return lab::xstring("Error writing to file \'\'.").insert(fpath, 23); } diff --git a/assignment-2.5-spreadsheets/src/datahandler.h b/assignment-2.5-spreadsheets/src/datahandler.h @@ -16,112 +16,112 @@ using equivalencies = std::map<lab::xstring, lab::xstring>; class DataHandler { - private: - const char *datapath = "res/grades.csv"; - const char *reppath = "res/report.csv"; - std::map<lab::xstring, Course *> courses; - std::map<lab::xstring, Student *> studs; - std::map<Course *, float> grds; - std::map<Student *, std::map<Course *, float>> data; - std::vector<lab::xstring> errs; - std::vector<lab::xstring> missing; - equivalencies eqvs; - ErrLog errlog; - int misscount, errcount; + private: + const char *datapath = "res/grades.csv"; + const char *reppath = "res/report.csv"; + std::map<lab::xstring, Course *> courses; + std::map<lab::xstring, Student *> studs; + std::map<Course *, float> grds; + std::map<Student *, std::map<Course *, float>> data; + std::vector<lab::xstring> errs; + std::vector<lab::xstring> missing; + equivalencies eqvs; + ErrLog errlog; + int misscount, errcount; - public: - DataHandler(); - ~DataHandler(); + public: + DataHandler(); + ~DataHandler(); - template<typename T> bool import_data(const char *fpath); - bool store_data(); - bool make_report() const; - void summary() const; - - private: - bool analyze( - const lab::xstring& currid, - lab::xstring& id, - 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; - const lab::xstring err_write(const char *fpath) const; - template<typename T> void dealloc(std::map<lab::xstring, T *>& vec); + template<typename T> bool import_data(const char *fpath); + bool store_data(); + bool make_report() const; + void summary() const; + + private: + bool analyze( + const lab::xstring& currid, + lab::xstring& id, + 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; + const lab::xstring err_write(const char *fpath) const; + template<typename T> void dealloc(std::map<lab::xstring, T *>& vec); }; template<typename T> bool DataHandler::import_data(const char *fpath) { - std::ifstream f; - f.exceptions(std::ifstream::badbit); - try - { - if (!valid_path(fpath)) - throw std::runtime_error(err_csv(fpath).cstr()); - f.open(fpath); - if (f.is_open()) - { - std::cout << "Importing data from \'" << fpath << "\'." << std::endl; - lab::xstring skip; - lab::getline(f, skip); - while (f.good()) - { - if constexpr (std::is_same_v<T, Course>) - { - lab::xstring code, name; - lab::getline(f, code, ';'); - lab::getline(f, name); - if (f.eof()) break; - courses.insert(std::make_pair(code, new Course(code, name))); - } - else if constexpr (std::is_same_v<T, Student>) - { - 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(id, new Student(id, lname, fname))); - } - else if constexpr (std::is_same_v<T, equivalencies>) - { - lab::xstring oldcurr, newcurr; - lab::getline(f, oldcurr, ';'); - lab::getline(f, newcurr); - if (f.eof()) break; - eqvs.insert(std::make_pair(oldcurr, newcurr)); - } - } - } - f.close(); - } - catch (const std::ifstream::failure& e) - { - std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; - return false; - } - return true; + std::ifstream f; + f.exceptions(std::ifstream::badbit); + try + { + if (!valid_path(fpath)) + throw std::runtime_error(err_csv(fpath).cstr()); + f.open(fpath); + if (f.is_open()) + { + std::cout << "Importing data from \'" << fpath << "\'." << std::endl; + lab::xstring skip; + lab::getline(f, skip); + while (f.good()) + { + if constexpr (std::is_same_v<T, Course>) + { + lab::xstring code, name; + lab::getline(f, code, ';'); + lab::getline(f, name); + if (f.eof()) break; + courses.insert(std::make_pair(code, new Course(code, name))); + } + else if constexpr (std::is_same_v<T, Student>) + { + 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(id, new Student(id, lname, fname))); + } + else if constexpr (std::is_same_v<T, equivalencies>) + { + lab::xstring oldcurr, newcurr; + lab::getline(f, oldcurr, ';'); + lab::getline(f, newcurr); + if (f.eof()) break; + eqvs.insert(std::make_pair(oldcurr, newcurr)); + } + } + } + f.close(); + } + catch (const std::ifstream::failure& e) + { + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; + return false; + } + return true; } template<typename T> void DataHandler::dealloc(std::map<lab::xstring, T *>& vec) { - if (!vec.empty()) - { - for (auto&& v : vec) - { - if (v.second != nullptr) - { - delete v.second; - v.second = nullptr; - } - } - vec.clear(); - } + if (!vec.empty()) + { + for (auto&& v : vec) + { + if (v.second != nullptr) + { + delete v.second; + v.second = nullptr; + } + } + vec.clear(); + } } #endif /* DATA_HANDLER_H */ diff --git a/assignment-2.5-spreadsheets/src/errlog.cpp b/assignment-2.5-spreadsheets/src/errlog.cpp @@ -5,28 +5,28 @@ ErrLog::ErrLog() {fclear();} void ErrLog::fclear() const { - std::ofstream f; - f.open(fpath, std::ofstream::trunc); - f << "Type;Info;Err" << std::endl; - f.close(); + std::ofstream f; + f.open(fpath, std::ofstream::trunc); + f << "Type;Info;Err" << std::endl; + f.close(); } void ErrLog::write(ErrType type, const lab::xstring& s) const { - std::ofstream f; - f.open(fpath, std::ios_base::app); - switch(type) - { - case ErrType::STUDENT_MISSING: - f << "Student;" << s << ";Missing" << std::endl; - break; - case ErrType::COURSE_MISSING: - f << "Course;" << s << ";Missing" << std::endl; - break; - case ErrType::DIFFERENT_GRADES: - f << "Grade;" << s << ";Different" << std::endl; - break; - } - f.close(); + std::ofstream f; + f.open(fpath, std::ios_base::app); + switch(type) + { + case ErrType::STUDENT_MISSING: + f << "Student;" << s << ";Missing" << std::endl; + break; + case ErrType::COURSE_MISSING: + f << "Course;" << s << ";Missing" << std::endl; + break; + case ErrType::DIFFERENT_GRADES: + f << "Grade;" << s << ";Different" << std::endl; + break; + } + f.close(); } diff --git a/assignment-2.5-spreadsheets/src/errlog.h b/assignment-2.5-spreadsheets/src/errlog.h @@ -6,21 +6,21 @@ class ErrLog { - private: - const char *fpath = "res/errlog.csv"; - void fclear() const; + private: + const char *fpath = "res/errlog.csv"; + void fclear() const; - public: - enum class ErrType { - STUDENT_MISSING, - COURSE_MISSING, - DIFFERENT_GRADES - }; + public: + enum class ErrType { + STUDENT_MISSING, + COURSE_MISSING, + DIFFERENT_GRADES + }; - ErrLog(); - void write(ErrType type, const lab::xstring& s) const; + ErrLog(); + void write(ErrType type, const lab::xstring& s) const; - private: + private: }; #endif /* ERRLOG_H */ diff --git a/assignment-2.5-spreadsheets/src/main.cpp b/assignment-2.5-spreadsheets/src/main.cpp @@ -3,13 +3,13 @@ int main(int argc, char **argv) { - DataHandler dh; - if (!dh.import_data<Course>("res/courses.csv")) return -1; - if (!dh.import_data<Student>("res/students.csv")) return -1; - if (!dh.import_data<equivalencies>("res/equivalencies.csv")) return -1; - if (!dh.store_data()) return -1; - if (!dh.make_report()) return -1; - dh.summary(); + DataHandler dh; + if (!dh.import_data<Course>("res/courses.csv")) return -1; + if (!dh.import_data<Student>("res/students.csv")) return -1; + if (!dh.import_data<equivalencies>("res/equivalencies.csv")) return -1; + if (!dh.store_data()) return -1; + if (!dh.make_report()) return -1; + dh.summary(); - return 0; + return 0; } diff --git a/assignment-2.5-spreadsheets/src/student.h b/assignment-2.5-spreadsheets/src/student.h @@ -5,20 +5,20 @@ class Student { - private: - lab::xstring id; - lab::xstring lname; - lab::xstring fname; + private: + lab::xstring id; + lab::xstring lname; + lab::xstring fname; - public: - Student(const lab::xstring& id, - const lab::xstring& lname, - const lab::xstring& fname) - :id(id), lname(lname), fname(fname) {} + public: + Student(const lab::xstring& id, + const lab::xstring& lname, + const lab::xstring& fname) + :id(id), lname(lname), fname(fname) {} - inline constexpr const lab::xstring& get_id() const {return id;} - inline constexpr const lab::xstring& get_lname() const {return lname;} - inline constexpr const lab::xstring& get_fname() const {return fname;} + inline constexpr const lab::xstring& get_id() const {return id;} + inline constexpr const lab::xstring& get_lname() const {return lname;} + inline constexpr const lab::xstring& get_fname() const {return fname;} }; #endif /* STUDENT_H */ diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -3,269 +3,269 @@ namespace lab { xstring::xstring() { - str = new char[1]; - str[0] = '\0'; - len = 0; + str = new char[1]; + str[0] = '\0'; + len = 0; } xstring::xstring(const char *s) - :str(conv(s)), len(std::strlen(s)) {} + :str(conv(s)), len(std::strlen(s)) {} xstring::xstring(const xstring& s) { - if (!s.empty()) - { - str = conv(s.str); - len = s.len; - } - else clear(); + if (!s.empty()) + { + str = conv(s.str); + len = s.len; + } + else clear(); } xstring::~xstring() { - delete[] str; + delete[] str; } xstring xstring::operator= (const xstring& s) { - if (this == &s) return *this; - if (!s.empty()) - { - if (!empty()) delete[] str; - str = conv(s.str); - len = s.len; - } - else clear(); - return *this; + if (this == &s) return *this; + if (!s.empty()) + { + if (!empty()) delete[] str; + str = conv(s.str); + len = s.len; + } + else clear(); + return *this; } xstring xstring::operator= (const char *s) { - if (!empty()) delete[] str; - str = conv(s); - len = length(); - return *this; + if (!empty()) delete[] str; + str = conv(s); + len = length(); + return *this; } xstring xstring::operator+ (const xstring& s) { - return xstring(append(s)); + return xstring(append(s)); } xstring xstring::operator+ (const char *s) { - return xstring(append(s)); + return xstring(append(s)); } xstring xstring::operator+ (char c) { - return xstring(append(c)); + return xstring(append(c)); } xstring& xstring::operator+= (const xstring& s) { - append(s.str); - return *this; + append(s.str); + return *this; } xstring& xstring::operator+= (const char *s) { - append(s); - return *this; + append(s); + return *this; } xstring& xstring::operator+= (char c) { - push_back(c); - return *this; + push_back(c); + return *this; } char& xstring::operator[] (std::size_t i) const { - if (i >= len) throw std::runtime_error("Out of bounds."); - return str[i]; + if (i >= len) throw std::runtime_error("Out of bounds."); + return str[i]; } std::ostream& operator<< (std::ostream& stream, const xstring& s) { - return stream << s.str; + return stream << s.str; } std::istream& operator>> (std::istream& stream, const xstring& s) { - return stream >> s.str; + return stream >> s.str; } xstring& xstring::append(const xstring& s) { - if (!s.empty()) - { - resize(s.len); - std::strcat(str, s.str); - } - len = length(); - return *this; + if (!s.empty()) + { + resize(s.len); + std::strcat(str, s.str); + } + len = length(); + return *this; } xstring& xstring::append(const char *s) { - if (!strempty(s)) - { - resize(std::strlen(s)); - std::strcat(str, s); - } - len = length(); - return *this; + if (!strempty(s)) + { + resize(std::strlen(s)); + std::strcat(str, s); + } + len = length(); + return *this; } xstring& xstring::append(char c) { - push_back(c); - return *this; + push_back(c); + return *this; } xstring& xstring::insert(const xstring& s, std::size_t i) { - if (!s.empty() && i < len) - { - char *tmp1 = new char[i + 1]; - char *tmp2 = new char[len - i + 1]; - std::copy(str, str + i + 1, tmp1); - std::copy(str + i, str + len + 1, tmp2); - resize(s.len); - std::copy(tmp1, tmp1 + i + 1, str); - std::copy(s.str, s.str + s.len + 1, str + i); - std::copy(tmp2, tmp2 + len - i + 1, str + s.len + i); - len = length(); - str[len] = '\0'; - delete[] tmp1; - delete[] tmp2; - } - return *this; + if (!s.empty() && i < len) + { + char *tmp1 = new char[i + 1]; + char *tmp2 = new char[len - i + 1]; + std::copy(str, str + i + 1, tmp1); + std::copy(str + i, str + len + 1, tmp2); + resize(s.len); + std::copy(tmp1, tmp1 + i + 1, str); + std::copy(s.str, s.str + s.len + 1, str + i); + std::copy(tmp2, tmp2 + len - i + 1, str + s.len + i); + len = length(); + str[len] = '\0'; + delete[] tmp1; + delete[] tmp2; + } + return *this; } xstring& xstring::insert(const char *s, std::size_t i) { - if (!strempty(s) && i < len) - { - char *tmp1 = new char[i + 1]; - char *tmp2 = new char[len - i + 1]; - std::copy(str, str + i + 1, tmp1); - std::copy(str + i, str + len + 1, tmp2); - resize(std::strlen(s)); - std::copy(tmp1, tmp1 + i + 1, str); - 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; - delete[] tmp2; - } - return *this; + if (!strempty(s) && i < len) + { + char *tmp1 = new char[i + 1]; + char *tmp2 = new char[len - i + 1]; + std::copy(str, str + i + 1, tmp1); + std::copy(str + i, str + len + 1, tmp2); + resize(std::strlen(s)); + std::copy(tmp1, tmp1 + i + 1, str); + 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; + delete[] tmp2; + } + return *this; } void xstring::push_back(char c) { - resize(1); - str[len] = c; - str[len+1] = '\0'; - len = length(); + resize(1); + str[len] = c; + str[len+1] = '\0'; + len = length(); } void xstring::pop_back() { - if (len - 1 > 0) - { - char *tmp = new char[len]; - std::copy(str, str + len, tmp); - delete[] str; - tmp[len-1] = '\0'; - str = tmp; - len--; - } - else return; + if (len - 1 > 0) + { + char *tmp = new char[len]; + std::copy(str, str + len, tmp); + delete[] str; + tmp[len-1] = '\0'; + str = tmp; + len--; + } + else return; } void xstring::replace(std::size_t i, char c) { - if (i < len) str[i] = c; + if (i < len) str[i] = c; } void xstring::clear() { - if (!this->empty()) delete[] str; - str = new char[1]; - str[0] = '\0'; - len = 0; + if (!this->empty()) delete[] str; + str = new char[1]; + str[0] = '\0'; + len = 0; } bool xstring::find(const xstring& s) const { - return (std::strstr(this->cstr(), s.cstr()) != nullptr); + return (std::strstr(this->cstr(), s.cstr()) != nullptr); } bool xstring::find(const char *s) const { - return (std::strstr(this->cstr(), s) != nullptr); + return (std::strstr(this->cstr(), s) != nullptr); } void xstring::resize(std::size_t n) { - if (!this->empty()) - { - std::size_t l = len + n; - char *tmp = new char[l + 1]; - std::copy(str, str + len + 1, tmp); - delete[] str; - tmp[l] = '\0'; - str = tmp; - len = length(); - } + if (!this->empty()) + { + std::size_t l = len + n; + char *tmp = new char[l + 1]; + std::copy(str, str + len + 1, tmp); + delete[] str; + tmp[l] = '\0'; + str = tmp; + len = length(); + } } char * xstring::conv(const char *s) const { - std::size_t l = std::strlen(s); - char *tmp = new char[l + 1]; - std::copy(s, s + l+1, tmp); - tmp[l] = '\0'; - return tmp; + std::size_t l = std::strlen(s); + char *tmp = new char[l + 1]; + std::copy(s, s + l+1, tmp); + tmp[l] = '\0'; + return tmp; } std::istream& getline(std::istream& stream, xstring& s, char delim) { - char c; - s.clear(); - while (stream.get(c) && c != '\n') - { - if (c == delim) break; - else s.push_back(c); - } - return stream; + char c; + s.clear(); + while (stream.get(c) && c != '\n') + { + if (c == delim) break; + else s.push_back(c); + } + return stream; } } diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -9,75 +9,75 @@ namespace lab { class xstring { - private: - char *str; - std::size_t len; + private: + char *str; + std::size_t len; - public: - xstring(); - xstring(const char *s); - xstring(const xstring& s); - ~xstring(); - - xstring operator= (const xstring& s); - xstring operator= (const char *s); - xstring operator+ (const xstring& s); - xstring operator+ (char c); - xstring operator+ (const char *s); - xstring& operator+= (const xstring& s); - xstring& operator+= (const char *s); - xstring& operator+= (char c); - inline constexpr bool operator== (const xstring& s) const - {return std::strcmp(this->str, s.str) == 0;} - inline constexpr bool operator== (const char *s) const - {return std::strcmp(this->str, s) == 0;} - inline constexpr bool operator!= (const xstring& s) const - {return std::strcmp(this->str, s.str) != 0;} - inline constexpr bool operator!= (const char *s) const - {return std::strcmp(this->str, s) != 0;} - inline constexpr bool operator< (const xstring& s) const - {return std::strcmp(this->str, s.str) < 0;} - inline constexpr bool operator< (const char *s) const - {return std::strcmp(this->str, s) < 0;} - inline constexpr bool operator<= (const xstring& s) const - {return std::strcmp(this->str, s.str) <= 0;} - inline constexpr bool operator<= (const char *s) const - {return std::strcmp(this->str, s) <= 0;} - inline constexpr bool operator> (const xstring& s) const - {return std::strcmp(this->str, s.str) > 0;} - inline constexpr bool operator> (const char *s) const - {return std::strcmp(this->str, s) > 0;} - inline constexpr bool operator>= (const xstring& s) const - {return std::strcmp(this->str, s.str) >= 0;} - inline constexpr bool operator>= (const char *s) const - {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); + public: + xstring(); + xstring(const char *s); + xstring(const xstring& s); + ~xstring(); + + xstring operator= (const xstring& s); + xstring operator= (const char *s); + xstring operator+ (const xstring& s); + xstring operator+ (char c); + xstring operator+ (const char *s); + xstring& operator+= (const xstring& s); + xstring& operator+= (const char *s); + xstring& operator+= (char c); + inline constexpr bool operator== (const xstring& s) const + {return std::strcmp(this->str, s.str) == 0;} + inline constexpr bool operator== (const char *s) const + {return std::strcmp(this->str, s) == 0;} + inline constexpr bool operator!= (const xstring& s) const + {return std::strcmp(this->str, s.str) != 0;} + inline constexpr bool operator!= (const char *s) const + {return std::strcmp(this->str, s) != 0;} + inline constexpr bool operator< (const xstring& s) const + {return std::strcmp(this->str, s.str) < 0;} + inline constexpr bool operator< (const char *s) const + {return std::strcmp(this->str, s) < 0;} + inline constexpr bool operator<= (const xstring& s) const + {return std::strcmp(this->str, s.str) <= 0;} + inline constexpr bool operator<= (const char *s) const + {return std::strcmp(this->str, s) <= 0;} + inline constexpr bool operator> (const xstring& s) const + {return std::strcmp(this->str, s.str) > 0;} + inline constexpr bool operator> (const char *s) const + {return std::strcmp(this->str, s) > 0;} + inline constexpr bool operator>= (const xstring& s) const + {return std::strcmp(this->str, s.str) >= 0;} + inline constexpr bool operator>= (const char *s) const + {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); - xstring& append(const xstring& s); - xstring& append(const char *s); - 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; - inline constexpr char *cstr() const {return str;} - inline constexpr char& front() const {return str[0];} - inline constexpr char& back() const - {return (!this->empty()) ? str[len-1] : str[0];}; - inline constexpr bool empty() const {return len == 0;} - inline constexpr std::size_t length() const {return std::strlen(str);} - - private: - void resize(std::size_t n); - char *conv(const char *s) const; - inline constexpr bool strempty(const char *s) const - {return std::strlen(s) == 0;} + xstring& append(const xstring& s); + xstring& append(const char *s); + 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; + inline constexpr char *cstr() const {return str;} + inline constexpr char& front() const {return str[0];} + inline constexpr char& back() const + {return (!this->empty()) ? str[len-1] : str[0];}; + inline constexpr bool empty() const {return len == 0;} + inline constexpr std::size_t length() const {return std::strlen(str);} + + private: + void resize(std::size_t n); + char *conv(const char *s) const; + inline constexpr bool strempty(const char *s) const + {return std::strlen(s) == 0;} }; std::istream& getline(std::istream& stream, xstring& s, char delim = '\n'); @@ -88,33 +88,33 @@ template<typename T> constexpr const char *getformat(); template<typename T> xstring to_xstr(T val) { - char buf[25]; - std::snprintf(buf, sizeof(buf), getformat<T>(), val); - return xstring(buf); + char buf[25]; + std::snprintf(buf, sizeof(buf), getformat<T>(), val); + return xstring(buf); } template<typename T> xstring to_xstr(const char *fs, T val) { - char buf[25]; - std::snprintf(buf, sizeof(buf), fs, val); - return xstring(buf); + char buf[25]; + std::snprintf(buf, sizeof(buf), fs, val); + return xstring(buf); } template<typename T> constexpr const char * getformat() { - if constexpr (std::is_same_v<T, short>) return "%hi"; - if constexpr (std::is_same_v<T, int>) return "%d"; - if constexpr (std::is_same_v<T, long>) return "%ld"; - if constexpr (std::is_same_v<T, long long>) return "%lld"; - if constexpr (std::is_same_v<T, unsigned short>) return "%hu"; - if constexpr (std::is_same_v<T, unsigned int>) return "%u"; - if constexpr (std::is_same_v<T, unsigned long>) return "%lu"; - if constexpr (std::is_same_v<T, unsigned long>) return "%llu"; - if constexpr (std::is_same_v<T, float>) return "%f"; - if constexpr (std::is_same_v<T, double>) return "%f"; - if constexpr (std::is_same_v<T, long double>) return "%Lf"; + if constexpr (std::is_same_v<T, short>) return "%hi"; + if constexpr (std::is_same_v<T, int>) return "%d"; + if constexpr (std::is_same_v<T, long>) return "%ld"; + if constexpr (std::is_same_v<T, long long>) return "%lld"; + if constexpr (std::is_same_v<T, unsigned short>) return "%hu"; + if constexpr (std::is_same_v<T, unsigned int>) return "%u"; + if constexpr (std::is_same_v<T, unsigned long>) return "%lu"; + if constexpr (std::is_same_v<T, unsigned long>) return "%llu"; + if constexpr (std::is_same_v<T, float>) return "%f"; + if constexpr (std::is_same_v<T, double>) return "%f"; + if constexpr (std::is_same_v<T, long double>) return "%Lf"; } }