uni

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

commit 6b0cbb964cb12144d40c20f51c5fcef0475aec99
parent 314bf188303c465b1010cd7b98e1c6f6d4cfd759
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon,  1 Jun 2020 14:27:24 +0300

code cleanup

Diffstat:
Massignment-2.2-classes/classes.cpp | 1-
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/student.h | 1-
Massignment-2.4-inheritance/bin/inheritance | 0
Massignment-2.4-inheritance/obj/appsystem.o | 0
Massignment-2.4-inheritance/obj/main.o | 0
Massignment-2.4-inheritance/src/appsystem.cpp | 85-------------------------------------------------------------------------------
Massignment-2.4-inheritance/src/appsystem.h | 38++++++++++++++++++++++++++++----------
Massignment-2.4-inheritance/src/main.cpp | 35+++++++++++++++++++----------------
Massignment-2.5-spreadsheets/bin/spreadsheets | 0
Massignment-2.5-spreadsheets/obj/datahandler.o | 0
Massignment-2.5-spreadsheets/obj/errlog.o | 0
Massignment-2.5-spreadsheets/obj/main.o | 0
Massignment-2.5-spreadsheets/src/datahandler.cpp | 37++++++++++++++-----------------------
Massignment-2.5-spreadsheets/src/datahandler.h | 6+++---
Massignment-2.5-spreadsheets/src/errlog.h | 2+-
18 files changed, 65 insertions(+), 140 deletions(-)

diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp @@ -12,7 +12,6 @@ class Student unsigned int semester; unsigned int pcourses; float *grades; - std::size_t size; public: Student(const char *id, const std::string& name); 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/student.h b/assignment-2.3-operoverloading/src/student.h @@ -16,7 +16,6 @@ class Student 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 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/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/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -1,7 +1,5 @@ #include "appsystem.h" -AppSystem::AppSystem() {} - AppSystem::~AppSystem() { dealloc<App>(apps); @@ -42,89 +40,6 @@ AppSystem::write_office_exts(Office *of, std::ofstream& f) } void -AppSystem::newrev(const std::string& appname, Review *rev) -{ - for (auto&& app : apps) - if (app->get_name() == appname) - app->addrev(rev); -} - -void -AppSystem::newrevs(const std::string& appname, const std::vector<Review *> revs) -{ - for (auto&& app : apps) - if (app->get_name() == appname) - app->addrevs(revs); -} - -void -AppSystem::chserialnum(const std::string& appname, const char *serialnum) -{ - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_serialnum(serialnum); -} - -void -AppSystem::chname(const std::string& appname, const std::string& name) -{ - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_name(name); -} - -void -AppSystem::chos(const std::string& appname, const std::string& os) -{ - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_os(os); -} - -void -AppSystem::chmanf(const std::string& appname, Manufacturer *manf) -{ - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_manf(manf); -} - -void -AppSystem::chprice(const std::string& appname, int price) -{ - for (auto&& app : apps) - if (app->get_name() == appname) - app->set_price(price); -} - -void -AppSystem::chgenre(const std::string& appname, const std::string& genre) -{ - for (const auto& app : apps) - if (Game *o = dynamic_cast<Game *>(app)) - if (o->get_name() == appname) - o->set_genre(genre); -} - -void -AppSystem::chonline(const std::string& appname, bool online) -{ - for (const auto& app : apps) - if (Game *o = dynamic_cast<Game *>(app)) - if (o->get_name() == appname) - o->set_online(online); -} - -void -AppSystem::chexts(const std::string& appname, const std::vector<std::string> exts) -{ - for (const auto& app : apps) - if (Office *o = dynamic_cast<Office *>(app)) - if (o->get_name() == appname) - o->set_exts(exts); -} - -void AppSystem::removebad(Manufacturer *man) { apps.erase(std::remove_if(apps.begin(), apps.end(), [&](App *app) diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -16,7 +16,7 @@ class AppSystem std::vector<Manufacturer *> manfs; public: - AppSystem(); + AppSystem() = default; ~AppSystem(); AppSystem& operator+= (App *app); @@ -24,13 +24,14 @@ class AppSystem template<typename T> bool import_data(const char *fpath); template<typename T> bool export_data(const char *fpath); - void newrev (const std::string& appname, Review *rev); - void newrevs (const std::string& appname, const std::vector<Review *> revs); - void chserialnum(const std::string& appname, const char *serialnum); - void chname (const std::string& appname, const std::string& name); - void chos (const std::string& appname, const std::string& os); - void chmanf (const std::string& appname, Manufacturer *manf); - void chprice (const std::string& appname, int price); + template<typename T> void call( + const std::string& appname, + T element, + void(App::*func)(T)); + template<typename T, class U> void cast_call( + const std::string& appname, + T element, + void(U::*func)(T)); void chgenre (const std::string& appname, const std::string& genre); void chonline (const std::string& appname, bool online); void chexts (const std::string& appname, const std::vector<std::string> exts); @@ -146,7 +147,7 @@ AppSystem::import_data(const char *fpath) f.open(fpath); if (f.is_open()) { - std::cout << "Importing data from \'" << fpath << "\'." << std::endl; + std::printf("Importing data from \'%s\'\n", fpath); std::string skip; std::getline(f, skip); while (f.good()) @@ -199,7 +200,7 @@ AppSystem::export_data(const char *fpath) if (!valid_path(strpath)) throw std::runtime_error(err_csv(strpath)); f.open(fpath); - std::cout << "Exporting data to \'" << fpath << "\'." << std::endl; + std::printf("Exporting data to \'%s\'\n", fpath); if constexpr (std::is_same_v<T, Manufacturer>) { @@ -259,6 +260,23 @@ AppSystem::export_data(const char *fpath) } template<typename T> void +AppSystem::call(const std::string& appname, T element, void(App::*func)(T)) +{ + for (auto&& app : apps) + if (app->get_name() == appname) + (app->*func)(element); +} + +template<typename T, class U> void +AppSystem::cast_call(const std::string& appname, T element, void(U::*func)(T)) +{ + for (auto&& app : apps) + if (U *o = dynamic_cast<U *>(app)) + if (o->get_name() == appname) + (o->*func)(element); +} + +template<typename T> void AppSystem::dealloc(std::vector<T *>& vec) { if (!vec.empty()) diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -38,7 +38,7 @@ main(int argc, char **argv) if (!sys.export_data<Manufacturer>("res/manfdata_out.csv")) return -1; if (!sys.export_data<App>("res/appdata_out.csv")) return -1; if (!sys.export_data<Review>("res/revs_out.csv")) return -1; - std::cout << std::endl << "Thank you :)" << std::endl; + std::printf("\nThank you :)\n"); return 0; } @@ -66,10 +66,9 @@ operator<< (std::ostream& stream, const AppSystem& sys) void cont(const char *s) { - std::cout << std::endl; - std::cout << "Press <ENTER> to continue. . ."; + std::printf("\nPress <ENTER> to continue. . ."); if (std::cin.get()) system("clear || cls"); - if (strlen(s) > 0) std::cout << s << std::endl << std::endl; + if (strlen(s) > 0) std::printf("%s\n\n", s); } void @@ -87,20 +86,24 @@ pluseqs(AppSystem& sys) void edit(AppSystem& sys) { - sys.newrev("minecurses", new Review(5, "gamer", "Good game")); - sys.newrevs("Vim", + sys.call<Review *>("minecurses", new Review(5, "gamer", "Good game"), + &App::addrev); + sys.call<const std::vector<Review *>>("Vim", {new Review(2, "user1", "Not so good"), new Review(4, "user2", "Good app"), - new Review(5, "user3", "Very good :)")}); - sys.chserialnum("zathura", "1254"); - sys.chname("minecurses", "minesweeper"); - sys.chos("Vim", "GoodOS"); - sys.chmanf("LibreOffice", - new Manufacturer("0006", "FreeSoftware", "freesoft@freesoft.com")); - sys.chprice("LoL", 155); - sys.chgenre("CS:GO", "Shooter"); - sys.chonline("minesweeper", true); - sys.chexts("zathura", {".tex", ".epub", ".ms"}); + new Review(5, "user3", "Very good :)")}, + &App::addrevs); + sys.call<const char *>("zathura", "1254", &App::set_serialnum); + sys.call<const std::string&>("minecurses", "minesweeper", &App::set_name); + sys.call<const std::string&>("Vim", "GoodOS", &App::set_os); + sys.call<Manufacturer *>("LibreOffice", + new Manufacturer("0006", "FreeSoftware", "freesoft@freesoft.com"), + &App::set_manf); + sys.call<int>("LoL", 155, &App::set_price); + sys.cast_call<const std::string&, Game>("CS:GO", "Shooter", &Game::set_genre); + sys.cast_call<bool, Game>("minesweeper", true, &Game::set_online); + sys.cast_call<const std::vector<std::string>&, Office>("zathura", + {".tex", ".epub", ".ms"}, &Office::set_exts); } void 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/errlog.o b/assignment-2.5-spreadsheets/obj/errlog.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/src/datahandler.cpp b/assignment-2.5-spreadsheets/src/datahandler.cpp @@ -26,10 +26,8 @@ DataHandler::store_data() f.open(datapath); if (f.is_open()) { - std::cout << "Importing data from \'" << datapath << "\'." << std::endl; - std::cout << std::endl; - std::cout << "Making data structures and analyzing data." << std::endl; - + std::printf("Importing data from \'%s\'\n\n", datapath); + std::printf("Making data structures and analyzing data.\n"); lab::xstring skip; lab::getline(f, skip); @@ -86,23 +84,19 @@ DataHandler::analyze( } grds.insert(std::make_pair(courses[code], grade)); } - else if (its == studs.end()) + else if (its == studs.end() && + std::find(errs.begin(), errs.end(), id) == errs.end()) { - if (std::find(errs.begin(), errs.end(), id) == errs.end()) - { errs.push_back(id); errlog.write(ErrLog::ErrType::STUDENT_MISSING, id); errcount++; - } } - else if (itc == courses.end()) + else if (itc == courses.end() && + std::find(errs.begin(), errs.end(), code) == errs.end()) { - if (std::find(errs.begin(), errs.end(), code) == errs.end()) - { errs.push_back(code); errlog.write(ErrLog::ErrType::COURSE_MISSING, code); errcount++; - } } if (its != studs.end() && itc != courses.end()) @@ -110,7 +104,6 @@ DataHandler::analyze( miss(id, code, grade); diffr(id, code, grade); } - return true; } @@ -168,7 +161,7 @@ DataHandler::make_report() const f.open(reppath); if (f.is_open()) { - std::cout << "Making report." << std::endl; + std::printf("Making report.\n"); f << "ID;Last name;First name;New course code;New course name;" << "Old course code;Old course name;Grade" << std::endl; for (const auto& m : missing) f << m << std::endl; @@ -191,15 +184,13 @@ DataHandler::summary() const for (const auto& dat : data) datacount += dat.second.size(); - std::cout << std::endl; - std::cout << "Students: " << studs.size() << std::endl; - std::cout << "Courses: " << courses.size() << std::endl; - std::cout << "Equivalencies: " << eqvs.size() << std::endl; - std::cout << "Total grades stored: " << datacount << std::endl; - std::cout << "Grades missing: " << misscount << std::endl; - std::cout << "Errors: " << errcount << std::endl; - std::cout << std::endl; - std::cout << "Thank you :)" << std::endl; + std::printf("\nStudents: %ld\n", studs.size()); + std::printf("Courses: %ld\n", courses.size()); + std::printf("Equivalences: %ld\n", eqvs.size()); + std::printf("Total grades stored: %d\n", datacount); + std::printf("Grades missing: %d\n", misscount); + std::printf("Errors: %d\n", errcount); + std::printf("\nThank you :)\n"); } bool diff --git a/assignment-2.5-spreadsheets/src/datahandler.h b/assignment-2.5-spreadsheets/src/datahandler.h @@ -17,8 +17,8 @@ using equivalencies = std::map<lab::xstring, lab::xstring>; class DataHandler { private: - const char *datapath = "res/grades.csv"; - const char *reppath = "res/report.csv"; + static constexpr const char *datapath = "res/grades.csv"; + static constexpr const char *reppath = "res/report.csv"; std::map<lab::xstring, Course *> courses; std::map<lab::xstring, Student *> studs; std::map<Course *, float> grds; @@ -65,7 +65,7 @@ DataHandler::import_data(const char *fpath) f.open(fpath); if (f.is_open()) { - std::cout << "Importing data from \'" << fpath << "\'." << std::endl; + std::printf("Importing data from \'%s\'\n", fpath); lab::xstring skip; lab::getline(f, skip); while (f.good()) diff --git a/assignment-2.5-spreadsheets/src/errlog.h b/assignment-2.5-spreadsheets/src/errlog.h @@ -7,7 +7,7 @@ class ErrLog { private: - const char *fpath = "res/errlog.csv"; + static constexpr const char *fpath = "res/errlog.csv"; void fclear() const; public: