uni

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

commit 314bf188303c465b1010cd7b98e1c6f6d4cfd759
parent a82ea3c6e7a65f213e4b87afd6dcfa24b8d48ade
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon,  1 Jun 2020 01:23:29 +0300

templated some functions

Diffstat:
Massignment-2.2-classes/classes.cpp | 84+++++++++++++++++++++++++++++++++++++++----------------------------------------
Massignment-2.3-operoverloading/bin/operoverloading | 0
Massignment-2.3-operoverloading/obj/course.o | 0
Massignment-2.3-operoverloading/obj/main.o | 0
Massignment-2.3-operoverloading/obj/student.o | 0
Massignment-2.3-operoverloading/src/course.cpp | 7++-----
Massignment-2.3-operoverloading/src/course.h | 13+++++++------
Massignment-2.3-operoverloading/src/student.cpp | 68+++++++++++++++++---------------------------------------------------
Massignment-2.3-operoverloading/src/student.h | 49+++++++++++++++++++++++++++++++++++++------------
Massignment-2.4-inheritance/src/game.h | 2+-
Massignment-2.4-inheritance/src/office.h | 2+-
Massignment-2.5-spreadsheets/bin/spreadsheets | 0
Massignment-2.5-spreadsheets/obj/datahandler.o | 0
Massignment-2.5-spreadsheets/obj/main.o | 0
Massignment-2.5-spreadsheets/obj/xstring.o | 0
Massignment-2.5-spreadsheets/src/course.h | 3+++
Massignment-2.5-spreadsheets/src/datahandler.cpp | 2+-
Massignment-2.5-spreadsheets/src/student.h | 6++++++
Massignment-2.5-spreadsheets/src/xstring.cpp | 12++++++++++++
Massignment-2.5-spreadsheets/src/xstring.h | 30++++++++++++++++++------------
20 files changed, 146 insertions(+), 132 deletions(-)

diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp @@ -41,31 +41,32 @@ class Student void detailed_print() const; private: - char *convert_id (const char *id); - float *convert_PSG(const float *grades); - float calc_average() const; + template<typename T> T *conv(const T *arr, std::size_t len) const; + template<typename T> T *resize(const T *arr, std::size_t len); + constexpr std::size_t len(const char *s) const {return std::strlen(s) + 1;} + float avg() const; }; Student::Student(const char *id, const std::string& name) - :id(convert_id(id)), name(name), semester(1), pcourses(0), grades(nullptr) {} + :id(conv<char>(id, len(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) {} + :id(conv<char>(id, len(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)) {} + :id(conv<char>(id, len(id))), name(name), semester(semester), pcourses(pcourses), + grades(conv<float>(grades, pcourses)) {} Student::Student(const Student& s) :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); + this->id = conv<char>(s.id, len(s.id)); + this->grades = conv<float>(s.grades, s.pcourses); } Student::~Student() @@ -92,7 +93,7 @@ void Student::set_id(const char *id) { if (this->id != nullptr) delete[] this->id; - this->id = convert_id(id); + this->id = conv<char>(id, len(id)); } void @@ -116,40 +117,13 @@ Student::set_pcourses(const unsigned int pcourses) void Student::set_grades(const float *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; -} - -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; + this->grades = conv<float>(grades, pcourses); } 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; - } + float *tmp = resize<float>(grades, pcourses); tmp[pcourses] = grade; grades = tmp; pcourses++; @@ -165,12 +139,12 @@ Student::detailed_print() const std::cout << "Subject " << i+1 << ": "; std::cout << grades[i] << std::endl; } - std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl; + std::cout << "Average grade: " << std::setprecision(2) << avg() << std::endl; } } float -Student::calc_average() const +Student::avg() const { if (grades != nullptr) { @@ -183,6 +157,30 @@ Student::calc_average() const else return 0.0f; } +template<typename T> T * +Student::conv(const T *arr, std::size_t len) const +{ + if (arr != nullptr) + { + T *tmp = new T[len]; + std::memcpy(tmp, arr, sizeof(T) * len); + return tmp; + } + else return nullptr; +} + +template<typename T> T * +Student::resize(const T *arr, std::size_t len) +{ + T *tmp = new T[len+1]; + if (arr != nullptr) + { + std::memcpy(tmp, arr, sizeof(T) * len); + delete[] arr; + } + return tmp; +} + static std::ostream& operator<< (std::ostream& stream, const Student& s); static void cont(); static void constructor1(const Student& s1); diff --git a/assignment-2.3-operoverloading/bin/operoverloading b/assignment-2.3-operoverloading/bin/operoverloading Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/course.o b/assignment-2.3-operoverloading/obj/course.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/main.o b/assignment-2.3-operoverloading/obj/main.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/student.o b/assignment-2.3-operoverloading/obj/student.o Binary files differ. diff --git a/assignment-2.3-operoverloading/src/course.cpp b/assignment-2.3-operoverloading/src/course.cpp @@ -1,8 +1,5 @@ #include "course.h" - -Course::Course() {} - Course::Course(const std::string& code, const std::string& cname, unsigned int csemester) :code(code), cname(cname), csemester(csemester) {} @@ -12,13 +9,13 @@ Course::Course(const Course& c) const std::string& Course::get_code() const { - return this->code; + return code; } const std::string& Course::get_cname() const { - return this->cname; + return cname; } void diff --git a/assignment-2.3-operoverloading/src/course.h b/assignment-2.3-operoverloading/src/course.h @@ -7,13 +7,14 @@ class Course { private: - std::string code; // Course code - std::string cname; // Course name - unsigned int csemester; // Corresponding semester of course + 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() = default; + Course(const std::string& code, const std::string& cname, + const unsigned int csemester); Course(const Course& s); const std::string& get_code() const; @@ -22,7 +23,7 @@ class Course void set_code(const std::string& code); void set_cname(const std::string& cname); - void set_csemester(unsigned int csemester); + void set_csemester(const unsigned int csemester); }; #endif /* COURSE_H */ diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -1,29 +1,26 @@ #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(conv<char>(id, len(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) {} + :id(conv<char>(id, len(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) {} + :id(conv<char>(id, len(id))), name(name), semester(semester), + pcourses(pcourses), grades(conv<float>(grades, pcourses)), + 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); - + this->id = conv<char>(s.id, len(s.id)); + this->grades = conv<float>(s.grades, s.pcourses); if (s.nsc <= 0) { nsc = 0; @@ -58,12 +55,7 @@ Student::~Student() void Student::operator+= (Course *c) { - Course **tmp = new Course *[nsc+1]; - if (sc != nullptr) - { - std::memcpy(tmp, sc, sizeof(Course *) * nsc); - delete[] sc; - } + Course **tmp = resize<Course *>(sc, nsc); tmp[nsc] = c; sc = tmp; nsc++; @@ -73,11 +65,12 @@ Student Student::operator= (const Student& s) { if (this == &s) return *this; - this->id = convid(s.id); + this->id = conv<char>(s.id, len(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.grades != nullptr) + this->grades = conv<float>(s.grades, s.pcourses); if (s.sc != nullptr) { this->nsc = s.nsc; @@ -89,14 +82,14 @@ Student::operator= (const Student& s) const std::string& Student::get_name() const { - return this->name; + return name; } void Student::set_id(const char *id) { if (this->id != nullptr) delete[] this->id; - this->id = convid(id); + this->id = conv<char>(id, len(id)); } void @@ -120,7 +113,7 @@ Student::set_pcourses(const unsigned int pcourses) void Student::set_grades(const float *grades) { - this->grades = convpsg(grades); + this->grades = conv<float>(grades, pcourses); } void @@ -140,37 +133,10 @@ Student::set_submitted_courses(Course **sc) } } -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; -} - -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; -} - 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; - } + float *tmp = resize<float>(grades, pcourses); tmp[pcourses] = grade; grades = tmp; pcourses++; diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -11,14 +11,14 @@ 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 + char *id; // id + std::string name; // Name + unsigned int semester; // Current semester + unsigned int pcourses; // Passed courses + float *grades; // Grades + std::size_t size; // Grades array size + Course **sc; // Submitted courses + unsigned int nsc; // Number of submitted courses public: Student(const char *id, const std::string& name); @@ -45,7 +45,7 @@ class Student 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 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); @@ -60,9 +60,34 @@ class Student void detailed_print() const; private: - char *convid(const char *id); - float *convpsg(const float *grades); - float avg() const; + template<typename T> T *conv(const T *arr, std::size_t len) const; + template<typename T> T *resize(const T *arr, std::size_t len); + constexpr std::size_t len(const char *s) const {return std::strlen(s) + 1;} + float avg() const; }; +template<typename T> T * +Student::conv(const T *arr, std::size_t len) const +{ + if (arr != nullptr) + { + T *tmp = new T[len]; + std::memcpy(tmp, arr, sizeof(T) * len); + return tmp; + } + else return nullptr; +} + +template<typename T> T * +Student::resize(const T *arr, std::size_t len) +{ + T *tmp = new T[len+1]; + if (arr != nullptr) + { + std::memcpy(tmp, arr, sizeof(T) * len); + delete[] arr; + } + return tmp; +} + #endif /* STUDENT_H */ diff --git a/assignment-2.4-inheritance/src/game.h b/assignment-2.4-inheritance/src/game.h @@ -3,7 +3,7 @@ #include "app.h" -class Game: public App +class Game final: public App { private: std::string genre; diff --git a/assignment-2.4-inheritance/src/office.h b/assignment-2.4-inheritance/src/office.h @@ -3,7 +3,7 @@ #include "app.h" -class Office: public App +class Office final: public App { private: std::vector<std::string> extensions; diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/datahandler.o b/assignment-2.5-spreadsheets/obj/datahandler.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/main.o b/assignment-2.5-spreadsheets/obj/main.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/xstring.o b/assignment-2.5-spreadsheets/obj/xstring.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/src/course.h b/assignment-2.5-spreadsheets/src/course.h @@ -10,6 +10,9 @@ struct Course Course(const lab::xstring& code, const lab::xstring& name) :code(code), name(name) {} + + Course(lab::xstring&& code, lab::xstring&& name) + :code(std::move(code)), name(std::move(name)) {} }; #endif /* COURSE_H */ diff --git a/assignment-2.5-spreadsheets/src/datahandler.cpp b/assignment-2.5-spreadsheets/src/datahandler.cpp @@ -208,7 +208,7 @@ DataHandler::valid_path(const char *fpath) const return (std::strstr(fpath, ".csv") != nullptr); } -const lab::xstring +const lab::xstring DataHandler::err_csv(const char *fpath) const { return lab::xstring("Error. File must be of format \'.csv\'. ()"). diff --git a/assignment-2.5-spreadsheets/src/student.h b/assignment-2.5-spreadsheets/src/student.h @@ -13,6 +13,12 @@ struct Student const lab::xstring& lname, const lab::xstring& fname) :id(id), lname(lname), fname(fname) {} + + Student(lab::xstring&& id, + lab::xstring&& lname, + lab::xstring&& fname) + :id(std::move(id)), lname(std::move(lname)), + fname(std::move(fname)) {} }; #endif /* STUDENT_H */ diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -21,6 +21,18 @@ xstring::xstring(const xstring& s) else clear(); } +xstring::xstring(xstring&& s) noexcept +{ + if (!s.empty()) + { + str = s.str; + len = s.len; + s.str = nullptr; + s.len = 0; + } + else clear(); +} + xstring::~xstring() { delete[] str; diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -17,6 +17,7 @@ class xstring xstring(); xstring(const char *s); xstring(const xstring& s); + xstring(xstring&& s) noexcept; ~xstring(); xstring operator= (const xstring& s); @@ -69,8 +70,8 @@ class xstring constexpr char *cstr() const {return str;} constexpr char& front() const {return str[0];} constexpr char& back() const - {return (!this->empty()) ? str[len-1] : str[0];}; - constexpr bool empty() const {return len == 0;} + {return (!empty()) ? str[len-1] : str[0];}; + constexpr bool empty() const {return len == 0;} constexpr std::size_t length() const {return std::strlen(str);} private: @@ -104,17 +105,22 @@ to_xstr(const char *fs, T val) 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, char>) return "%c"; + if constexpr (std::is_same_v<T, unsigned char>) return "%c"; + 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, unsigned>) return "%u"; + 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, void *>) return "%p"; + if constexpr (std::is_same_v<T, char *>) return "%s"; } }