uni

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

commit cefde2e4590048c9e9991ee5301101f0de32c437
parent a0c986a5c03ceef781a27ed89f2c05c9499e5271
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu,  7 May 2020 17:25:05 +0300

fixed exception handling, added print pure virtual

Diffstat:
Massignment-2.4-inheritance/bin/inheritance | 0
Massignment-2.4-inheritance/obj/app.o | 0
Massignment-2.4-inheritance/obj/appsystem.o | 0
Massignment-2.4-inheritance/obj/game.o | 0
Massignment-2.4-inheritance/obj/main.o | 0
Massignment-2.4-inheritance/obj/manufacturer.o | 0
Massignment-2.4-inheritance/obj/office.o | 0
Massignment-2.4-inheritance/obj/review.o | 0
Massignment-2.4-inheritance/src/app.cpp | 27++++++++-------------------
Massignment-2.4-inheritance/src/app.h | 7++++++-
Massignment-2.4-inheritance/src/appsystem.h | 16+++++-----------
Massignment-2.4-inheritance/src/game.cpp | 47++++++++++++++++++++++++++++++++++++++++-------
Massignment-2.4-inheritance/src/game.h | 2++
Massignment-2.4-inheritance/src/main.cpp | 47+----------------------------------------------
Massignment-2.4-inheritance/src/manufacturer.cpp | 16+++-------------
Massignment-2.4-inheritance/src/manufacturer.h | 2+-
Massignment-2.4-inheritance/src/office.cpp | 41+++++++++++++++++++++++++++++++++++++++++
Massignment-2.4-inheritance/src/office.h | 2++
Massignment-2.4-inheritance/src/review.cpp | 7+++++--
Massignment-2.4-inheritance/src/review.h | 1+
Aassignment-2.5-spreadsheets/bin/spreadsheets | 0
Aassignment-2.5-spreadsheets/obj/app.o | 0
Aassignment-2.5-spreadsheets/obj/course.o | 0
Aassignment-2.5-spreadsheets/obj/grades.o | 0
Aassignment-2.5-spreadsheets/obj/main.o | 0
Aassignment-2.5-spreadsheets/obj/student.o | 0
Aassignment-2.5-spreadsheets/obj/xstring.o | 0
Massignment-2.5-spreadsheets/src/app.cpp | 10+++++++++-
Massignment-2.5-spreadsheets/src/app.h | 5+++--
Massignment-2.5-spreadsheets/src/main.cpp | 7++++++-
Massignment-2.5-spreadsheets/src/xstring.cpp | 18+++++++++++++++---
Massignment-2.5-spreadsheets/src/xstring.h | 4+++-
32 files changed, 151 insertions(+), 108 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/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/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/app.cpp b/assignment-2.4-inheritance/src/app.cpp @@ -7,31 +7,20 @@ App::App(const char *serialnum, const std::string& name, const std::string& os, Manufacturer *manf, int price) :serialnum(convsn(serialnum)), name(name), os(os), manf(manf), price(price) { - try - { - if (strcmp(serialnum, "") == 0) throw serialnum; - if (name.empty()) throw name; - if (os.empty()) throw os; - if (price < 0) throw price; - } - catch (const char *serialnum) - { + if (strcmp(serialnum, "") == 0) throw std::runtime_error("Empty app serial number."); - } - catch (const std::string& name) - { + if (name.empty()) throw std::runtime_error("Empty app name."); - } - catch (const std::string& os) - { + if (os.empty()) throw std::runtime_error("Empty OS name."); - } - catch (int price) - { + if (price < 0) throw std::runtime_error("Price can't have negative value."); - } } +App::App(const App& a) + :serialnum(a.serialnum), name(a.name), os(a.os), manf(a.manf), + price(a.price) {} + App::~App() { if (serialnum != nullptr) delete[] serialnum; diff --git a/assignment-2.4-inheritance/src/app.h b/assignment-2.4-inheritance/src/app.h @@ -1,9 +1,11 @@ #ifndef APP_H #define APP_H +#include <cstring> +#include <iomanip> +#include <iostream> #include <stdexcept> #include <string> -#include <string.h> #include <vector> #include "manufacturer.h" @@ -23,6 +25,7 @@ class App App(); App(const char *serialnum, const std::string& name, const std::string& os, Manufacturer *manf, int price); + App(const App& app); virtual ~App(); void addrev(Review *rev); @@ -40,6 +43,8 @@ class App void set_os(const std::string& os); void set_manf(Manufacturer *manf); void set_price(int price); + + virtual void print(std::ostream& stream) const = 0; private: char *convsn(const char *serialnum); diff --git a/assignment-2.4-inheritance/src/appsystem.h b/assignment-2.4-inheritance/src/appsystem.h @@ -5,6 +5,7 @@ #include <fstream> #include <iostream> #include <sstream> + #include "game.h" #include "office.h" @@ -140,8 +141,8 @@ bool AppSystem::import_data(const char *fpath) try { std::string strpath(fpath); - if (!valid_path(strpath)) throw strpath; - + if (!valid_path(strpath)) + throw std::runtime_error(err_csv(strpath)); f.open(fpath); if (f.is_open()) { @@ -178,10 +179,6 @@ bool AppSystem::import_data(const char *fpath) } f.close(); } - catch (const std::string& strpath) - { - throw std::runtime_error(err_csv(strpath)); - } catch (const std::ifstream::failure& e) { std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; @@ -198,7 +195,8 @@ bool AppSystem::export_data(const char *fpath) try { std::string strpath(fpath); - if (!valid_path(strpath)) throw strpath; + if (!valid_path(strpath)) + throw std::runtime_error(err_csv(strpath)); f.open(fpath); if (std::is_same<T, Manufacturer>::value) @@ -250,10 +248,6 @@ bool AppSystem::export_data(const char *fpath) } f.close(); } - catch (const std::string& strpath) - { - throw std::runtime_error(err_csv(strpath)); - } catch (const std::ofstream::failure& e) { std::cerr << err_read(fpath) << std::endl << e.what() << std::endl; diff --git a/assignment-2.4-inheritance/src/game.cpp b/assignment-2.4-inheritance/src/game.cpp @@ -8,16 +8,14 @@ Game::Game(const char *serialnum, const std::string& name, const std::string& genre, bool online) :App(serialnum, name, os, manf, price), genre(genre), online(online) { - try - { - if (genre.empty() || genre == "") throw genre; - } - catch (const std::string& genre) - { + if (genre.empty() || genre == "") throw std::runtime_error("Empty game genre."); - } } +Game::Game(const Game& g) + :App(g.serialnum, g.name, g.os, g.manf, g.price), genre(g.genre), + online(g.online) {} + const std::string Game::get_genre() const { return genre; @@ -37,3 +35,38 @@ void Game::set_online(bool online) { this->online = online; } + +void Game::print(std::ostream& stream) const +{ + stream << + std::left << std::setw(7) << serialnum << + std::left << std::setw(20) << name << + std::left << std::setw(20) << os << + std::left << std::setw(7) << price; + + stream << + std::left << std::setw(7) << manf->get_serialnum() << + std::left << std::setw(15) << manf->get_name() << + std::left << std::setw(25) << manf->get_email(); + + stream << + std::left << std::setw(10) << genre << + std::left << std::setw(10) << (online ? "Yes" : "No"); + stream << "N/A"; + + if (!reviews.empty()) + { + stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; + stream << + std::left << std::setw(7) << "Stars" << + std::left << std::setw(25) << "Username" << + std::left << "Comment" << std::endl << std::endl; + + for (auto& rev : reviews) + stream << + std::left << std::setw(7) << rev->get_stars() << + std::left << std::setw(25) << rev->get_username() << + std::left << rev->get_comment() << std::endl; + } + stream << std::endl; +} diff --git a/assignment-2.4-inheritance/src/game.h b/assignment-2.4-inheritance/src/game.h @@ -14,12 +14,14 @@ class Game: public App Game(const char *serialnum, const std::string& name, const std::string& os, Manufacturer *manf, int price, const std::string& genre, bool online); + Game(const Game& g); const std::string get_genre() const; bool get_online() const; void set_genre(const std::string& genre); void set_online(bool online); + virtual void print(std::ostream& stream) const override; }; #endif /* GAME_H */ diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -1,5 +1,4 @@ #include "appsystem.h" -#include <iomanip> std::ostream& operator<< (std::ostream& stream, const AppSystem& sys); static void cont(); @@ -41,53 +40,9 @@ std::ostream& operator<< (std::ostream& stream, const AppSystem& sys) std::left << std::setw(10) << "Genre" << std::left << std::setw(10) << "Online" << std::left << std::setw(25) << "Extensions" << std::endl << std::endl; - std::vector<App *> apps = sys.get_apps(); for (auto& app : apps) - { - - stream << - std::left << std::setw(7) << app->get_serialnum() << - std::left << std::setw(20) << app->get_name() << - std::left << std::setw(20) << app->get_os() << - std::left << std::setw(7) << app->get_price(); - - Manufacturer m = app->get_manf(); - stream << - std::left << std::setw(7) << m.get_serialnum() << - std::left << std::setw(15) << m.get_name() << - std::left << std::setw(25) << m.get_email(); - - Game *o = dynamic_cast<Game *>(app); - stream << std::left << std::setw(10) << (o ? o->get_genre() : "N/A"); - stream << std::left << std::setw(10) << - (o ? (o->get_online() ? "Yes" : "No") : "N/A"); - - if (Office *of = dynamic_cast<Office *>(app)) - { - std::vector<std::string> exts = of->get_exts(); - for (auto& ext : exts) - stream << ext << " "; - } - else stream << "N/A"; - - std::vector<Review *> revs = app->get_revs(); - if (!revs.empty()) - { - stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; - stream << - std::left << std::setw(7) << "Stars" << - std::left << std::setw(25) << "Username" << - std::left << "Comment" << std::endl << std::endl; - - for (auto& rev : revs) - stream << - std::left << std::setw(7) << rev->get_stars() << - std::left << std::setw(25) << rev->get_username() << - std::left << rev->get_comment() << std::endl; - } - stream << std::endl; - } + app->print(stream); return stream; } diff --git a/assignment-2.4-inheritance/src/manufacturer.cpp b/assignment-2.4-inheritance/src/manufacturer.cpp @@ -7,21 +7,11 @@ Manufacturer::Manufacturer(const char *serialnum, const char *name, const std::string& email) :serialnum(convstr(serialnum)), name(convstr(name)), email(email) { - try - { - if (strcmp(serialnum, "") == 0) throw serialnum; - if (strcmp(name, "") == 0) throw name; - if (email.empty() || email.find("@") == std::string::npos) throw email; - } - catch (const char *serialnum) - { + if (strcmp(serialnum, "") == 0) throw std::runtime_error("Empty manufacturer serial number."); - } - catch (const char *name) - { + if (strcmp(name, "") == 0) throw std::runtime_error("Empty manufacturer name."); - } - catch (const std::string& email) + if (email.empty() || email.find("@") == std::string::npos) { std::string err = "Invalid email. Empty email/Missing \'@\'"; throw std::runtime_error(err); diff --git a/assignment-2.4-inheritance/src/manufacturer.h b/assignment-2.4-inheritance/src/manufacturer.h @@ -1,9 +1,9 @@ #ifndef MANUFACTURER_H #define MANUFACTURER_H +#include <cstring> #include <stdexcept> #include <string> -#include <string.h> class Manufacturer { diff --git a/assignment-2.4-inheritance/src/office.cpp b/assignment-2.4-inheritance/src/office.cpp @@ -8,6 +8,9 @@ Office::Office(const char *serialnum, const std::string& name, int price, const std::vector<std::string>& ext) :App(serialnum, name, os, manf, price), extensions(ext) {} +Office::Office(const Office& o) + :App(o.serialnum, o.name, o.os, o.manf, o.price) {} + Office::~Office() { if (!extensions.empty()) extensions.clear(); @@ -23,3 +26,41 @@ void Office::set_exts(const std::vector<std::string>& extensions) if (!this->extensions.empty()) this->extensions.clear(); this->extensions = extensions; } + +void Office::print(std::ostream& stream) const +{ + stream << + std::left << std::setw(7) << serialnum << + std::left << std::setw(20) << name << + std::left << std::setw(20) << os << + std::left << std::setw(7) << price; + + stream << + std::left << std::setw(7) << manf->get_serialnum() << + std::left << std::setw(15) << manf->get_name() << + std::left << std::setw(25) << manf->get_email(); + + stream << + std::left << std::setw(10) << "N/A" << + std::left << std::setw(10) << "N/A"; + + for (auto& ext : extensions) + stream << ext << " "; + + std::vector<Review *> revs = get_revs(); + if (!reviews.empty()) + { + stream << std::endl << std::endl << std::left << "Reviews:" << std::endl; + stream << + std::left << std::setw(7) << "Stars" << + std::left << std::setw(25) << "Username" << + std::left << "Comment" << std::endl << std::endl; + + for (auto& rev : reviews) + stream << + std::left << std::setw(7) << rev->get_stars() << + std::left << std::setw(25) << rev->get_username() << + std::left << rev->get_comment() << std::endl; + } + stream << std::endl; +} diff --git a/assignment-2.4-inheritance/src/office.h b/assignment-2.4-inheritance/src/office.h @@ -13,10 +13,12 @@ class Office: public App Office(const char *serialnum, const std::string& name, const std::string& os, Manufacturer *manf, int price, const std::vector<std::string>& ext); + Office(const Office& o); ~Office(); const std::vector<std::string> get_exts() const; void set_exts(const std::vector<std::string>& extensions); + virtual void print(std::ostream& stream) const override; }; #endif /* OFFICE_H */ diff --git a/assignment-2.4-inheritance/src/review.cpp b/assignment-2.4-inheritance/src/review.cpp @@ -6,10 +6,13 @@ Review::Review() Review::Review(int stars, const std::string& username, const std::string& comment) :stars(stars), username(username), comment(comment) { - try {if (stars < 0 || stars > 5) throw stars;} - catch (int stars) {throw std::runtime_error("Wrong star value.");} + if (stars < 0 || stars > 5) + throw std::runtime_error("Wrong star value."); } +Review::Review(const Review& r) + :stars(r.stars), username(r.username), comment(r.comment) {} + int Review::get_stars() const { return stars; diff --git a/assignment-2.4-inheritance/src/review.h b/assignment-2.4-inheritance/src/review.h @@ -14,6 +14,7 @@ class Review public: Review(); Review(int stars, const std::string& username, const std::string& comment); + Review(const Review& r); int get_stars() const; const std::string& get_username() const; 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/course.o b/assignment-2.5-spreadsheets/obj/course.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/grades.o b/assignment-2.5-spreadsheets/obj/grades.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/student.o b/assignment-2.5-spreadsheets/obj/student.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 @@ -14,13 +14,15 @@ bool App::import_matchings(const char *fpath) std::ifstream f; f.open(fpath); // exceptions + lab::xstring skip; + lab::getline(f, skip); while (f.good()) { lab::xstring newcurr, oldcurr; lab::getline(f, newcurr, ';'); lab::getline(f, oldcurr); if (f.eof()) break; - matchings.emplace_back(newcurr, oldcurr); + matchings.emplace(newcurr, oldcurr); } f.close(); return true; @@ -40,3 +42,9 @@ const std::vector<Student *>& App::get_studs() const { return studs; } + +const std::map<lab::xstring, lab::xstring>& +App::get_matchings() const +{ + return matchings; +} diff --git a/assignment-2.5-spreadsheets/src/app.h b/assignment-2.5-spreadsheets/src/app.h @@ -3,7 +3,7 @@ #include <iostream> #include <fstream> -#include <unordered_map> +#include <map> #include <vector> #include "course.h" @@ -17,7 +17,7 @@ class App std::vector<Course *> courses; std::vector<Grades *> grades; std::vector<Student *> studs; - std::vector<std::pair<lab::xstring, lab::xstring>> matchings; + std::map<lab::xstring, lab::xstring> matchings; public: App(); @@ -29,6 +29,7 @@ class App const std::vector<Course *>& get_courses() const; const std::vector<Grades *>& get_grades() const; const std::vector<Student *>& get_studs() const; + const std::map<lab::xstring, lab::xstring>& get_matchings() const; private: template<typename T> void dealloc(std::vector<T *>& vec); diff --git a/assignment-2.5-spreadsheets/src/main.cpp b/assignment-2.5-spreadsheets/src/main.cpp @@ -10,7 +10,12 @@ int main(int argc, char **argv) if (!app.import_data<Student>("res/students.csv")) return -1; if (!app.import_matchings("res/matchings.csv")) return -1; //std::cout << app << std::endl; - + + std::map<lab::xstring, lab::xstring> a = app.get_matchings(); + std::map<lab::xstring, lab::xstring>::const_iterator it; + for (it = a.begin(); it != a.end(); it++) + std::cout << it->first << " " << it->second << std::endl; + return 0; } diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -2,7 +2,10 @@ namespace lab { xstring::xstring() - :str(new char[1]), len(0) {} + :str(new char[1]), len(0) +{ + str[0] = '\0'; +} xstring::xstring(const char *s) :str(conv(s)), len(strlen(s)) {} @@ -141,8 +144,7 @@ bool xstring::operator>= (const char *s) const char& xstring::operator[] (std::size_t i) const { - try {if (i >= len) throw i;} - catch (int i) {throw std::runtime_error("Out of bounds.");} + if (i >= len) throw std::runtime_error("Out of bounds."); return str[i]; } @@ -257,6 +259,16 @@ void xstring::replace(std::size_t i, char c) if (i < len) str[i] = c; } +bool xstring::find(const xstring& s) const +{ + return (strstr(this->cstr(), s.cstr()) != nullptr); +} + +bool xstring::find(const char *s) const +{ + return (strstr(this->cstr(), s) != nullptr); +} + char *xstring::cstr() const { return str; diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -3,8 +3,8 @@ #include <algorithm> #include <cstdlib> +#include <cstring> #include <iostream> -#include <string.h> namespace lab { class xstring @@ -51,6 +51,8 @@ class xstring void push_back(char c); void pop_back(); void replace(std::size_t i, char c); + bool find(const xstring& s) const; + bool find(const char *s) const; char *cstr() const; char& front() const; char& back() const;