commit 21b642f690479eb76100f87210b803b9eccd430c parent 97863587d3aaa43b0190e1a33acdfc3574df5fd6 Author: Christos Margiolis <christos@margiolis.net> Date: Wed, 8 Apr 2020 01:01:59 +0300 bug in 2.3 Diffstat:
37 files changed, 370 insertions(+), 173 deletions(-)
diff --git a/assignment-1.5-arrays-pointers-files/minecurses/bin/minecurses b/assignment-1.5-arrays-pointers-files/minecurses/bin/minecurses Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/audio.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/audio.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/gameplay.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/gameplay.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/obj/navigation.o b/assignment-1.5-arrays-pointers-files/minecurses/obj/navigation.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/audio.c b/assignment-1.5-arrays-pointers-files/minecurses/src/audio.c @@ -2,7 +2,7 @@ void *play_audio(void *threadid) { - int tid = (long)threadid; + /*int tid = (long)threadid;*/ Mix_Music *music = NULL; SDL_Init(SDL_INIT_AUDIO); Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 4096); @@ -33,6 +33,5 @@ void volume(char option) void pause_audio(void) { - if(Mix_PausedMusic() == 1) Mix_ResumeMusic(); - else Mix_PauseMusic(); + (Mix_PausedMusic() == 1) ? Mix_ResumeMusic() : Mix_PauseMusic(); } diff --git a/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c b/assignment-1.5-arrays-pointers-files/minecurses/src/gameplay.c @@ -10,32 +10,39 @@ void play_minesweeper(WINDOW *gamew, char **dboard, char **mboard, int COLS, int getmaxyx(stdscr, ymax, xmax); ymid = ymax/2; xmid = xmax/2; - print_board(gamew, dboard, COLS, ROWS); do { session_info(mbx, mby, xmax, xmid, ndefused, NMINES); navigate(gamew, dboard, &move, &mbx, &mby); - - if (move == ENTER || move == OPEN_LOWER || move == OPEN_UPPER) - gameover = open_cell(gamew, dboard, mboard, mby, mbx, gameover); - else if (move == FLAG_LOWER || move == FLAG_UPPER) - handle_flags(gamew, dboard, mboard, mby, mbx); - else if (move == DEFUSE_LOWER || move == DEFUSE_UPPER) - { - if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] == MINE) - { - ndefused++; - defuse_mine(gamew, dboard, mboard, mby, mbx); - } - else if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] != MINE) gameover = TRUE; - } - else if (move == PAUSE_AUDIO) pause_audio(); - else if (move == VOLUME_UP || move == VOLUME_DOWN) volume(move); - else if (move == 'm') handle_menu(gamew, dboard, COLS, ROWS); - - + switch (move) + { + case ENTER: case OPEN_LOWER: case OPEN_UPPER: + gameover = open_cell(gamew, dboard, mboard, mby, mbx, gameover); + break; + case FLAG_LOWER: case FLAG_UPPER: + handle_flags(gamew, dboard, mboard, mby, mbx); + break; + case DEFUSE_LOWER: case DEFUSE_UPPER: + if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] == MINE) + { + ndefused++; + defuse_mine(gamew, dboard, mboard, mby, mbx); + } + else if (dboard[mby][mbx] == FLAG && mboard[mby][mbx] != MINE) gameover = TRUE; + break; + case PAUSE_AUDIO: + pause_audio(); + break; + case VOLUME_UP: case VOLUME_DOWN: + volume(move); + break; + case 'm': + handle_menu(gamew, dboard, COLS, ROWS); + break; + default: break; + } } while (((mby >= 0 && mby < ROWS) && (mbx >= 0 && mbx < COLS)) && ndefused < NMINES && !gameover && move != QUIT); diff --git a/assignment-1.5-arrays-pointers-files/minecurses/txt/mnsout.txt b/assignment-1.5-arrays-pointers-files/minecurses/txt/mnsout.txt @@ -1,44 +1,9 @@ -Mine hit at position (16, 29) +Last mine defused at position (2, 3) Board overview -- - - - - - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - 1 * 1 -- - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - 1 1 1 - - - - 1 1 1 -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 * 1 - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - -- - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - 1 D 1 - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - 1 1 1 - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - 1 1 1 - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 * 1 - - -- - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - -- - - - - 1 * 1 - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - 1 1 1 - 1 * 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - 1 * 2 1 1 - - - - - - - - - - - - -- - - - - - - - - - - 1 1 1 - - - - - - - - - 1 1 2 * 1 - - - - - - - - - - - - -- - - - - - - - - - - 1 * 1 - - - - - - - - - - - 1 1 1 - - - - - - - - - - - - -- - - - - - - - - - - 1 1 1 - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - +1 1 1 - - +1 D 1 - - +1 1 1 - - +- - - - - diff --git a/assignment-1.5-arrays-pointers-files/minecurses/txt/scorelog.txt b/assignment-1.5-arrays-pointers-files/minecurses/txt/scorelog.txt @@ -9,3 +9,5 @@ christos 1 5x5 christos 1 5x5 christos 1 5x5 + + 1 5x5+ \ No newline at end of file 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/obj/subject.o b/assignment-2.3-operoverloading/obj/subject.o Binary files differ. diff --git a/assignment-2.3-operoverloading/src/main.cpp b/assignment-2.3-operoverloading/src/main.cpp @@ -1,5 +1,4 @@ #include "student.h" -#include "subject.h" std::ostream& operator<< (std::ostream& stream, const Student& s) { @@ -19,18 +18,22 @@ static void detprint (const Student& s3); static void setters(Student& s3); static void addgrd(Student& s3); static void submsubj(Student& s3); -static void plusequals_overload(Student& s3); +static void plusequals_overload(Student& s3, const Subject& s); static void getters(const Subject& s3); static void setters(Subject& sb); int main(int argc, char **argv) { - Student *s1 = new Student("12345678", "Name Surname"); + std::string n1 = "Name Surname"; + std::string n2 = "Name Surnamington"; + std::string n3 = "Name Surnaming"; + + Student *s1 = new Student("12345678", n1); system("clear"); constructor1(*s1); cont(); ostream_overload(*s1); cont(); - - Student *s2 = new Student("92345678", "Name Surnamington", 2); + + Student *s2 = new Student("92345678", n2, 2); constructor2(*s2); cont(); Student *copystud = new Student(*s2); @@ -41,20 +44,25 @@ int main(int argc, char **argv) delete copystud; delete s2; - float grd[4] = {9.4f, 8.4f, 5.5f, 6.3f}; - Student *s3 = new Student("72345678", "Name Surnaming", 2, 4, grd); + float *grd = new float[4]{9.4f, 8.4f, 5.5f, 6.3f}; + Student *s3 = new Student("72345678", n3, 2, 4, grd); + delete[] grd; constructor3(*s3); cont(); detprint(*s3); cont(); setters(*s3); addgrd(*s3); cont(); submsubj(*s3); cont(); - plusequals_overload(*s3); cont(); - delete s3; - Subject *sb = new Subject("35643", "OOP", 2); - getters(*sb); cont(); - setters(*sb); - delete sb; + std::string c = "356431"; + std::string n = "OOP"; + Subject *oop = new Subject(c, n, 2); + plusequals_overload(*s3, *oop); cont(); + + getters(*oop); cont(); + setters(*oop); + + delete s3; + delete oop; return 0; } @@ -136,15 +144,13 @@ static void constructor3(const Student& s3) std::cout << "Semester: " << s3.get_semester() << std::endl; std::cout << "Subjects passed: " << s3.get_psubj() << std::endl; - float *gr = new float[s3.get_psubj()]; - gr = s3.get_grades(); + float *gr = s3.get_grades(); std::cout << "Grades: "; 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; } - delete[] gr; } static void detprint (const Student& s3) @@ -160,8 +166,9 @@ static void setters(Student& s3) s3.set_name("AAAAAAA"); s3.set_semester(100); s3.set_psubj(2); // πρώτα η set_psubj() και μετα η set_grades() !!!!! - float gg[2] = {0.1f, 2.2f}; + 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; @@ -174,8 +181,7 @@ static void setters(Student& s3) std::cout << "Input: s3.set_psubj(2):" << '\t' << '\t'; std::cout << "New subjects passed: " << s3.get_psubj() << std::endl; - float *gr = new float[s3.get_psubj()]; - gr = s3.get_grades(); + float *gr = s3.get_grades(); std::cout << "Input: {0.1f, 2.2f}" << '\t' << '\t'; std::cout << "New grades: "; for (unsigned int i = 0; i < s3.get_psubj(); i++) @@ -183,14 +189,12 @@ static void setters(Student& s3) if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl; } - delete[] gr; } static void addgrd(Student& s3) { s3.add_grade(7.5f); - float *gr = new float[s3.get_psubj()]; - gr = s3.get_grades(); + float *gr = s3.get_grades(); std::cout << "Input: s3.add_grade(7.5f)" << '\t'; std::cout << "Updated grades: "; for (unsigned int i = 0; i < s3.get_psubj(); i++) @@ -198,54 +202,65 @@ static void addgrd(Student& s3) if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; else std::cout << gr[i] << std::endl; } - delete[] gr; } static void submsubj(Student& s3) { - std::string ss[3] = {"Math", "Physics", "Programming"}; - s3.set_num_submitted_subjects(3); - s3.set_submitted_subjects(ss); + std::string c1 = "11231"; + std::string c2 = "56562"; + std::string n1 = "Discrete Math"; + std::string n2 = "Physics"; + + Subject *a = new Subject{c1, n1, 1}; + Subject *b = new Subject{c2, n2, 1}; + Subject **sb = new Subject *[2]; + sb[0] = a; + sb[1] = b; + s3.set_num_submitted_subjects(2); + s3.set_submitted_subjects(sb); - std::cout << "Submitted subjects example" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Input: s3.set_num_submitted_subjects(3)" << std::endl;; - std::cout << "Number of submitted subjects: " << s3.get_num_submitted_subjects() << std::endl << std::endl; + delete a; + delete b; + + //std::cout << "Submitted subjects example" << std::endl; + //std::cout << "----------------------------" << std::endl; + //std::cout << "Input: s3.set_num_submitted_subjects(2)" << std::endl; + //std::cout << "Number of submitted subjects: " << s3.get_num_submitted_subjects() << std::endl << std::endl; - 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 << "Submitted subjects: "; - 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; - } - delete[] ssj; + //Subject **ssj = s3.get_submitted_subjects(); + //std::cout << "Input: {\"Discrete Math\", \"Physics\"}" << std::endl; + //std::cout << "Submitted subjects: "; + //for (unsigned int i = 0; i < s3.get_num_submitted_subjects(); i++) + //{ + //if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i].get_sname() << ", "; + //else std::cout << ssj[i].get_sname() << std::endl; + //} + + for (int i = 0; i < 2; i++) + delete[] sb[i]; + delete[] sb; } -static void plusequals_overload(Student& s3) +static void plusequals_overload(Student& s3, const Subject& s) { - s3 += "Discrete Maths"; - std::string *ssj = new std::string[s3.get_num_submitted_subjects()]; - ssj = s3.get_submitted_subjects(); - std::cout << "+= operator overload" << std::endl; - std::cout << "----------------------------" << std::endl; - std::cout << "Input: s3 += \"Discrete Maths\"" << std::endl;; - std::cout << "Updated submitted subjects: "; - 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; - } - delete[] ssj; + //s3 += s; + //Subject **ssj = s3.get_submitted_subjects(); + //std::cout << "+= operator overload" << std::endl; + //std::cout << "----------------------------" << std::endl; + //std::cout << "Input: s3 += \"OOP\"" << std::endl;; + //std::cout << "Updated submitted subjects: "; + //for (unsigned int i = 0; i < s3.get_num_submitted_subjects(); i++) + //{ + //if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i].get_sname() << ", "; + //else std::cout << ssj[i].get_sname() << std::endl; + //} } static void getters(const Subject& sb) { std::cout << "Getters example using Subject object" << std::endl; std::cout << "----------------------------" << std::endl; - std::cout << "Constructor: Subject(\"35643\", \"OOP\", 2)" << std::endl; + std::cout << "Constructor: Subject(\"356431\", \"OOP\", 2)" << std::endl; std::cout << "Code: " << sb.get_code() << std::endl; std::cout << "Subject name: " << sb.get_sname() << std::endl; std::cout << "Subject semester: " << sb.get_subjsemester() << std::endl; @@ -253,8 +268,11 @@ static void getters(const Subject& sb) static void setters(Subject& sb) { - sb.set_code("14556"); - sb.set_sname("Calculus I"); + std::string c = "14556"; + std::string n = "Calculus I"; + + sb.set_code(c); + sb.set_sname(n); sb.set_subjsemester(1); std::cout << "Setters example using Subject object" << std::endl; diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -1,43 +1,63 @@ #include "student.h" Student::Student(const char *AM, const std::string& name) - :AM(convert_AM(AM)), name(name), semester(1), psubj(0) {} + :AM(convert_AM(AM)), name(name), semester(1), psubj(0) +{ + numSubmittedSubjects = 0; + submittedSubjects = nullptr; +} Student::Student(const char *AM, const std::string& name, unsigned int semester) - :AM(convert_AM(AM)), name(name), semester(semester), psubj(0) {} + :AM(convert_AM(AM)), name(name), semester(semester), psubj(0) +{ + numSubmittedSubjects = 0; + submittedSubjects = nullptr; +} Student::Student(const char *AM, const std::string& name, unsigned int semester, unsigned int psubj, const float *grades) - :AM(convert_AM(AM)), name(name), semester(semester), psubj(psubj), grades(convert_PSG(grades)) {} + :AM(convert_AM(AM)), name(name), semester(semester), psubj(psubj), grades(convert_PSG(grades)) +{ + numSubmittedSubjects = 0; + submittedSubjects = nullptr; +} Student::Student(const Student& s) :name(s.name), semester(s.semester), psubj(s.psubj) { int sl = strlen(s.AM); this->AM = new char[sl + 1]; - std::copy(s.AM, s.AM+sl, AM); - + memcpy(AM, s.AM, sizeof(s.AM) + (sl+1)); this->grades = new float[psubj]; - std::copy(s.grades, s.grades+psubj, grades); + memcpy(grades, s.grades, sizeof(s.grades) * psubj); + + if (numSubmittedSubjects <= 0) submittedSubjects = nullptr; + else memcpy(submittedSubjects, s.submittedSubjects, sizeof(s.submittedSubjects) * s.numSubmittedSubjects); } Student::~Student() { delete[] this->AM; delete[] this->grades; + + for (int i = 0; i < numSubmittedSubjects; i++) + delete[] this->submittedSubjects[i]; delete[] this->submittedSubjects; } -void Student::operator+= (const std::string& s) +void Student::operator+= (const Subject& s) { - std::string *tmp = new std::string[numSubmittedSubjects+1]; - std::copy(submittedSubjects, submittedSubjects+numSubmittedSubjects, tmp); - tmp[numSubmittedSubjects] = s; - delete[] submittedSubjects; - submittedSubjects = new std::string[numSubmittedSubjects+1]; - std::copy(tmp, tmp+numSubmittedSubjects+1, submittedSubjects); + Subject **tmp = new Subject *[numSubmittedSubjects+1]; + memcpy(tmp, submittedSubjects, sizeof(Subject) * numSubmittedSubjects); + tmp[numSubmittedSubjects][0] = s; + if (submittedSubjects != nullptr) + { + for (int i = 0; i < numSubmittedSubjects; i++) + delete[] submittedSubjects[i]; + delete[] submittedSubjects; + } + submittedSubjects = tmp; numSubmittedSubjects++; - delete[] tmp; } Student& Student::operator= (const Student& s) @@ -56,51 +76,40 @@ Student& Student::operator= (const Student& s) return *this; } -float *Student::get_grades() const -{ - float *ret = new float[psubj]; - std::copy(grades, grades+psubj, ret); - return ret; -} - -std::string *Student::get_submitted_subjects() const -{ - std::string *ret = new std::string[numSubmittedSubjects]; - std::copy(submittedSubjects, submittedSubjects+numSubmittedSubjects, ret); - return ret; -} - -void Student::set_submitted_subjects(std::string* submittedSubjects) +void Student::set_submitted_subjects(Subject **submittedSubjects) { - this->submittedSubjects = new std::string[numSubmittedSubjects]; - std::copy(submittedSubjects, submittedSubjects+numSubmittedSubjects, this->submittedSubjects); + // handle 0 subj + this->submittedSubjects = new Subject *[numSubmittedSubjects]; + memcpy(this->submittedSubjects, submittedSubjects, sizeof(Subject) * numSubmittedSubjects); } char *Student::convert_AM(const char *AM) { int len = strlen(AM); - this->AM = new char[len+1]; - std::copy(AM, AM+len, this->AM); - return this->AM; + char *tmp = new char[len+1]; + memcpy(tmp, AM, len+1); + return tmp; } float *Student::convert_PSG(const float *grades) { - this->grades = new float[psubj]; - std::copy(grades, grades+psubj, this->grades); - return this->grades; + if (psubj > 0) + { + float *tmp = new float[psubj]; + memcpy(tmp, grades, sizeof(grades) * psubj); + return tmp; + } + else return nullptr; } void Student::add_grade(float grade) { float *tmp = new float[psubj+1]; - std::copy(grades, grades+psubj, tmp); + memcpy(tmp, grades, sizeof(grades) * psubj); tmp[psubj] = grade; - delete[] grades; - grades = new float[psubj+1]; - std::copy(tmp, tmp+psubj+1, grades); + if (grades != nullptr) delete[] grades; + grades = tmp; psubj++; - delete[] tmp; } void Student::detailed_print() const diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -1,15 +1,20 @@ +#ifndef STUDENT_H +#define STUDENT_H + #include <iostream> #include <iomanip> #include <algorithm> #include <string> #include <string.h> +#include "subject.h" + class Student { private: char *AM; std::string name; - std::string *submittedSubjects; + Subject **submittedSubjects; unsigned int numSubmittedSubjects; unsigned int semester; unsigned int psubj; @@ -25,7 +30,7 @@ class Student ~Student(); friend std::ostream& operator<< (std::ostream& stream, const Student& s); - void operator+= (const std::string& s); + void operator+= (const Subject& s); Student& operator= (const Student& s); inline bool operator== (const Student& s) const {return (this->semester == s.semester) ? true : false;} @@ -35,13 +40,13 @@ class Student inline bool operator> (const Student& s) const {return (this->semester > s.semester) ? true : false;} inline bool operator>= (const Student& s) const {return (this->semester >= s.semester) ? true : false;} - inline const char *get_AM() const {return this->AM;} - inline const std::string& get_name() const {return this->name;} - inline unsigned int get_semester() const {return this->semester;} - inline unsigned int get_psubj() const {return this->psubj;} - float *get_grades() const; - inline unsigned int get_num_submitted_subjects() const {return this->numSubmittedSubjects;} - std::string *get_submitted_subjects() const; + inline const char *get_AM(void) const {return this->AM;} + inline const std::string& get_name(void) const {return this->name;} + inline unsigned int get_semester(void) const {return this->semester;} + inline unsigned int get_psubj(void) const {return this->psubj;} + inline float *get_grades(void) const {return (this->psubj > 0) ? this->grades : nullptr;} + inline Subject **get_submitted_subjects(void) const {return this->submittedSubjects;} + inline unsigned int get_num_submitted_subjects(void) const {return this->numSubmittedSubjects;} inline void set_AM(const char *AM) {this->AM = convert_AM(AM);} inline void set_name(const std::string& name) {this->name = name;} @@ -49,7 +54,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;} - void set_submitted_subjects(std::string* submittedSubjects); + void set_submitted_subjects(Subject **submittedSubjects); char *convert_AM(const char *AM); float *convert_PSG(const float *grades); @@ -57,3 +62,5 @@ class Student void detailed_print() const; float calc_average() const; }; + +#endif /* STUDENT_H */ diff --git a/assignment-2.3-operoverloading/src/subject.cpp b/assignment-2.3-operoverloading/src/subject.cpp @@ -1,4 +1,12 @@ #include "subject.h" + +Subject::Subject() {} + Subject::Subject(const std::string& code, const std::string& sname, unsigned int subjsemester) :code(code), sname(sname), subjsemester(subjsemester) {} + +Subject::Subject(const Subject& s) + :code(s.code), sname(s.sname), subjsemester(s.subjsemester) {} + +Subject::~Subject() {} diff --git a/assignment-2.3-operoverloading/src/subject.h b/assignment-2.3-operoverloading/src/subject.h @@ -1,3 +1,6 @@ +#ifndef SUBJECT_H +#define SUBJECT_H + #include <iostream> #include <string> @@ -9,7 +12,10 @@ class Subject unsigned int subjsemester; public: + Subject(); Subject(const std::string& code, const std::string& sname, unsigned int subjsemester); + Subject(const Subject& s); + ~Subject(); inline const std::string& get_code(void) const {return this->code;} inline const std::string& get_sname(void) const {return this->sname;} @@ -19,3 +25,5 @@ class Subject inline void set_sname(const std::string& sname) {this->sname = sname;} inline void set_subjsemester(unsigned int subjsemester) {this->subjsemester = subjsemester;} }; + +#endif /* SUBJECT_H */ diff --git a/assignment-2.4-inheritance/Makefile b/assignment-2.4-inheritance/Makefile @@ -0,0 +1,40 @@ +TARGET = inheritance +#INSTALL_PATH = /usr/local/bin + +SRC_DIR = src +OBJ_DIR = obj +BIN_DIR = bin + +SRC = $(wildcard $(SRC_DIR)/*.cpp) +OBJ = $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o) + +MOVE = mv +MKDIR_P = mkdir -p + +CC = g++ +CPPFLAGS += -Iinclude +CFLAGS += -Wall +LDFLAGS += -Llib +#LDLIBS += -lm + +.PHONY: all clean + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(MKDIR_P) $(BIN_DIR) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + $(MOVE) $(TARGET) $(BIN_DIR) + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp + $(MKDIR_P) $(OBJ_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +run: + ./$(BIN_DIR)/$(TARGET) + +install: $(TARGET) + cp $(BIN_DIR)/$(TARGET) $(INSTALL_PATH) + +clean: + $(RM) $(OBJ) $(BIN_DIR)/$(TARGET) diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance Binary files differ. diff --git a/assignment-2.4-inheritance/data.txt b/assignment-2.4-inheritance/data.txt diff --git a/assignment-2.4-inheritance/obj/appsystem.o b/assignment-2.4-inheritance/obj/appsystem.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/game.o b/assignment-2.4-inheritance/obj/game.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/main.o b/assignment-2.4-inheritance/obj/main.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/manufacturer.o b/assignment-2.4-inheritance/obj/manufacturer.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/office.o b/assignment-2.4-inheritance/obj/office.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/review.o b/assignment-2.4-inheritance/obj/review.o Binary files differ. diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -0,0 +1,5 @@ +#include "appsystem.h" + +AppSystem::AppSystem() {} + +AppSystem::~AppSystem() {} diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -0,0 +1,21 @@ +#ifndef APPSTREAM_H +#define APPSTREAM_H + +#include <string> +#include <vector> + +class AppSystem +{ + private: + std::vector<std::string> applist; + std::vector<const char *> manufacturers; + + public: + AppSystem(); + virtual ~AppSystem(); + + inline void operator+= (const std::string& app) {this->applist.push_back(app);} + inline void operator+= (const char *man) {this->manufacturers.push_back(man);} +}; + +#endif /* APPSYSTEM_H */ diff --git a/assignment-2.4-inheritance/src/game.cpp b/assignment-2.4-inheritance/src/game.cpp @@ -0,0 +1,5 @@ +#include "game.h" + +Game::Game() {} + +Game::~Game() {} diff --git a/assignment-2.4-inheritance/src/game.h b/assignment-2.4-inheritance/src/game.h @@ -0,0 +1,20 @@ +#ifndef GAME_H +#define GAME_H + +#include "manufacturer.h" +#include "review.h" + +class Game : public Manufacturer, public Review +{ + private: + std::string genre; + std::string os; + bool online; + int price; + + public: + Game(); + ~Game(); +}; + +#endif /* GAME_H */ diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -0,0 +1,12 @@ +#include "appsystem.h" +#include "manufacturer.h" +#include "review.h" +#include "office.h" +#include "game.h" + +int main(int argc, char **argv) +{ + + + return 0; +} diff --git a/assignment-2.4-inheritance/src/manufacturer.cpp b/assignment-2.4-inheritance/src/manufacturer.cpp @@ -0,0 +1,5 @@ +#include "manufacturer.h" + +Manufacturer::Manufacturer() {} + +Manufacturer::~Manufacturer() {} diff --git a/assignment-2.4-inheritance/src/manufacturer.h b/assignment-2.4-inheritance/src/manufacturer.h @@ -0,0 +1,18 @@ +#ifndef MANUFACTURER_H +#define MANUFACTURER_H + +#include "appsystem.h" + +class Manufacturer : public AppSystem +{ + private: + const char *serialnum; + const char *name; + std::string email; + + public: + Manufacturer(); + virtual ~Manufacturer(); +}; + +#endif /* MANUFACTURER_H */ diff --git a/assignment-2.4-inheritance/src/office.cpp b/assignment-2.4-inheritance/src/office.cpp @@ -0,0 +1,5 @@ +#include "office.h" + +Office::Office() {} + +Office::~Office() {} diff --git a/assignment-2.4-inheritance/src/office.h b/assignment-2.4-inheritance/src/office.h @@ -0,0 +1,19 @@ +#ifndef OFFICE_H +#define OFFICE_H + +#include "manufacturer.h" +#include "review.h" + +class Office : public Manufacturer, public Review +{ + private: + std::vector<std::string> extensions; + std::string os; + int price; + + public: + Office(); + ~Office(); +}; + +#endif /* OFFICE_H */ diff --git a/assignment-2.4-inheritance/src/review.cpp b/assignment-2.4-inheritance/src/review.cpp @@ -0,0 +1,5 @@ +#include "review.h" + +Review::Review() {} + +Review::~Review() {} diff --git a/assignment-2.4-inheritance/src/review.h b/assignment-2.4-inheritance/src/review.h @@ -0,0 +1,18 @@ +#ifndef REVIEW_H +#define REVIEW_H + +#include "appsystem.h" + +class Review : public AppSystem +{ + private: + int stars; + std::string username; + std::string comment; + + public: + Review(); + virtual ~Review(); +}; + +#endif /* REVIEW_H */