uni

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

commit 6adae5b189f33f5f922cba276429295de5814548
parent 2008c67cc40d68d6bf3d12c9646fb5522f8fe546
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu,  2 Apr 2020 08:13:28 +0300

edited 2.2 and 2.3, pending bug 2.3 bug fix

Diffstat:
Massignment-2.2-classes/classes.cpp | 176++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
Massignment-2.3-operoverloading/bin/operoverloading | 0
Massignment-2.3-operoverloading/obj/main.o | 0
Massignment-2.3-operoverloading/obj/student.o | 0
Massignment-2.3-operoverloading/src/main.cpp | 27+++++++++++++++++----------
Massignment-2.3-operoverloading/src/student.cpp | 13++++++++-----
Massignment-2.3-operoverloading/src/student.h | 4++--
7 files changed, 138 insertions(+), 82 deletions(-)

diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp @@ -58,8 +58,8 @@ class Student char *convert_AM(const char *AM); float *convert_PSG(const float *grades); void add_grade(float grade); - void detailed_print(); - float calc_average(); + void detailed_print() const; + float calc_average() const; }; char *Student::convert_AM(const char *AM) @@ -80,15 +80,18 @@ float *Student::convert_PSG(const float *grades) void Student::add_grade(float grade) { float *tmp = new float[psubj+1]; - std::copy(this->grades, this->grades+psubj, tmp); + std::copy(grades, grades+psubj, tmp); tmp[psubj] = grade; - this->grades = tmp; + delete[] grades; + grades = new float[psubj+1]; + std::copy(tmp, tmp+psubj+1, grades); psubj++; + delete[] tmp; } -void Student::detailed_print() +void Student::detailed_print() const { - for (int i = 0; i < psubj; i++) + for (unsigned int i = 0; i < psubj; i++) { std::cout << "Subject " << i+1 << ": "; std::cout << grades[i] << std::endl; @@ -96,103 +99,146 @@ void Student::detailed_print() std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl; } -float Student::calc_average() +float Student::calc_average() const { float sum = 0; - for (int i = 0; i < psubj; i++) + for (unsigned int i = 0; i < psubj; i++) sum += grades[i]; float average = sum / psubj; return average; } +void cont(); +void constructor1(const Student& s1); +void constructor2(const Student& s2); +void copy_constructor(const Student& copystud); +void constructor3(const Student& s3); +void detprint (const Student& s3); +void setters(Student& s3); + int main(int argc, char **argv) { Student *s1 = new Student("12345678", "Name Surname"); - std::cout << "Constructor for s1 (AM, Name)" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "s1->get_AM(): " << s1->get_AM() << std::endl; - std::cout << "s1->get_name(): " << s1->get_name() << std::endl; - std::cout << "s1->get_semester(): " << s1->get_semester() << std::endl; - std::cout << "s1->get_psubj(): " << s1->get_psubj() << std::endl << std::endl; + system("clear"); + constructor1(*s1); cont(); delete s1; Student *s2 = new Student("12345678", "Name Surname", 2); - std::cout << "Constructor for s2 (AM, Name, Semester)" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "s2->get_AM(): " << s2->get_AM() << std::endl; - std::cout << "s2->get_name(): " << s2->get_name() << std::endl; - std::cout << "s2->get_semester(): " << s2->get_semester() << std::endl; - std::cout << "s2->get_psubj(): " << s2->get_psubj() << std::endl << std::endl; + constructor2(*s2); cont(); Student *copystud = new Student(*s2); - std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "copystud->get_AM(): " << copystud->get_AM() << std::endl; - std::cout << "copystud->get_name(): " << copystud->get_name() << std::endl; - std::cout << "copystud->get_semester(): " << copystud->get_semester() << std::endl; - std::cout << "copystud->get_psubj(): " << copystud->get_psubj() << std::endl << std::endl; + copy_constructor(*copystud); cont(); delete copystud; delete s2; float grd[4] = {9.4f, 8.4f, 5.5f, 6.3f}; Student *s3 = new Student("12345678", "Name Surname", 2, 4, grd); + constructor3(*s3); cont(); + detprint(*s3); cont(); + setters(*s3); cont(); + delete s3; + + return 0; +} + +void cont() +{ + std::cout << std::endl; + std::cout << "Press <ENTER> to continue. . ."; + if (std::cin.get()) system("clear"); +} + +void constructor1(const Student& s1) +{ + std::cout << "Constructor for s1 (AM, Name)" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "s1.get_AM(): " << s1.get_AM() << std::endl; + std::cout << "s1.get_name(): " << s1.get_name() << std::endl; + std::cout << "s1.get_semester(): " << s1.get_semester() << std::endl; + std::cout << "s1.get_psubj(): " << s1.get_psubj() << std::endl << std::endl; +} + +void constructor2(const Student& s2) +{ + std::cout << "Constructor for s2 (AM, Name, Semester)" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "s2.get_AM(): " << s2.get_AM() << std::endl; + std::cout << "s2.get_name(): " << s2.get_name() << std::endl; + std::cout << "s2.get_semester(): " << s2.get_semester() << std::endl; + std::cout << "s2.get_psubj(): " << s2.get_psubj() << std::endl << 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 << "copystud.get_AM(): " << copystud.get_AM() << std::endl; + std::cout << "copystud.get_name(): " << copystud.get_name() << std::endl; + std::cout << "copystud.get_semester(): " << copystud.get_semester() << std::endl; + std::cout << "copystud.get_psubj(): " << copystud.get_psubj() << std::endl << std::endl; +} + +void constructor3(const Student& s3) +{ std::cout << "Constructor for s3 (AM, Name, Semester, Subjects Passed, Grades)" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "s3->get_AM(): " << s3->get_AM() << std::endl; - std::cout << "s3->get_name(): " << s3->get_name() << std::endl; - std::cout << "s3->get_semester(): " << s3->get_semester() << std::endl; - std::cout << "s3->get_psubj(): " << s3->get_psubj() << std::endl; - - float *gr = s3->get_grades(); - std::cout << "s3->get_grades(): "; - for (int i = 0; i < s3->get_psubj(); i++) + std::cout << "s3.get_AM(): " << s3.get_AM() << std::endl; + std::cout << "s3.get_name(): " << s3.get_name() << std::endl; + std::cout << "s3.get_semester(): " << s3.get_semester() << std::endl; + std::cout << "s3.get_psubj(): " << s3.get_psubj() << std::endl; + + float *gr = s3.get_grades(); + std::cout << "s3.get_grades(): "; + for (unsigned int i = 0; i < s3.get_psubj(); i++) { - if (i != s3->get_psubj()-1) std::cout << gr[i] << ", "; + if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl << std::endl; } +} +void detprint (const Student& s3) +{ std::cout << "Detailed print of s3's grades" << std::endl; std::cout << "----------------------------" << std::endl; - s3->detailed_print(); + s3.detailed_print(); +} - s3->set_AM("01010101"); - s3->set_name("AAAAAAA"); - s3->set_semester(100); - s3->set_psubj(2); // πρώτα η set_psubj() και μετα η set_grades() !!!!! +void setters(Student& s3) +{ + s3.set_AM("01010101"); + s3.set_name("AAAAAAA"); + s3.set_semester(100); + s3.set_psubj(2); // πρώτα η set_psubj() και μετα η set_grades() !!!!! float gg[2] = {0.1f, 2.2f}; - s3->set_grades(gg); + s3.set_grades(gg); - std::cout << std::endl; std::cout << "Setters example using s3" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "Input: s3->set_AM(\"01010101\")" << '\t'; - std::cout << "Output: s3->get_AM(): " << s3->get_AM() << std::endl; - std::cout << "Input: s3->set_name(\"AAAAAAA\")" << '\t'; - std::cout << "Output: s3->get_name(): " << s3->get_name() << std::endl; - std::cout << "Input: s3->set_semester(100):" << '\t'; - std::cout << "Output: s3->get_semester(): " << s3->get_semester() << std::endl; - std::cout << "Input: s3->set_psubj(2):" << '\t'; - std::cout << "Output: s3->get_psubj(): " << s3->get_psubj() << std::endl; - - gr = s3->get_grades(); + std::cout << "Input: s3.set_AM(\"01010101\")" << '\t'; + std::cout << "Output: s3.get_AM(): " << s3.get_AM() << std::endl; + std::cout << "Input: s3.set_name(\"AAAAAAA\")" << '\t'; + std::cout << "Output: s3.get_name(): " << s3.get_name() << std::endl; + std::cout << "Input: s3.set_semester(100):" << '\t'; + std::cout << "Output: s3.get_semester(): " << s3.get_semester() << std::endl; + std::cout << "Input: s3.set_psubj(2):" << '\t'; + std::cout << "Output: s3.get_psubj(): " << s3.get_psubj() << std::endl; + + float *gr = new float[s3.get_psubj()]; + gr = s3.get_grades(); std::cout << "Input: {0.1f, 2.2f}" << '\t' << '\t'; - std::cout << "Output: s3->get_grades(): "; - for (int i = 0; i < s3->get_psubj(); i++) + std::cout << "Output: s3.get_grades(): "; + for (unsigned int i = 0; i < s3.get_psubj(); i++) { - if (i != s3->get_psubj()-1) std::cout << gr[i] << ", "; + if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl; } - - s3->add_grade(7.5f); - gr = s3->get_grades(); - std::cout << "Input: s3->add_grade(7.5f)" << '\t'; - std::cout << "Output: s3->get_grades(): "; - for (int i = 0; i < s3->get_psubj(); i++) + s3.add_grade(7.5f); + gr = s3.get_grades(); + std::cout << "Input: s3.add_grade(7.5f)" << '\t'; + std::cout << "Output: s3.get_grades(): "; + for (unsigned int i = 0; i < s3.get_psubj(); i++) { - if (i != s3->get_psubj()-1) std::cout << gr[i] << ", "; + if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl; } - delete s3; - - return 0; } diff --git a/assignment-2.3-operoverloading/bin/operoverloading b/assignment-2.3-operoverloading/bin/operoverloading Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/main.o b/assignment-2.3-operoverloading/obj/main.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/student.o b/assignment-2.3-operoverloading/obj/student.o Binary files differ. diff --git a/assignment-2.3-operoverloading/src/main.cpp b/assignment-2.3-operoverloading/src/main.cpp @@ -119,7 +119,7 @@ void constructor3(const Student& s3) float *gr = s3.get_grades(); std::cout << "s3.get_grades(): "; - for (int i = 0; i < s3.get_psubj(); i++) + for (unsigned int i = 0; i < s3.get_psubj(); i++) { if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl << std::endl; @@ -142,10 +142,6 @@ void setters(Student& s3) float gg[2] = {0.1f, 2.2f}; s3.set_grades(gg); - std::string ss[3] = {"Math", "Physics", "Programming"}; - s3.set_num_submitted_subjects(3); - s3.set_submitted_subjects(ss); - std::cout << "Setters example using s3" << std::endl; std::cout << "----------------------------" << std::endl; std::cout << "Input: s3.set_AM(\"01010101\")" << '\t'; @@ -157,10 +153,11 @@ void setters(Student& s3) std::cout << "Input: s3.set_psubj(2):" << '\t'; std::cout << "Output: s3.get_psubj(): " << s3.get_psubj() << std::endl; - float *gr = s3.get_grades(); + float *gr = new float[s3.get_psubj()]; + gr = s3.get_grades(); std::cout << "Input: {0.1f, 2.2f}" << '\t' << '\t'; std::cout << "Output: s3.get_grades(): "; - for (int i = 0; i < s3.get_psubj(); i++) + for (unsigned int i = 0; i < s3.get_psubj(); i++) { if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl; @@ -169,7 +166,7 @@ void setters(Student& s3) gr = s3.get_grades(); std::cout << "Input: s3.add_grade(7.5f)" << '\t'; std::cout << "Output: s3.get_grades(): "; - for (int i = 0; i < s3.get_psubj(); i++) + for (unsigned int i = 0; i < s3.get_psubj(); i++) { if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl; @@ -179,14 +176,24 @@ void setters(Student& s3) std::cout << "Input: s3.set_num_submitted_subjects(3)" << std::endl;; std::cout << "Output: s3.get_num_submitted_subjects(): " << s3.get_num_submitted_subjects() << std::endl << std::endl; - std::string *ssj = s3.get_submitted_subjects(); + std::string ss[3] = {"Math", "Physics", "Programming"}; + s3.set_num_submitted_subjects(3); + s3.set_submitted_subjects(ss); + + std::string *ssj = new std::string[s3.get_num_submitted_subjects()]; + ssj = s3.get_submitted_subjects(); std::cout << "Input: {\"Math\", \"Physics\", \"Programming\"}" << std::endl;; std::cout << "Output: s3.get_submitted_subjects(): "; - for (int i = 0; i < s3.get_num_submitted_subjects(); i++) + for (unsigned int i = 0; i < s3.get_num_submitted_subjects(); i++) { if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i] << ", "; else std::cout << ssj[i] << std::endl; } + + gr = nullptr; + ssj = nullptr; + delete[] gr; + delete[] ssj; } void getters(const Subject& sb) diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -45,15 +45,18 @@ float *Student::convert_PSG(const float *grades) void Student::add_grade(float grade) { float *tmp = new float[psubj+1]; - std::copy(this->grades, this->grades+psubj, tmp); - tmp[this->psubj] = grade; - this->grades = tmp; + std::copy(grades, grades+psubj, tmp); + tmp[psubj] = grade; + delete[] grades; + grades = new float[psubj+1]; + std::copy(tmp, tmp+psubj+1, grades); psubj++; + delete[] tmp; } void Student::detailed_print() const { - for (int i = 0; i < psubj; i++) + for (unsigned int i = 0; i < psubj; i++) { std::cout << "Subject " << i+1 << ": "; std::cout << grades[i] << std::endl; @@ -64,7 +67,7 @@ void Student::detailed_print() const float Student::calc_average() const { float sum = 0; - for (int i = 0; i < psubj; i++) + for (unsigned int i = 0; i < psubj; i++) sum += grades[i]; float average = sum / psubj; return average; diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -40,7 +40,7 @@ class Student inline unsigned int get_psubj() const {return this->psubj;} inline float *get_grades() const {return this->grades;} inline unsigned int get_num_submitted_subjects() const {return this->numSubmittedSubjects;} - inline std::string *get_submitted_subjects() const {return this->submittedSubjects;} // const? + inline std::string *get_submitted_subjects() const {return this->submittedSubjects;} inline void set_AM(const char *AM) {this->AM = convert_AM(AM);} inline void set_name(const std::string& name) {this->name = name;} @@ -48,7 +48,7 @@ class Student inline void set_psubj(unsigned int psubj) {this->psubj = psubj;} inline void set_grades(float *grades) {this->grades = convert_PSG(grades);} inline void set_num_submitted_subjects(unsigned int numSubmittedSubjects) {this->numSubmittedSubjects = numSubmittedSubjects;} - inline void set_submitted_subjects(std::string* submittedSubjects) {this->submittedSubjects = submittedSubjects;} // reference?? + inline void set_submitted_subjects(std::string* submittedSubjects) {this->submittedSubjects = submittedSubjects;} inline void print_3first() const {std::cout << this->AM << " " << this->name << " " << this->semester << std::endl;} char *convert_AM(const char *AM);