uni

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

commit e7298c52920b85e856ec53760fd4df9ab35bf6be
parent 0254972c4a3fd9e4cb35488f6035d2d7fbabb3db
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue, 12 May 2020 23:01:51 +0300

pending map fix

Diffstat:
Massignment-2.4-inheritance/bin/inheritance | 0
Massignment-2.4-inheritance/obj/appsystem.o | 0
Massignment-2.5-spreadsheets/bin/spreadsheets | 0
Massignment-2.5-spreadsheets/obj/app.o | 0
Massignment-2.5-spreadsheets/obj/grade.o | 0
Massignment-2.5-spreadsheets/obj/main.o | 0
Massignment-2.5-spreadsheets/obj/xstring.o | 0
Massignment-2.5-spreadsheets/src/app.cpp | 54++++++++++++++++++++++++++++++++++--------------------
Massignment-2.5-spreadsheets/src/app.h | 40+++++++++++++---------------------------
Dassignment-2.5-spreadsheets/src/grade.cpp | 4----
Dassignment-2.5-spreadsheets/src/grade.h | 17-----------------
Massignment-2.5-spreadsheets/src/main.cpp | 7+------
Massignment-2.5-spreadsheets/src/xstring.cpp | 10+++-------
13 files changed, 51 insertions(+), 81 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.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/grade.o b/assignment-2.5-spreadsheets/obj/grade.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/xstring.o b/assignment-2.5-spreadsheets/obj/xstring.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/src/app.cpp b/assignment-2.5-spreadsheets/src/app.cpp @@ -5,32 +5,46 @@ App::App() {} App::~App() { dealloc<Course>(courses); - dealloc<Grade>(grades); - dealloc<Student>(studs); + dealloc<Student>(studs); + eqvs.clear(); } -const std::vector<Course *>& -App::get_courses() const +void +App::store_data() { - return courses; + for (auto& stud : studs) + std::cout << stud.first << std::endl; } -const std::vector<Grade *>& -App::get_grades() const +void +App::analyze() { - return grades; -} - -const std::vector<Student *>& -App::get_studs() const -{ - return studs; -} - -const equivalencies& -App::get_eqvs() const -{ - return eqvs; + 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); + } + } + f.close(); + } + catch (const std::ifstream::failure& e) + { + std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; + } } bool diff --git a/assignment-2.5-spreadsheets/src/app.h b/assignment-2.5-spreadsheets/src/app.h @@ -7,7 +7,6 @@ #include <vector> #include "course.h" -#include "grade.h" #include "student.h" #include "xstring.h" @@ -16,9 +15,9 @@ typedef std::map<lab::xstring, lab::xstring> equivalencies; class App { private: - std::vector<Course *> courses; - std::vector<Grade *> grades; - std::vector<Student *> studs; + std::map<lab::xstring, Course *> courses; + std::map<lab::xstring, Student *> studs; + std::map<Student *, std::map<Course *, float>> data; equivalencies eqvs; public: @@ -26,18 +25,15 @@ class App ~App(); template<typename T> bool import_data(const char *fpath); + void store_data(); + void analyze(); - const std::vector<Course *>& get_courses() const; - const std::vector<Grade *>& get_grades() const; - const std::vector<Student *>& get_studs() const; - const equivalencies& get_eqvs() const; - 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::vector<T *>& vec); + template<typename T> void dealloc(std::map<lab::xstring, T *>& vec); }; template<typename T> bool @@ -62,17 +58,7 @@ App::import_data(const char *fpath) lab::getline(f, code, ';'); lab::getline(f, name); if (f.eof()) break; - courses.push_back(new Course(code, name)); - } - else if (std::is_same<T, Grade>::value) - { - lab::xstring AM, code, grade; - lab::getline(f, AM, ';'); - lab::getline(f, code, ';'); - lab::getline(f, grade); - if (f.eof()) break; - grades.push_back(new Grade(AM, code, - std::atof(grade.cstr()))); + courses.insert(std::make_pair(code, new Course(code, name))); } else if (std::is_same<T, Student>::value) { @@ -81,7 +67,7 @@ App::import_data(const char *fpath) lab::getline(f, lname, ';'); lab::getline(f, fname); if (f.eof()) break; - studs.push_back(new Student(AM, lname, fname)); + studs.insert(std::make_pair(AM, new Student(AM, lname, fname))); } else if (std::is_same<T, equivalencies>::value) { @@ -89,9 +75,9 @@ App::import_data(const char *fpath) lab::getline(f, newcurr, ';'); lab::getline(f, oldcurr); if (f.eof()) break; - eqvs.emplace(newcurr, oldcurr); + eqvs.insert(std::make_pair(newcurr, oldcurr)); } - } + } } f.close(); } @@ -104,11 +90,11 @@ App::import_data(const char *fpath) } template<typename T> void -App::dealloc(std::vector<T *>& vec) +App::dealloc(std::map<lab::xstring, T *>& vec) { for (auto& v : vec) - if (v != nullptr) - delete v; + if (v.second != nullptr) + delete v.second; if (!vec.empty()) vec.clear(); } diff --git a/assignment-2.5-spreadsheets/src/grade.cpp b/assignment-2.5-spreadsheets/src/grade.cpp @@ -1,4 +0,0 @@ -#include "grade.h" - -Grade::Grade(const lab::xstring& AM, const lab::xstring& code, float grade) - :AM(AM), code(code), grade(grade) {} diff --git a/assignment-2.5-spreadsheets/src/grade.h b/assignment-2.5-spreadsheets/src/grade.h @@ -1,17 +0,0 @@ -#ifndef GRADE_H -#define GRADE_H - -#include "xstring.h" - -class Grade -{ - private: - lab::xstring AM; - lab::xstring code; - float grade; - - public: - Grade(const lab::xstring& AM, const lab::xstring& code, float grade); -}; - -#endif /* GRADE_H */ diff --git a/assignment-2.5-spreadsheets/src/main.cpp b/assignment-2.5-spreadsheets/src/main.cpp @@ -7,14 +7,9 @@ main(int argc, char **argv) { App app; if (!app.import_data<Course>("res/courses.csv")) return -1; - if (!app.import_data<Grade>("res/grades.csv")) return -1; if (!app.import_data<Student>("res/students.csv")) return -1; if (!app.import_data<equivalencies>("res/equivalencies.csv")) return -1; - - equivalencies a = app.get_eqvs(); - equivalencies::const_iterator it; - for (it = a.begin(); it != a.end(); it++) - std::cout << it->first << " " << it->second << std::endl; + app.store_data(); return 0; } diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -3,7 +3,7 @@ namespace lab { xstring::xstring() { - str = new char[0]; + str = new char[1]; str[0] = '\0'; len = 0; } @@ -263,15 +263,10 @@ xstring::pop_back() char *tmp = new char[len]; std::copy(str, str + len, tmp); delete[] str; - tmp[len - 1] = '\0'; + tmp[len-1] = '\0'; str = tmp; len--; } - else if (len - 1 == 0) - { - delete[] str; - clear(); - } else return; } @@ -353,6 +348,7 @@ xstring::conv(const char *s) const std::size_t l = strlen(s); char *tmp = new char[l + 1]; std::copy(s, s + l+1, tmp); + tmp[l] = '\0'; return tmp; }