commit af6c439ab1462502f8527887927b7425d5958cb2 parent d89c3f0105c81c6cbbe64efda69910c71296de54 Author: Christos Margiolis <christos@margiolis.net> Date: Thu, 14 May 2020 02:06:59 +0300 pending first grade eating fix Diffstat:
18 files changed, 488 insertions(+), 209 deletions(-)
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/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -26,7 +26,7 @@ App::~App() if (serialnum != nullptr) delete[] serialnum; if (!reviews.empty()) { - for (auto& rev :reviews) + for (auto&& rev :reviews) delete rev; reviews.clear(); } diff --git a/assignment-2.4-inheritance/src/appsystem.cpp b/assignment-2.4-inheritance/src/appsystem.cpp @@ -37,14 +37,14 @@ void AppSystem::write_office_exts(Office *of, std::ofstream& f) { std::vector<std::string> exts = of->get_exts(); - for (auto& ext : exts) + for (const auto& ext : exts) f << ext << '|'; } void AppSystem::newrev(const std::string& appname, Review *rev) { - for (auto& app : apps) + for (auto&& app : apps) if (app->get_name() == appname) app->addrev(rev); } @@ -52,7 +52,7 @@ AppSystem::newrev(const std::string& appname, Review *rev) void AppSystem::newrevs(const std::string& appname, const std::vector<Review *> revs) { - for (auto& app : apps) + for (auto&& app : apps) if (app->get_name() == appname) app->addrevs(revs); } @@ -60,7 +60,7 @@ AppSystem::newrevs(const std::string& appname, const std::vector<Review *> revs) void AppSystem::chserialnum(const std::string& appname, const char *serialnum) { - for (auto& app : apps) + for (auto&& app : apps) if (app->get_name() == appname) app->set_serialnum(serialnum); } @@ -68,7 +68,7 @@ AppSystem::chserialnum(const std::string& appname, const char *serialnum) void AppSystem::chname(const std::string& appname, const std::string& name) { - for (auto& app : apps) + for (auto&& app : apps) if (app->get_name() == appname) app->set_name(name); } @@ -76,7 +76,7 @@ AppSystem::chname(const std::string& appname, const std::string& name) void AppSystem::chos(const std::string& appname, const std::string& os) { - for (auto& app : apps) + for (auto&& app : apps) if (app->get_name() == appname) app->set_os(os); } @@ -84,7 +84,7 @@ AppSystem::chos(const std::string& appname, const std::string& os) void AppSystem::chmanf(const std::string& appname, Manufacturer *manf) { - for (auto& app : apps) + for (auto&& app : apps) if (app->get_name() == appname) app->set_manf(manf); } @@ -92,7 +92,7 @@ AppSystem::chmanf(const std::string& appname, Manufacturer *manf) void AppSystem::chprice(const std::string& appname, int price) { - for (auto& app : apps) + for (auto&& app : apps) if (app->get_name() == appname) app->set_price(price); } @@ -100,7 +100,7 @@ AppSystem::chprice(const std::string& appname, int price) void AppSystem::chgenre(const std::string& appname, const std::string& genre) { - for (auto& app : apps) + for (const auto& app : apps) if (Game *o = dynamic_cast<Game *>(app)) if (o->get_name() == appname) o->set_genre(genre); @@ -109,7 +109,7 @@ AppSystem::chgenre(const std::string& appname, const std::string& genre) void AppSystem::chonline(const std::string& appname, bool online) { - for (auto& app : apps) + for (const auto& app : apps) if (Game *o = dynamic_cast<Game *>(app)) if (o->get_name() == appname) o->set_online(online); @@ -118,7 +118,7 @@ AppSystem::chonline(const std::string& appname, bool online) void AppSystem::chexts(const std::string& appname, const std::vector<std::string> exts) { - for (auto& app : apps) + for (const auto& app : apps) if (Office *o = dynamic_cast<Office *>(app)) if (o->get_name() == appname) o->set_exts(exts); @@ -152,7 +152,7 @@ const std::vector<Office *> AppSystem::get_freeapps() const { std::vector<Office *> fapps; - for (auto& app : apps) + for (const auto& app : apps) if (Office *o = dynamic_cast<Office *>(app)) if (o->get_price() == 0) fapps.push_back(o); @@ -163,7 +163,7 @@ const std::vector<Game *> AppSystem::get_goodgames() const { std::vector<Game *> ggames; - for (auto& app : apps) + for (const auto& app : apps) { if (Game *o = dynamic_cast<Game *>(app)) { @@ -171,7 +171,7 @@ AppSystem::get_goodgames() const if (!revs.empty()) { int sum = 0, count = 0; - for (auto& rev : revs) + for (const auto& rev : revs) { sum += rev->get_stars(); count++; 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/app.o b/assignment-2.5-spreadsheets/obj/app.o 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/obj/report.o b/assignment-2.5-spreadsheets/obj/report.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/res/errlog.csv b/assignment-2.5-spreadsheets/res/errlog.csv @@ -0,0 +1,163 @@ +Type;Info;Err +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8010;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8010;Missing. +Course;P1-8010;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8010;Missing. +Course;P1-8010;Missing. +Course;P1-8010;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8010;Missing. +Course;P1-8020;Missing. +Course;P1-8020;Missing. +Course;P1-8010;Missing. +Student;SR001950;Missing. +Student;SR001951;Missing. +Student;SR001951;Missing. +Student;SR001951;Missing. +Student;SR001952;Missing. +Student;SR001952;Missing. +Student;SR001952;Missing. +Student;SR001952;Missing. +Student;SR001952;Missing. +Student;SR001953;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001954;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001955;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001956;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001957;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001958;Missing. +Student;SR001959;Missing. +Student;SR001959;Missing. +Student;SR001959;Missing. +Student;SR001959;Missing. +Student;SR001959;Missing. +Student;SR001959;Missing. +Student;SR001959;Missing. +Student;SR001959;Missing. +Student;SR001960;Missing. +Student;SR001960;Missing. +Student;SR001960;Missing. +Student;SR001960;Missing. +Student;SR001960;Missing. +Student;SR001961;Missing. +Student;SR001961;Missing. +Student;SR001961;Missing. +Student;SR001961;Missing. +Student;SR001961;Missing. +Student;SR001962;Missing. +Student;SR001962;Missing. +Student;SR001962;Missing. +Student;SR001962;Missing. +Student;SR001963;Missing. +Student;SR001963;Missing. +Student;SR001963;Missing. +Student;SR001964;Missing. +Student;SR001964;Missing. +Student;SR001964;Missing. +Student;SR001964;Missing. +Student;SR001964;Missing. +Student;SR001964;Missing. +Student;SR001964;Missing. +Student;SR001964;Missing. +Student;SR001965;Missing. +Student;SR001965;Missing. +Student;SR001965;Missing. +Student;SR001965;Missing. +Student;SR001965;Missing. +Student;SR001965;Missing. +Student;SR001965;Missing. +Student;SR001966;Missing. +Student;SR001966;Missing. +Student;SR001966;Missing. +Student;SR001966;Missing. +Student;SR001966;Missing. +Student;SR001966;Missing. +Student;SR001966;Missing. +Student;SR001966;Missing. diff --git a/assignment-2.5-spreadsheets/src/app.cpp b/assignment-2.5-spreadsheets/src/app.cpp @@ -1,78 +0,0 @@ -#include "app.h" - -App::App() {} - -App::~App() -{ - dealloc<Course>(courses); - dealloc<Student>(studs); - eqvs.clear(); -} - -void -App::store_data() -{ - std::ifstream f; - const char *fpath = "res/grades.csv"; - f.exceptions(std::ifstream::badbit); - try - { - if (!valid_path(fpath)) - throw std::runtime_error(err_csv(fpath).cstr()); - f.open(fpath); - if (f.is_open()) - { - lab::xstring skip; - lab::getline(f, skip); - while (f.good()) - { - lab::xstring AM, code, grade; - lab::getline(f, AM, ';'); - lab::getline(f, code, ';'); - lab::getline(f, grade); - if (f.eof()) break; - } - } - f.close(); - } - catch (const std::ifstream::failure& e) - { - std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; - } -} - -void -App::analyze() -{ - -} - -bool -App::valid_path(const char *fpath) -{ - return (strstr(fpath, ".csv") != nullptr); -} - -const lab::xstring -App::err_csv(const char *fpath) -{ - lab::xstring err = "Error. File must be of format \'.csv\'. ()."; - err.insert(fpath, 39); - return err; -} - -const lab::xstring -App::err_read(const char *fpath) -{ - lab::xstring err = "Error reading file \'\'."; - err.insert(fpath, 20); - return err; -} - -const lab::xstring -App::err_write(const char *fpath) -{ - lab::xstring err = "Error writing to file \'\'."; - err.insert(fpath, 23); - return err; -} diff --git a/assignment-2.5-spreadsheets/src/app.h b/assignment-2.5-spreadsheets/src/app.h @@ -1,102 +0,0 @@ -#ifndef APP_H -#define APP_H - -#include <iostream> -#include <fstream> -#include <map> -#include <vector> - -#include "course.h" -#include "student.h" -#include "xstring.h" - -typedef std::map<lab::xstring, lab::xstring> equivalencies; - -class App -{ - private: - std::map<lab::xstring, Course *> courses; - std::map<lab::xstring, Student *> studs; - std::map<Student *, std::map<Course *, float>> data; - equivalencies eqvs; - - public: - App(); - ~App(); - - template<typename T> bool import_data(const char *fpath); - void store_data(); - void analyze(); - - private: - bool valid_path(const char *fpath); - const lab::xstring err_csv(const char *fpath); - const lab::xstring err_read(const char *fpath); - const lab::xstring err_write(const char *fpath); - template<typename T> void dealloc(std::map<lab::xstring, T *>& vec); -}; - -template<typename T> bool -App::import_data(const char *fpath) -{ - std::ifstream f; - f.exceptions(std::ifstream::badbit); - try - { - if (!valid_path(fpath)) - throw std::runtime_error(err_csv(fpath).cstr()); - f.open(fpath); - if (f.is_open()) - { - lab::xstring skip; - lab::getline(f, skip); - while (f.good()) - { - if (std::is_same<T, Course>::value) - { - lab::xstring code, name; - lab::getline(f, code, ';'); - lab::getline(f, name); - if (f.eof()) break; - courses.insert(std::make_pair(code, new Course(code, name))); - } - else if (std::is_same<T, Student>::value) - { - lab::xstring AM, lname, fname; - lab::getline(f, AM, ';'); - lab::getline(f, lname, ';'); - lab::getline(f, fname); - if (f.eof()) break; - studs.insert(std::make_pair(AM, new Student(AM, lname, fname))); - } - else if (std::is_same<T, equivalencies>::value) - { - lab::xstring newcurr, oldcurr; - lab::getline(f, newcurr, ';'); - lab::getline(f, oldcurr); - if (f.eof()) break; - eqvs.insert(std::make_pair(newcurr, oldcurr)); - } - } - } - f.close(); - } - catch (const std::ifstream::failure& e) - { - std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; - return false; - } - return true; -} - -template<typename T> void -App::dealloc(std::map<lab::xstring, T *>& vec) -{ - for (auto& v : vec) - if (v.second != nullptr) - delete v.second; - if (!vec.empty()) - vec.clear(); -} - -#endif /* APP_H */ diff --git a/assignment-2.5-spreadsheets/src/datahandler.cpp b/assignment-2.5-spreadsheets/src/datahandler.cpp @@ -0,0 +1,129 @@ +#include "datahandler.h" + +DataHandler::DataHandler() {} + +DataHandler::~DataHandler() +{ + dealloc<Course>(courses); + dealloc<Student>(studs); + eqvs.clear(); +} + +bool +DataHandler::store_data() +{ + std::ifstream f; + f.exceptions(std::ifstream::badbit); + try + { + if (!valid_path(datapath)) + throw std::runtime_error(err_csv(datapath).cstr()); + f.open(datapath); + if (f.is_open()) + { + lab::xstring skip; + lab::getline(f, skip); + + lab::xstring AM, code, grade; + lab::getline(f, AM, ';'); + lab::getline(f, code, ';'); + lab::getline(f, grade); + if (studs[AM] && courses[code]) + grd.insert(std::make_pair(courses[code], + std::atof(grade.cstr()))); + + while (f.good()) + { + lab::xstring currAM = AM; + // eats up first grade of each student + while (currAM == AM) + { + lab::getline(f, AM, ';'); + lab::getline(f, code, ';'); + lab::getline(f, grade); + if (f.eof()) break; + if (!analyze(currAM, AM, code, grade)) break; + } + } + for (const auto& d : data) + { + std::cout << d.first->get_AM() << std::endl << std::endl; + for (const auto& da : d.second) + std::cout << da.first->get_code() << " " << da.second << std::endl; + std::cout << std::endl; + } + } + f.close(); + } + catch (const std::ifstream::failure& e) + { + std::cerr << err_read(datapath) << std::endl << e.what() << std::endl; + return false; + } + return true; +} + +bool +DataHandler::analyze( + const lab::xstring& currAM, + const lab::xstring& AM, + const lab::xstring& code, + const lab::xstring& grade) +{ + if (studs[AM] && courses[code]) + { + if (currAM != AM) + { + data.insert(std::make_pair(studs[currAM], grd)); + grd.clear(); + return false; + } + grd.insert(std::make_pair(courses[code], + std::atof(grade.cstr()))); + } + else if (!studs[AM]) + errlog.write(ErrLog::ErrType::STUDENT_MISSING, AM); + else if (!courses[code]) + errlog.write(ErrLog::ErrType::COURSE_MISSING, code); + + return true; +} + +bool +DataHandler::make_report() const +{ + std::ofstream f; + f.open(reppath); + f.close(); + return true; +} + +bool +DataHandler::valid_path(const char *fpath) const +{ + return (strstr(fpath, ".csv") != nullptr); +} + +const lab::xstring +DataHandler::err_csv(const char *fpath) const +{ + lab::xstring err = "Error. File must be of format \'.csv\'. ()."; + err.insert(fpath, 39); + return err; +} + +const lab::xstring +DataHandler::err_read(const char *fpath) const +{ + lab::xstring err = "Error reading file \'\'."; + err.insert(fpath, 20); + return err; +} + +const lab::xstring +DataHandler::err_write(const char *fpath) const +{ + lab::xstring err = "Error writing to file \'\'."; + err.insert(fpath, 23); + return err; +} diff --git a/assignment-2.5-spreadsheets/src/datahandler.h b/assignment-2.5-spreadsheets/src/datahandler.h @@ -0,0 +1,112 @@ +#ifndef DATA_HANDLER_H +#define DATA_HANDLER_H + +#include <iostream> +#include <fstream> +#include <map> +#include <vector> + +#include "course.h" +#include "errlog.h" +#include "student.h" +#include "xstring.h" + +typedef std::map<lab::xstring, lab::xstring> equivalencies; + +class DataHandler +{ + private: + const char *datapath = "res/grades.csv"; + const char *reppath = "res/report.csv"; + std::map<lab::xstring, Course *> courses; + std::map<lab::xstring, Student *> studs; + std::map<Course *, float> grd; + std::map<Student *, std::map<Course *, float>> data; + equivalencies eqvs; + ErrLog errlog; + + public: + DataHandler(); + ~DataHandler(); + + template<typename T> bool import_data(const char *fpath); + bool store_data(); + bool make_report() const; + + private: + bool analyze( + const lab::xstring& currAM, + const lab::xstring& AM, + const lab::xstring& code, + const lab::xstring& grade); + bool valid_path(const char *fpath) const; + const lab::xstring err_csv (const char *fpath) const; + const lab::xstring err_read (const char *fpath) const; + const lab::xstring err_write(const char *fpath) const; + template<typename T> void dealloc(std::map<lab::xstring, T *>& vec); +}; + +template<typename T> bool +DataHandler::import_data(const char *fpath) +{ + std::ifstream f; + f.exceptions(std::ifstream::badbit); + try + { + if (!valid_path(fpath)) + throw std::runtime_error(err_csv(fpath).cstr()); + f.open(fpath); + if (f.is_open()) + { + lab::xstring skip; + lab::getline(f, skip); + while (f.good()) + { + if (std::is_same<T, Course>::value) + { + lab::xstring code, name; + lab::getline(f, code, ';'); + lab::getline(f, name); + if (f.eof()) break; + courses.insert(std::make_pair(code, new Course(code, name))); + } + else if (std::is_same<T, Student>::value) + { + lab::xstring AM, lname, fname; + lab::getline(f, AM, ';'); + lab::getline(f, lname, ';'); + lab::getline(f, fname); + if (f.eof()) break; + studs.insert(std::make_pair(AM, new Student(AM, lname, fname))); + } + else if (std::is_same<T, equivalencies>::value) + { + lab::xstring newcurr, oldcurr; + lab::getline(f, newcurr, ';'); + lab::getline(f, oldcurr); + if (f.eof()) break; + eqvs.insert(std::make_pair(newcurr, oldcurr)); + } + } + } + f.close(); + } + catch (const std::ifstream::failure& e) + { + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; + return false; + } + return true; +} + +template<typename T> void +DataHandler::dealloc(std::map<lab::xstring, T *>& vec) +{ + for (auto&& v : vec) + if (v.second != nullptr) + delete v.second; + if (!vec.empty()) + vec.clear(); +} + +#endif /* DATA_HANDLER_H */ diff --git a/assignment-2.5-spreadsheets/src/errlog.cpp b/assignment-2.5-spreadsheets/src/errlog.cpp @@ -0,0 +1,33 @@ +#include "errlog.h" + +ErrLog::ErrLog() {fclear();} + +void +ErrLog::fclear() const +{ + std::ofstream f; + f.open(fpath, std::ofstream::trunc); + f << "Type;Info;Err" << std::endl; + f.close(); +} + +void +ErrLog::write(ErrType type, const lab::xstring& s) const +{ + std::ofstream f; + f.open(fpath, std::ios_base::app); + switch(type) + { + case ErrType::STUDENT_MISSING: + f << "Student;" << s << ";Missing." << std::endl; + break; + case ErrType::COURSE_MISSING: + f << "Course;" << s << ";Missing." << std::endl; + break; + case ErrType::GRADE_DUPLICATE: + break; + case ErrType::MULTIPLE_GRADES: + break; + } + f.close(); +} diff --git a/assignment-2.5-spreadsheets/src/errlog.h b/assignment-2.5-spreadsheets/src/errlog.h @@ -0,0 +1,29 @@ +#ifndef ERRLOG_H +#define ERRLOG_H + +#include <fstream> +#include "xstring.h" + +class ErrLog +{ + private: + const char *fpath = "res/errlog.csv"; + + public: + enum class ErrType { + STUDENT_MISSING, + COURSE_MISSING, + GRADE_DUPLICATE, + MULTIPLE_GRADES + }; + + public: + ErrLog(); + + void write(ErrType type, const lab::xstring& s) const; + + private: + void fclear() const; +}; + +#endif /* ERRLOG_H */ diff --git a/assignment-2.5-spreadsheets/src/main.cpp b/assignment-2.5-spreadsheets/src/main.cpp @@ -1,21 +1,14 @@ -#include "app.h" - -std::ostream& operator<< (std::ostream& stream, const App& app); +#include "datahandler.h" int main(int argc, char **argv) { - App app; - if (!app.import_data<Course>("res/courses.csv")) return -1; - if (!app.import_data<Student>("res/students.csv")) return -1; - if (!app.import_data<equivalencies>("res/equivalencies.csv")) return -1; - app.store_data(); + DataHandler dh; + if (!dh.import_data<Course>("res/courses.csv")) return -1; + if (!dh.import_data<Student>("res/students.csv")) return -1; + if (!dh.import_data<equivalencies>("res/equivalencies.csv")) return -1; + if (!dh.store_data()) return -1; + if (!dh.make_report()) return -1; return 0; } - -std::ostream& -operator<< (std::ostream& stream, const App& app) -{ - return stream; -}