uni

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

commit 24335e2a2e0d0fa0ba5b20028fd4786dc07ae0f2
parent 90508c18f2610713acaa069244ffa9ccc5f03c72
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue, 17 Mar 2020 20:36:38 +0200

improvements

Diffstat:
MREADME.md | 3+--
Massignment-2.2-classes/classes.cpp | 79+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
2 files changed, 48 insertions(+), 34 deletions(-)

diff --git a/README.md b/README.md @@ -1,2 +1,2 @@ # uni-assignments -**Format:** ```assignment-x.y-z``` (```x``` = semester, ```y``` = number of assignment, ```z``` = subject).- \ No newline at end of file +**Format:** ```assignment-x.y-z``` (```x``` = semester, ```y``` = number of assignment, ```z``` = subject). diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp @@ -50,18 +50,16 @@ class Student char *convert_AM(const char *AM) { int len = strlen(AM); - this->AM = new char[len]; - for (int i = 0; i < len && len; i++) - this->AM[i] = AM[i]; + this->AM = new char[len+1]; + std::copy(AM, AM+len, this->AM); return this->AM; } float *convert_PSG(const float *grades) { this->grades = new float[psubj]; - for (int i = 0; i < psubj && psubj; i++) - this->grades[i] = grades[i]; - return this->grades; + std::copy(grades, grades+psubj, this->grades); + return this->grades; } void add_grade(float grade) @@ -96,49 +94,51 @@ class Student int main(int argc, char **argv) { Student *s1 = new Student("19390133", "Christos Margiolis"); - std::cout << "Constructor for s1 (AM, Name)" << std::endl << "----------------------------" << std::endl; + 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::cout << "s1->get_psubj(): " << s1->get_psubj() << std::endl << std::endl; delete s1; - std::cout << std::endl; - Student *s2 = new Student("19390133", "Christos Margiolis", 2); - std::cout << "Constructor for s2 (AM, Name, Semester)" << std::endl << "----------------------------" << std::endl; + 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::cout << std::endl; + std::cout << "s2->get_psubj(): " << s2->get_psubj() << std::endl << std::endl; Student *copystud = new Student(*s2); - std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl << "----------------------------" << std::endl; + 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::cout << "copystud->get_psubj(): " << copystud->get_psubj() << std::endl << std::endl; delete s2; - std::cout << std::endl; - float grd[4] = {9.4f, 8.4f, 5.5f, 6.3f}; Student *s3 = new Student("19390133", "Christos Margiolis", 2, 4, grd); - std::cout << "Constructor for s3 (AM, Name, Semester, Subjects Passed, Grades)" << std::endl << "----------------------------" << std::endl; + 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 *gr1 = s3->get_grades(); - std::cout << "s3->get_grades(): "; // new line? - for (int i = 0; i < s3->get_psubj(); i++) - std::cout << gr1[i] << ", "; - std::cout << std::endl; - std::cout << std::endl; - std::cout << "Detailed print of s3's grades" << std::endl << "----------------------------" << std::endl; + float *gr = s3->get_grades(); + std::cout << "s3->get_grades(): "; + for (int i = 0; i < s3->get_psubj(); i++) + { + std::cout << gr[i] << ", "; + if (i == s3->get_psubj()-1) + std::cout << gr[i] << std::endl << std::endl; + } + + std::cout << "Detailed print of s3's grades" << std::endl; + std::cout << "----------------------------" << std::endl; s3->detailed_print(); s3->set_AM("01010101"); @@ -149,8 +149,8 @@ int main(int argc, char **argv) s3->set_grades(gg); std::cout << std::endl; - - std::cout << "Setters example using s3" << std::endl << "----------------------------" << 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'; @@ -159,12 +159,27 @@ int main(int argc, char **argv) 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 *gr2 = s3->get_grades(); + + gr = s3->get_grades(); std::cout << "Input: {0.1f, 2.2f}" << '\t' << '\t'; - std::cout << "Output: s3->get_grades(): "; // new line? + std::cout << "Output: s3->get_grades(): "; for (int i = 0; i < s3->get_psubj(); i++) - std::cout << gr2[i] << ", "; - std::cout << std::endl; + { + std::cout << gr[i] << ", "; + if (i == s3->get_psubj()-1) + 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++) + { + std::cout << gr[i] << ", "; + if (i == s3->get_psubj()-1) + std::cout << gr[i] << std::endl; + } delete s3; return 0;