commit cc0c7d8b5f56ba7e17bfa38cd29e1f1e43d40396 parent cf2974b0fa72c58b87e780a5984205cb16aae286 Author: Christos Margiolis <christos@margiolis.net> Date: Mon, 4 May 2020 03:35:10 +0300 almost finished 2.4 Diffstat:
27 files changed, 194 insertions(+), 108 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). +**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 @@ -34,18 +34,20 @@ class Student void set_psubj(unsigned int psubj); void set_grades(float *grades); - char *convert_AM(const char *AM); - float *convert_PSG(const float *grades); void add_grade(float grade); void detailed_print() const; + + private: + char *convert_AM(const char *AM); + float *convert_PSG(const float *grades); float calc_average() const; }; 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), grades(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), grades(nullptr) {} Student::Student(const char *AM, const std::string& name, unsigned int semester, unsigned int psubj, const float *grades) @@ -63,8 +65,8 @@ Student::Student(const Student& s) Student::~Student() { - delete[] this->AM; - delete[] this->grades; + if (this->AM != nullptr) delete[] this->AM; + if (this->AM != nullptr) delete[] this->grades; } const char *Student::get_AM() const @@ -151,21 +153,28 @@ void Student::add_grade(float grade) void Student::detailed_print() const { - for (unsigned int i = 0; i < psubj; i++) + if (grades != nullptr) { - std::cout << "Subject " << i+1 << ": "; - std::cout << grades[i] << std::endl; + for (unsigned int i = 0; i < psubj; i++) + { + 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) << calc_average() << std::endl; } float Student::calc_average() const { - float sum = 0; - for (unsigned int i = 0; i < psubj; i++) - sum += grades[i]; - float average = sum / psubj; - return average; + if (grades != nullptr) + { + float sum = 0; + for (unsigned int i = 0; i < psubj; i++) + sum += grades[i]; + float average = sum / psubj; + return average; + } + else return 0.0f; } std::ostream& operator<< (std::ostream& stream, const Student& s) @@ -270,11 +279,14 @@ static void constructor3(const Student& s3) std::cout << "subjects passed: " << s3.get_psubj() << std::endl; float *gr = s3.get_grades(); - std::cout << "grades: "; - for (unsigned int i = 0; i < s3.get_psubj(); i++) + if (gr != nullptr) { - if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl << std::endl; + 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; + } } } @@ -307,12 +319,15 @@ static void setters(Student& s3) std::cout << "New subjects passed: " << s3.get_psubj() << std::endl; 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++) + if (gr != nullptr) { - if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; + std::cout << "Input: {0.1f, 2.2f}" << '\t' << '\t'; + std::cout << "New 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; + } } } @@ -320,11 +335,14 @@ static void addgrd(Student& s3) { s3.add_grade(7.5f); 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++) + if (gr != nullptr) { - if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; + std::cout << "Input: s3.add_grade(7.5f)" << '\t'; + std::cout << "Updated 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; + } } } 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 @@ -147,11 +147,14 @@ static void constructor3(const Student& s3) std::cout << "Subjects passed: " << s3.get_psubj() << std::endl; float *gr = s3.get_grades(); - std::cout << "Grades: "; - for (unsigned int i = 0; i < s3.get_psubj(); i++) + if (gr != nullptr) { - if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl << std::endl; + 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; + } } } @@ -184,12 +187,15 @@ static void setters(Student& s3) std::cout << "New subjects passed: " << s3.get_psubj() << std::endl; 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++) + if (gr != nullptr) { - if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; + std::cout << "Input: {0.1f, 2.2f}" << '\t' << '\t'; + std::cout << "New 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; + } } } @@ -197,12 +203,15 @@ static void addgrd(Student& s3) { s3.add_grade(7.5f); 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++) + if (gr != nullptr) { - if (i != s3.get_psubj()-1) std::cout << gr[i] << ", "; - else std::cout << gr[i] << std::endl; + std::cout << "Input: s3.add_grade(7.5f)" << '\t'; + std::cout << "Updated 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; + } } } @@ -225,12 +234,15 @@ static void submsubj(Student& s3) std::cout << "Number of submitted subjects: " << s3.get_num_submitted_subjects() << std::endl << std::endl; 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 (ssj != nullptr) { - if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i][0].get_sname() << ", "; - else std::cout << ssj[i][0].get_sname() << std::endl; + 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][0].get_sname() << ", "; + else std::cout << ssj[i][0].get_sname() << std::endl; + } } } @@ -238,14 +250,17 @@ static void plusequals_overload(Student& s3, Subject *s) { 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 (ssj != nullptr) { - if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i][0].get_sname() << ", "; - else std::cout << ssj[i][0].get_sname() << std::endl; + 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][0].get_sname() << ", "; + else std::cout << ssj[i][0].get_sname() << std::endl; + } } } diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -45,9 +45,9 @@ Student::Student(const Student& s) Student::~Student() { - delete[] this->AM; - delete[] this->grades; - delete[] this->ssubj; + if (this->AM != nullptr) delete[] this->AM; + if (this->grades != nullptr) delete[] this->grades; + if (this->ssubj != nullptr) delete[] this->ssubj; } void Student::operator+= (Subject *s) @@ -176,8 +176,11 @@ void Student::set_num_submitted_subjects(unsigned nssubj) void Student::set_submitted_subjects(Subject **ssubj) { - this->ssubj = new Subject *[nssubj]; - memcpy(this->ssubj, ssubj, sizeof(Subject *) * nssubj); + if (ssubj != nullptr) + { + this->ssubj = new Subject *[nssubj]; + memcpy(this->ssubj, ssubj, sizeof(Subject *) * nssubj); + } } char *Student::convert_AM(const char *AM) @@ -214,19 +217,26 @@ void Student::add_grade(float grade) void Student::detailed_print() const { - for (unsigned int i = 0; i < psubj; i++) + if (grades != nullptr) { - std::cout << "Subject " << i+1 << ": "; - std::cout << grades[i] << std::endl; + for (unsigned int i = 0; i < psubj; i++) + { + 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) << calc_average() << std::endl; } float Student::calc_average() const { - float sum = 0; - for (unsigned int i = 0; i < psubj; i++) - sum += grades[i]; - float average = sum / psubj; - return average; + if (grades != nullptr) + { + float sum = 0; + for (unsigned int i = 0; i < psubj; i++) + sum += grades[i]; + float average = sum / psubj; + return average; + } + else return 0.0f; } diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -54,10 +54,12 @@ class Student void set_num_submitted_subjects(unsigned int nssubj); void set_submitted_subjects(Subject **ssubj); - char *convert_AM(const char *AM); - float *convert_PSG(const float *grades); void add_grade(float grade); void detailed_print() const; + + private: + char *convert_AM(const char *AM); + float *convert_PSG(const float *grades); float calc_average() const; }; 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/obj/app.o b/assignment-2.4-inheritance/obj/app.o Binary files differ. 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/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/res/appdata.csv b/assignment-2.4-inheritance/res/appdata.csv @@ -1,5 +1,5 @@ -Type,SN,Name,OS,Manf,Price,Genre,Online -Game,123,LoL,Windows,Riot,0,MOBA,Yes -Game,345,CS:GO,Cross,Valve,14,FPS,Yes -Office,567,Libre,Linux,GNU,0,N/A,N/A -Office,789,Vim,Linux,GNU,0,N/A,N/A +Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions +Game,123,LoL,Windows,Riot,0,MOBA,Yes,N/A +Game,345,CS:GO,Cross,Valve,14,FPS,Yes,N/A +Office,567,LibreOffice,Linux,GNU,0,N/A,N/A,.doc|.xls|.odt|.ods|.ppt| +Office,789,Vim,Linux,GNU,0,N/A,N/A,.txt|.md|.tex|.cpp|.sh| diff --git a/assignment-2.4-inheritance/res/appdata_out.csv b/assignment-2.4-inheritance/res/appdata_out.csv @@ -0,0 +1,6 @@ +Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions +Game,123,LoL,Windows,Riot,0,MOBA,No,N/A +Game,345,CS:GO,Cross,Valve,14,FPS,No,N/A +Office,567,LibreOffice,Linux,GNU,0,N/A,N/A,.doc|.xls|.odt|.ods|.ppt| +Office,789,Vim,Linux,GNU,0,N/A,N/A,.txt|.md|.tex|.cpp|.sh| +Office,459,OpenOffice,Linux 2.2,Company,0,N/A,N/A,.doc|.xls|.ppt| diff --git a/assignment-2.4-inheritance/res/appout.csv b/assignment-2.4-inheritance/res/appout.csv @@ -1,6 +0,0 @@ -Type,SN,Name,OS,Manf,Price,Genre,Online -Game,123,LoL,Windows,Riot,0,MOBA,Yes -Game,345,CS:GO,Cross,Valve,14,FPS,Yes -Office,567,Libre,Linux,GNU,0,N/A,N/A -Office,789,Vim,Linux,GNU,0,N/A,N/A -Office,459,LibreOffice,Linux 2.2,GNU,0,N/A,N/A diff --git a/assignment-2.4-inheritance/res/manfdata_out.csv b/assignment-2.4-inheritance/res/manfdata_out.csv @@ -0,0 +1,6 @@ +SN,Name,Email +124,Company,comp@comp.com +568,Chris,chris@cm.com +741,Riot,riot@riot.com +987,Valve,valve@valve.com +456,GNU,gnu@gnu.com diff --git a/assignment-2.4-inheritance/res/manfout.csv b/assignment-2.4-inheritance/res/manfout.csv @@ -1,6 +0,0 @@ -SN,Name,Email -124,GNU,gnu@gnu.com -568,Chris,chris@cm.com -741,Riot,riot@riot.com -987,Valve,valve@valve.com -456,GNU,gnu@gnu.com diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -10,8 +10,8 @@ App::App(const char *serialnum, const std::string& name, try { if (strcmp(serialnum, "") == 0) throw serialnum; - if (name.empty() || name == "") throw name; - if (os.empty() || os == "") throw os; + if (name.empty()) throw name; + if (os.empty()) throw os; if (price < 0) throw price; } catch (const char *serialnum) diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -36,6 +36,9 @@ bool AppSystem::read_data<Manufacturer>(const char *fpath) f.exceptions(std::ifstream::badbit); try { + std::string strpath(fpath); + if (strpath.find(".csv") == std::string::npos) throw strpath; + f.open(fpath); if (f.is_open()) { @@ -46,6 +49,10 @@ bool AppSystem::read_data<Manufacturer>(const char *fpath) } f.close(); } + catch (const std::string& strpath) + { + throw std::runtime_error("Error. File must be of format \'.csv\'."); + } catch (const std::ifstream::failure& e) { std::cerr << "Error reading file \'" << fpath << "\'." << std::endl << @@ -62,6 +69,9 @@ bool AppSystem::read_data<App>(const char *fpath) f.exceptions(std::ifstream::badbit); try { + std::string strpath(fpath); + if (strpath.find(".csv") == std::string::npos) throw strpath; + f.open(fpath); if (f.is_open()) { @@ -79,6 +89,10 @@ bool AppSystem::read_data<App>(const char *fpath) } f.close(); } + catch (const std::string& strpath) + { + throw std::runtime_error("Error. File must be of format \'.csv\'."); + } catch (const std::ifstream::failure& e) { std::cerr << "Error reading file \'" << fpath << "\'." << std::endl << @@ -155,7 +169,8 @@ bool AppSystem::read_office(std::ifstream& f) std::getline(f, manf, ','); std::getline(f, price, ','); std::getline(f, skip1, ','); - std::getline(f, skip2); + std::getline(f, skip2, ','); + std::vector<std::string> exts = read_office_exts(f); if (!manfs.empty()) { @@ -164,7 +179,7 @@ bool AppSystem::read_office(std::ifstream& f) if (man->get_name() == manf) { apps.push_back(new Office(sn.c_str(), name, os, - man, std::stoi(price), {})); + man, std::stoi(price), exts)); break; } } @@ -179,6 +194,16 @@ bool AppSystem::read_office(std::ifstream& f) return true; } +const std::vector<std::string> AppSystem::read_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; +} + template<> bool AppSystem::export_data<Manufacturer>(const char *fpath) { @@ -196,7 +221,7 @@ bool AppSystem::export_data<Manufacturer>(const char *fpath) } catch (const std::ofstream::failure& e) { - std::cerr << "Error writing to file \'" << fpath << "\'." << std::endl << + std::cerr << "Error writing to file (" << fpath << ")." << std::endl << e.what() << std::endl; return false; } @@ -211,7 +236,7 @@ bool AppSystem::export_data<App>(const char *fpath) try { f.open(fpath); - f << "Type,SN,Name,OS,Manf,Price,Genre,Online\n"; + f << "Type,SN,Name,OS,Manf,Price,Genre,Online,Extensions\n"; for (auto& app : apps) { Manufacturer manf = app->get_manf(); @@ -223,19 +248,33 @@ bool AppSystem::export_data<App>(const char *fpath) manf.get_name() << ',' << app->get_price() << ',' << (o ? o->get_genre() :"N/A") << ',' << - (o ? (o->get_online() ? "Yes" : "No") : "N/A") << std::endl; + (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; + } } f.close(); } catch (const std::ofstream::failure& e) { - std::cerr << "Error writing to file \'" << fpath << "\'." << std::endl << + std::cerr << "Error writing to file (" << fpath << ")." << std::endl << e.what() << std::endl; return false; } return true; } +void AppSystem::write_office_exts(Office *o, std::ofstream& f) +{ + std::vector<std::string> exts = o->get_exts(); + for (auto& ext : exts) + f << ext << '|'; +} + void AppSystem::newrev(const std::string& appname, Review *rev) { for (auto& app : apps) diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -4,6 +4,7 @@ #include <algorithm> #include <fstream> #include <iostream> +#include <sstream> #include "game.h" #include "office.h" @@ -43,6 +44,8 @@ class AppSystem bool read_manf(std::ifstream& f); bool read_game(std::ifstream& f); bool read_office(std::ifstream& f); + const std::vector<std::string> read_office_exts(std::ifstream& f); + void write_office_exts(Office *o, std::ofstream& f); }; #endif /* APPSYSTEM_H */ diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -45,21 +45,21 @@ std::ostream& operator<< (std::ostream& stream, const AppSystem& sys) int main(int argc, char **argv) { AppSystem sys; - Manufacturer *gnu = new Manufacturer("124", "GNU", "gnu@gnu.com"); - Manufacturer *cm = new Manufacturer("568", "Chris", "chris@cm.com"); - sys += gnu; - sys += cm; + Manufacturer *comp = new Manufacturer("124", "Company", "comp@comp.com"); + Manufacturer *chris = new Manufacturer("568", "Chris", "chris@cm.com"); + sys += comp; + sys += chris; if (!sys.read_data<Manufacturer>("res/manfdata.csv")) return -1; if (!sys.read_data<App>("res/appdata.csv")) return -1; std::vector<std::string> ext = {".doc", ".xls", ".ppt"}; - sys += new Office("459", "LibreOffice", "Linux 2.2", gnu, 0, ext); - sys += new Game("731", "minecurses", "Linux 4.5", cm, 0, "Puzzle", false); + sys += new Office("459", "OpenOffice", "Linux 2.2", comp, 0, ext); + sys += new Game("731", "minecurses", "Linux 4.5", chris, 0, "Puzzle", false); Review rev(4, "Name Surnaming", "Good"); sys.newrev("minecurses", &rev); std::cout << sys << std::endl; - sys.removebad(cm); + sys.removebad(chris); std::cout << sys << std::endl; std::vector<Office *> fapps = sys.get_freeapps(); @@ -69,8 +69,8 @@ int main(int argc, char **argv) for (auto& ggame : ggames) std::cout << ggame->get_name() << std::endl; - if (!sys.export_data<Manufacturer>("res/manfout.csv")) return -1; - if (!sys.export_data<App>("res/appout.csv")) return -1; + if (!sys.export_data<Manufacturer>("res/manfdata_out.csv")) return -1; + if (!sys.export_data<App>("res/appdata_out.csv")) return -1; return 0; } diff --git a/assignment-2.4-inheritance/src/manufacturer.cpp b/assignment-2.4-inheritance/src/manufacturer.cpp @@ -11,8 +11,7 @@ Manufacturer::Manufacturer(const char *serialnum, const char *name, { if (strcmp(serialnum, "") == 0) throw serialnum; if (strcmp(name, "") == 0) throw name; - if (email.empty() || email == "" || - email.find("@") == std::string::npos) throw email; + if (email.empty() || email.find("@") == std::string::npos) throw email; } catch (const char *serialnum) { 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/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.