├── Practice ├── Practice-08-Exam │ ├── Exam-Task.pdf │ └── Exam Task - Solution │ │ ├── FitnessDataBaseMain.cpp │ │ ├── Client.h │ │ ├── Fitness.h │ │ ├── Client.cpp │ │ └── Fitness.cpp ├── Practice-01-Structures │ ├── Practice_01.pdf │ └── Practice_01.cpp ├── Practice-02-Files │ ├── Practice_02-Files.pdf │ ├── Student.h │ ├── Student.cpp │ └── Point.cpp ├── Practice-09-10-Inheritance │ ├── Practice-9-10-Inheritance.pdf │ ├── Person-Student-Teacher-Tasks │ │ ├── Person.h │ │ ├── Student.h │ │ ├── Teacher.h │ │ ├── main.cpp │ │ ├── Person.cpp │ │ ├── Student.cpp │ │ └── Teacher.cpp │ └── Appointment_Meeting_problem │ │ ├── Appointment.h │ │ ├── Meeting.h │ │ ├── main.cpp │ │ ├── Meeting.cpp │ │ └── Appointment.cpp ├── Practice-11-12-Polymorphism │ ├── Practice-11-12-Polymorphism.pdf │ └── Physist Lab - Polymorphism │ │ ├── LightSensor.h │ │ ├── HumiditySensor.h │ │ ├── TemperatureSensor.h │ │ ├── Laboratory.h │ │ ├── Sensor.h │ │ ├── LightSensor.cpp │ │ ├── HumiditySensor.cpp │ │ ├── TemperatureSensor.cpp │ │ ├── LabMainProgram.cpp │ │ ├── Sensor.cpp │ │ └── Laboratory.cpp ├── Practice-05-06-Classes-BIG4 │ ├── Practice-05-06-Classes-BIG4 .pdf │ ├── Point-Circle-Line-Rectangle-Tasks │ │ ├── Main.cpp │ │ ├── Point.h │ │ ├── Line.h │ │ ├── Circle.h │ │ ├── Point.cpp │ │ ├── Color.h │ │ ├── Rectangle.h │ │ ├── Circle.cpp │ │ ├── Line.cpp │ │ ├── Color.cpp │ │ └── Rectangle.cpp │ └── PhysicsLab │ │ ├── Laboratory.h │ │ ├── TemperatureSensor.h │ │ ├── TemperatureSensor.cpp │ │ └── Laboratory.cpp ├── Practice-13-14 OperatorOverloading │ ├── Practice-13-14-Operator-Overloading.pdf │ ├── MyString.h │ └── MyString.cpp ├── Practice-03-04-Exercise--Magic-The-Gathering │ ├── Magic-The-Gathering-problem.pdf │ ├── ID.h │ ├── helpFunctions.h │ ├── Player.h │ ├── Card.h │ ├── Deck.h │ ├── helpFunctions.cpp │ ├── ID.cpp │ ├── main.cpp │ ├── Player.cpp │ ├── Card.cpp │ └── Deck.cpp └── Practice-07-Serialization-Deserialization │ ├── Practice-07-Serialization-Deserialization-Exercise.pdf │ ├── Egg.h │ ├── Basket.h │ ├── Egg.cpp │ └── Basket.cpp ├── .gitignore ├── README.md └── Seminar ├── string-class ├── String.h ├── main.cpp └── String.cpp ├── binary-files.cpp └── simple-files.cpp /Practice/Practice-08-Exam/Exam-Task.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-08-Exam/Exam-Task.pdf -------------------------------------------------------------------------------- /Practice/Practice-01-Structures/Practice_01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-01-Structures/Practice_01.pdf -------------------------------------------------------------------------------- /Practice/Practice-02-Files/Practice_02-Files.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-02-Files/Practice_02-Files.pdf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore compiled files 2 | *.out 3 | *.o 4 | *.obj 5 | *.gch 6 | *.exe 7 | *.dll 8 | 9 | # Ignore swapfiles 10 | *.swp 11 | *~ 12 | 13 | # Ignore VS project files 14 | *.vcproj 15 | *.sln 16 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Practice-9-10-Inheritance.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-09-10-Inheritance/Practice-9-10-Inheritance.pdf -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Practice-11-12-Polymorphism.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-11-12-Polymorphism/Practice-11-12-Polymorphism.pdf -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Practice-05-06-Classes-BIG4 .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-05-06-Classes-BIG4/Practice-05-06-Classes-BIG4 .pdf -------------------------------------------------------------------------------- /Practice/Practice-13-14 OperatorOverloading/Practice-13-14-Operator-Overloading.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-13-14 OperatorOverloading/Practice-13-14-Operator-Overloading.pdf -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/Magic-The-Gathering-problem.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-03-04-Exercise--Magic-The-Gathering/Magic-The-Gathering-problem.pdf -------------------------------------------------------------------------------- /Practice/Practice-07-Serialization-Deserialization/Practice-07-Serialization-Deserialization-Exercise.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VasiPeycheva/Object-Oriented-Programming--2017-2018/HEAD/Practice/Practice-07-Serialization-Deserialization/Practice-07-Serialization-Deserialization-Exercise.pdf -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/ID.h: -------------------------------------------------------------------------------- 1 | #ifndef __ID__HEADER__INCLUDED__ 2 | #define __ID__HEADER__INCLUDED__ 3 | 4 | #include 5 | #include 6 | #include "helpFunctions.h" 7 | 8 | void initialization(); 9 | bool file_exists(); 10 | void save_changes(); 11 | unsigned short getID(); 12 | 13 | #endif // !__ID__HEADER__INCLUDED__ 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Sofia University "St. Kliment Ohridski", Faculty of Mathematics and Informatics : Lections and code from Object Oriented Programming course 2017-2018: VPN edition (the course is held by Vasilena,Plamen, Nikolay) 2 | 3 | Софийски университет "Св. Климент Охридски", Факултет по Математика и Информатика: Код от упражненията по ООП-практикум 2017/2018 спец. Информатика 4 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Line.h" 3 | #include "Rectangle.h" 4 | #include "Color.h" 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | 10 | Point p1(1, 2); 11 | Point p2(3, 4); 12 | 13 | Line l1(p1, p2, RED); 14 | l1.print(); 15 | 16 | Rectangle rec(p1, 3, 4, BLUE); 17 | 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/LightSensor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Sensor.h" 3 | class LightSensor:public Sensor 4 | { 5 | public: 6 | LightSensor(const char* descr); 7 | LightSensor(const LightSensor&); 8 | LightSensor& operator=(const LightSensor&); 9 | virtual ~LightSensor(); 10 | 11 | virtual unsigned short getValue() const override; 12 | virtual Sensor* clone() const override; 13 | }; -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Point.h: -------------------------------------------------------------------------------- 1 | #ifndef __POINT__HEADER__INCLUDED__ 2 | #define __POINT__HEADER__INCLUDED__ 3 | 4 | class Point 5 | { 6 | public: 7 | Point(int x, int y); //Constructor 8 | ~Point(); //Destructor 9 | 10 | const int getX() const; 11 | const int getY() const; 12 | 13 | void translate(int horizontal, int vertical); 14 | 15 | private: 16 | int x; 17 | int y; 18 | }; 19 | 20 | 21 | #endif -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/HumiditySensor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Sensor.h" 3 | 4 | class HumiditySensor:public Sensor 5 | { 6 | public: 7 | HumiditySensor(const char* descr); 8 | HumiditySensor(const HumiditySensor&); 9 | HumiditySensor& operator=(const HumiditySensor&); 10 | virtual ~HumiditySensor(); 11 | 12 | virtual unsigned short getValue() const override; 13 | virtual Sensor* clone() const override; 14 | 15 | }; -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/TemperatureSensor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Sensor.h" 3 | 4 | class TemperatureSensor:public Sensor 5 | { 6 | public: 7 | TemperatureSensor(const char* descr); 8 | TemperatureSensor(const TemperatureSensor&); 9 | TemperatureSensor& operator=(const TemperatureSensor&); 10 | virtual ~TemperatureSensor(); 11 | 12 | virtual unsigned short getValue() const override; 13 | virtual Sensor* clone() const override; 14 | }; 15 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Line.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINE__HEADER__INCLUDED__ 2 | #define __LINE__HEADER__INCLUDED__ 3 | 4 | #include "Color.h" 5 | #include "Point.h" 6 | 7 | class Line 8 | { 9 | public: 10 | Line(Point, Point, Color); 11 | ~Line(); 12 | 13 | void translate(int horizontal, int vertical); 14 | const void print() const; 15 | 16 | private: 17 | Point A; 18 | Point B; 19 | Color lineColor; 20 | }; 21 | 22 | #endif // !__LINE__HEADER__INCLUDED__ -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/helpFunctions.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELP__FUNCTITONS__HEADER__INCLUDED__ 2 | #define __HELP__FUNCTITONS__HEADER__INCLUDED__ 3 | 4 | //help function for reading player/card name 5 | void readName(char[], unsigned short); 6 | 7 | //help functions for finding the most common card 8 | void countingSort(unsigned int[], const unsigned short []); 9 | unsigned short getMostCommonID(unsigned int[], unsigned short); 10 | 11 | #endif // !__HELP__FUNCTITONS__HEADER__INCLUDED__ 12 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Person-Student-Teacher-Tasks/Person.h: -------------------------------------------------------------------------------- 1 | #ifndef __PERSON__HEADER__INCLUDED__ 2 | #define __PERSON__HEADER__INCLUDED__ 3 | 4 | class Person { 5 | public: 6 | Person(const char * personName,const int birth); 7 | Person(const Person & other); 8 | ~Person(); 9 | 10 | void printInformation() const; 11 | 12 | private: 13 | void clean(); 14 | void copy(const Person & other); 15 | 16 | private: 17 | char * name; 18 | const int birthYear; 19 | }; 20 | 21 | #endif // !__PERSON__HEADER__INCLUDED__ 22 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Circle.h: -------------------------------------------------------------------------------- 1 | #ifndef __CIRCLE__HEADER__INCLUDED__ 2 | #define __CIRCLE__HEADER__INCLUDED__ 3 | 4 | #include "Point.h" 5 | #include "Color.h" 6 | 7 | class Circle 8 | { 9 | public: 10 | Circle(Point center, int radius, Color circleColor); 11 | ~Circle(); 12 | 13 | void translate(int horizontal, int vertical); 14 | const void print() const; 15 | 16 | private: 17 | Point center; 18 | int radius; 19 | Color circleColor; 20 | }; 21 | 22 | #endif // ! __CIRCLE__HEADER__INCLUDED__ 23 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Point.h" 3 | 4 | using namespace std; 5 | 6 | 7 | Point::Point(int x, int y) 8 | :x(x) 9 | ,y(y) 10 | {} 11 | 12 | 13 | Point::~Point() 14 | {} 15 | 16 | const int Point::getX() const 17 | { 18 | return x; 19 | } 20 | 21 | const int Point::getY() const 22 | { 23 | return y; 24 | } 25 | 26 | //h is for horizontal 27 | //v is for vertical 28 | void Point::translate(int h, int v) 29 | { 30 | x = x + h; 31 | y = y + v; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Color.h: -------------------------------------------------------------------------------- 1 | #ifndef __COLOR__HEADER__INCLUDED__ 2 | #define __COLOR__HEADER__INCLUDED__ 3 | 4 | const int maxColorSize = 16; 5 | 6 | enum Color 7 | { 8 | INVALID_COLOR = -1, 9 | 10 | BLACK, 11 | BLUE, 12 | RED, 13 | GREEN, 14 | YELLOW, 15 | ORANGE, 16 | BROWN, 17 | PURPLE, 18 | PINK, 19 | 20 | COLOR_COUNT 21 | }; 22 | 23 | 24 | Color strToColor(char choice[maxColorSize]); 25 | char * colorTostr(Color c); 26 | void printColor(Color c); 27 | 28 | #endif // !__COLOR__HEADER__INCLUDED__ 29 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __PLAYER__HEADER__INCLUDED__ 2 | #define __PLAYER__HEADER__INCLUDED__ 3 | #include "ID.h" 4 | 5 | const unsigned short maxPlayerName = 128; 6 | 7 | struct Player 8 | { 9 | char name[maxPlayerName]; 10 | unsigned short ID; 11 | }; 12 | 13 | 14 | void createPlayer(); 15 | void savePlayer(const Player &player); 16 | static bool alreadyExists(char[]); 17 | 18 | bool playerGetID(Player &player); 19 | bool playerGetName(Player &player); 20 | 21 | 22 | 23 | 24 | #endif // !__PLAYER__HEADER__INCLUDED__ 25 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Appointment_Meeting_problem/Appointment.h: -------------------------------------------------------------------------------- 1 | #ifndef __APPOINTMENT__HEADER__INCLUDED__ 2 | #define __APPOINTMENT__HEADER__INCLUDED__ 3 | 4 | class Appointment 5 | { 6 | public: 7 | Appointment(const char *, const char[], const char[]); 8 | ~Appointment(); 9 | Appointment(const Appointment &); 10 | Appointment& operator=(const Appointment &); 11 | 12 | private: 13 | void copy(const Appointment &); 14 | private: 15 | char * description; 16 | char start[8]; 17 | char end[8]; 18 | }; 19 | 20 | #endif // !__APPOINTMENT__HEADER__INCLUDED__ 21 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Rectangle.h: -------------------------------------------------------------------------------- 1 | #ifndef __RECTANGLE__HEADER__INCLUDED__ 2 | #define __RECTANGLE__HEADER__INCLUDED__ 3 | 4 | #include "Color.h" 5 | #include "Point.h" 6 | 7 | class Rectangle 8 | { 9 | public: 10 | Rectangle(Point OxOy, int recWidth, int recHeight, Color recColor); 11 | ~Rectangle(); 12 | 13 | void translate(int horizontal, int vertical); 14 | const void print() const; 15 | 16 | private: 17 | Point Oxy; 18 | int width; 19 | int height; 20 | Color recColor; 21 | }; 22 | 23 | #endif // !__RECTANGLE__HEADER__INCLUDED__ -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Appointment_Meeting_problem/Meeting.h: -------------------------------------------------------------------------------- 1 | #ifndef __MEETING__HEADER__INCLUDED__ 2 | #define __MEETING__HEADER__INCLUDED__ 3 | #include "Appointment.h" 4 | 5 | class Meeting : public Appointment 6 | { 7 | public: 8 | Meeting(const char *, const char *, const char start[], const char end[]); 9 | ~Meeting(); 10 | Meeting(const Meeting &); 11 | Meeting& operator=(const Meeting &); 12 | 13 | const char * getContact() const {return contact;} 14 | private: 15 | void copy(const Meeting &); 16 | 17 | private: 18 | char * contact; 19 | }; 20 | 21 | #endif // !__MEETING__HEADER__INCLUDED__ 22 | 23 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Circle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Circle.h" 3 | 4 | using namespace std; 5 | 6 | 7 | Circle::Circle(Point Oxy, int rad, Color cirColor) 8 | :center(Oxy) 9 | ,radius(rad) 10 | ,circleColor(circleColor) 11 | {} 12 | 13 | Circle::~Circle() 14 | {} 15 | 16 | void Circle::translate(int horizontal, int vertical) 17 | { 18 | center.translate(horizontal, vertical); 19 | } 20 | 21 | const void Circle::print() const 22 | { 23 | cout << "circle " << 24 | center.getX() << " " 25 | << center.getY() << " " 26 | << radius << " "; 27 | printColor(circleColor); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Person-Student-Teacher-Tasks/Student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT__HEADER__INCLUDED__ 2 | #define __STUDENT__HEADER__INCLUDED__ 3 | 4 | #include "Person.h" 5 | 6 | class Student :public Person 7 | { 8 | public: 9 | Student(const char * name,const int birthYear, 10 | const char * universityName, const char * courseName, int fn); 11 | Student(const Student & other); 12 | ~Student(); 13 | 14 | void printInformation() const; 15 | 16 | private: 17 | void clean(); 18 | void copy(const Student & other); 19 | 20 | private: 21 | char * university; 22 | char * course; 23 | int facultyNumber; 24 | }; 25 | 26 | #endif // !__STUDENT__HEADER__INCLUDED__ 27 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Line.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Line.h" 4 | 5 | using namespace std; 6 | 7 | Line::Line(Point P1, Point P2, Color c) 8 | :A(P1) 9 | ,B(P2) 10 | ,lineColor(c) 11 | {} 12 | 13 | Line::~Line() 14 | {} 15 | 16 | 17 | void Line::translate(int horizontal, int vertical) 18 | { 19 | A.translate(horizontal, vertical); 20 | B.translate(horizontal, vertical); 21 | } 22 | 23 | const void Line::print() const 24 | { 25 | cout << "line " 26 | << A.getX() << " " 27 | << A.getY() << " " 28 | << B.getX() << " " 29 | << B.getY() << " "; 30 | printColor(lineColor); 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Appointment_Meeting_problem/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Appointment.h" 2 | #include "Meeting.h" 3 | #include 4 | 5 | bool hasAppointmentWith(Meeting* schedule, int size, const char * name) 6 | { 7 | for (size_t i = 0; i < size; i++) 8 | { 9 | if (strcmp(schedule[i].getContact(), name) == 0) 10 | return true; 11 | } 12 | return false; 13 | } 14 | 15 | 16 | int main() 17 | { 18 | Meeting h("Iva", "staff reduce", "1244", "1350"); 19 | Meeting p("Plamen", "salary change", "1600", "1700"); 20 | 21 | Meeting ** sched = new Meeting*[2]; 22 | sched[0] = new Meeting(h); 23 | sched[1] = new Meeting(p); 24 | 25 | std::cout << hasAppointmentWith(*sched, 2, "Angel"); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/Laboratory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Sensor.h" 3 | 4 | class Laboratory 5 | { 6 | public: 7 | Laboratory(const char * name); 8 | Laboratory(const Laboratory&); 9 | Laboratory& operator=(const Laboratory&); 10 | ~Laboratory(); 11 | 12 | const char* getName() const; 13 | void setName(const char*); 14 | 15 | const Sensor& getSensor(unsigned int) const; 16 | void addSensor(const Sensor&); 17 | 18 | bool isLaboratoryReadyForExperiments() const; 19 | 20 | private: 21 | void copyFrom(const Laboratory&); 22 | void freeData(); 23 | void resize(); 24 | 25 | private: 26 | char* name; 27 | Sensor** sensors; 28 | unsigned int cap; 29 | unsigned int size; 30 | }; -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Color.cpp: -------------------------------------------------------------------------------- 1 | #include "Color.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | char* strColor[] = { "black", "blue", "red", "green", "yellow", "orange", "brown", "purple", "pink" }; 7 | 8 | Color strToColor(char choice[maxColorSize]) 9 | { 10 | for (int i = 0; i < COLOR_COUNT; i++) 11 | { 12 | if (strcmp(choice, strColor[i]) == 0) 13 | return (Color)i; 14 | } 15 | return INVALID_COLOR; 16 | } 17 | 18 | 19 | char* colorTostr(Color c) { 20 | for (int i = 0; i < COLOR_COUNT ; i++) { 21 | if (i == c) 22 | return strColor[i]; 23 | } 24 | return "invalid"; 25 | } 26 | 27 | void printColor(Color c) { 28 | cout << colorTostr(c) << endl; 29 | } -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/Point-Circle-Line-Rectangle-Tasks/Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Rectangle.h" 3 | using namespace std; 4 | 5 | Rectangle::Rectangle(Point P1, int recWidth, int recHeight, Color recColor) 6 | : Oxy(P1) 7 | , width(recWidth) 8 | , height(recHeight) 9 | , recColor(recColor) 10 | {} 11 | 12 | Rectangle::~Rectangle() 13 | {} 14 | 15 | void Rectangle::translate(int horizontal, int vertical) 16 | { 17 | Oxy.translate(horizontal, vertical); 18 | } 19 | 20 | const void Rectangle::print() const 21 | { 22 | cout << "rectangle " 23 | << Oxy.getX() << " " 24 | << Oxy.getY() << " " 25 | << width << " " 26 | << height << " "; 27 | printColor(recColor); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Practice/Practice-07-Serialization-Deserialization/Egg.h: -------------------------------------------------------------------------------- 1 | #ifndef __EGG__HEADER__INCLUDED__ 2 | #define __EGG__HEADER__INCLUDED__ 3 | 4 | #include 5 | #include 6 | 7 | class Egg 8 | { 9 | public: 10 | Egg(); 11 | Egg(const char *); 12 | ~Egg(); 13 | Egg(const Egg &); 14 | Egg& operator=(const Egg &); 15 | 16 | unsigned int getEggSize() const; 17 | const char * getEggName() const; 18 | 19 | void eggPrint(std::ofstream &); 20 | void eggSerialization(std::ofstream &); 21 | void eggDeserialization(std::ifstream &); 22 | 23 | private: 24 | void copy(const Egg &); 25 | void clean(); 26 | 27 | private: 28 | char * eggName; 29 | unsigned int eggSize; 30 | }; 31 | 32 | #endif // !__EGG__HEADER__INCLUDED__ 33 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Person-Student-Teacher-Tasks/Teacher.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEACHER__HEADER__INCLUDED__ 2 | #define __TEACHER__HEADER__INCLUDED__ 3 | 4 | #include "Person.h" 5 | 6 | class Teacher :public Person 7 | { 8 | public: 9 | Teacher(const char * name, int birthYear, 10 | const char * universityName, const char * subjectName); 11 | Teacher(const Teacher & other); 12 | ~Teacher(); 13 | 14 | void setUniversity(const char * universityName); 15 | void setSubject(const char * subjectName); 16 | 17 | void printInformation() const; 18 | 19 | private: 20 | void clean(); 21 | void copy(const Teacher & other); 22 | 23 | private: 24 | char * university; 25 | char * subject; 26 | }; 27 | 28 | #endif // !__TEACHER__HEADER__INCLUDED__ -------------------------------------------------------------------------------- /Practice/Practice-08-Exam/Exam Task - Solution/FitnessDataBaseMain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include"Client.h" 4 | #include"Fitness.h" 5 | 6 | int main() { 7 | /*Client client1("Paco", 22, 85); 8 | Client client2("Vasi", 23, 103); 9 | Client client3("Mitko", 22, 34); 10 | Client client4("Vanaka", 23, 82); 11 | Client client5("Gosho", 101, 232); 12 | client1.print(); 13 | 14 | Fitness f1("Fitness One"); 15 | 16 | f1.addClient(client1); 17 | f1.addClient(client2); 18 | f1.addClient(client3); 19 | f1.addClient(client4); 20 | f1.addClient(client5); 21 | f1.print(); 22 | f1.serializeFitness();*/ 23 | 24 | 25 | Fitness f1("Fitness One"); 26 | f1.deserializeFitness("Fitness One.dat"); 27 | f1.sort(); 28 | f1.print(); 29 | f1.reportFitness(); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/Sensor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Sensor 4 | { 5 | public: 6 | Sensor(const char*); 7 | Sensor(const Sensor&); 8 | Sensor& operator=(const Sensor&); 9 | virtual ~Sensor(); 10 | 11 | const char* getDescription() const; 12 | void setDescription(const char*); 13 | 14 | unsigned short getID() const; 15 | 16 | virtual unsigned short getValue() const = 0; 17 | 18 | //Declare pure virtual function to make Sensor abstract class; 19 | //The function is constant because we want to use it with constant objects; 20 | //When we declare function as pure virtual we must implement it in every child class; 21 | //returns pointer to a Sensor; 22 | virtual Sensor* clone() const = 0; 23 | 24 | protected: 25 | unsigned int id; 26 | char* description; 27 | }; -------------------------------------------------------------------------------- /Practice/Practice-07-Serialization-Deserialization/Basket.h: -------------------------------------------------------------------------------- 1 | #ifndef __BASKET__HEADER__INCLUDED__ 2 | #define __BASKET__HEADER__INCLUDED__ 3 | 4 | #include "Egg.h" 5 | 6 | class Basket 7 | { 8 | public: 9 | Basket(const char *); 10 | ~Basket(); 11 | Basket(const Basket &); 12 | Basket& operator=(const Basket &); 13 | 14 | void addEgg(const Egg &); 15 | void removeEgg(const char *); 16 | void serialization(); 17 | void deserialization(); 18 | void reportBasket(); 19 | 20 | private: 21 | void setBasketName(const char *); 22 | void initializeArr(Egg *&,int&); 23 | void copy(const Basket &); 24 | void resize(); 25 | void clean(); 26 | 27 | int eggExists(const char *); 28 | 29 | private: 30 | char * basketName; 31 | Egg * arr; 32 | int size; 33 | int capacity; 34 | }; 35 | 36 | 37 | #endif // !__BASKET__HEADER__INCLUDED__ 38 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/Card.h: -------------------------------------------------------------------------------- 1 | #ifndef __CARD__HEADER__INCLUDED__ 2 | #define __CARD__HEADER__INCLUDED__ 3 | #include "ID.h" 4 | #include "helpFunctions.h" 5 | 6 | const unsigned short maxCardName = 64; 7 | static const char * colorsName[] = { "Red", "Black", "Blue", "White", "Green" }; 8 | 9 | enum Color 10 | { 11 | INVALID_COLOR = -1, 12 | 13 | RED, 14 | BLACK, 15 | BLUE, 16 | WHITE, 17 | GREEN, 18 | 19 | COLOR_COUNT 20 | }; 21 | 22 | struct Card 23 | { 24 | char name[maxCardName]; 25 | unsigned short ID; 26 | Color color; 27 | }; 28 | 29 | 30 | void createCard(); 31 | void saveCard(const Card &card); 32 | static bool alreadyExists(char[]); 33 | 34 | bool cardGetID(Card &card); 35 | bool cardGetName(Card &card); 36 | 37 | Color readColor(char[]); 38 | 39 | #endif // !__CARD__HEADER__INCLUDED__ 40 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/Deck.h: -------------------------------------------------------------------------------- 1 | #ifndef __DECK__HEADER__INCLUDED__ 2 | #define __DECK__HEADER__INCLUDED__ 3 | #include "Player.h" 4 | #include "Card.h" 5 | 6 | const unsigned short cardsInDeck = 60; 7 | 8 | struct Deck 9 | { 10 | unsigned short playerID; 11 | unsigned short cardID[cardsInDeck]; 12 | Color color; 13 | }; 14 | 15 | 16 | bool createDeck(); 17 | Color defineColor(Card[]); 18 | bool saveDeck(const Deck &deck); 19 | 20 | 21 | //functions for the report command 22 | void report(); 23 | size_t decksCnt(); 24 | void writeDeckInfo(const Deck& , std::ofstream &); 25 | 26 | //functions for finding the most common card 27 | unsigned short mostCommonCard(const Deck [], size_t); 28 | void writeMostCommonCard(const Deck[], size_t, std::ofstream &out); 29 | 30 | 31 | #endif // !__DECK__HEADER__INCLUDED__ 32 | -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/LightSensor.cpp: -------------------------------------------------------------------------------- 1 | #include "LightSensor.h" 2 | #include 3 | 4 | static const unsigned short LIGHT_RANGE = 80; 5 | LightSensor::LightSensor(const char * descr) :Sensor(descr) 6 | { 7 | } 8 | 9 | LightSensor::LightSensor(const LightSensor& other) : Sensor(other) 10 | { 11 | } 12 | 13 | LightSensor & LightSensor::operator=(const LightSensor& other) 14 | { 15 | if (this != &other) 16 | { 17 | Sensor::operator=(other); 18 | } 19 | 20 | return *this; 21 | } 22 | 23 | LightSensor::~LightSensor() 24 | { 25 | 26 | } 27 | 28 | unsigned short LightSensor::getValue() const 29 | { 30 | double light = rand() % (LIGHT_RANGE+1); 31 | return ((light * 100) / LIGHT_RANGE); 32 | } 33 | 34 | Sensor * LightSensor::clone() const 35 | { 36 | return new LightSensor(*this); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/PhysicsLab/Laboratory.h: -------------------------------------------------------------------------------- 1 | #ifndef __LABORATORY__HEADER__INCLUDED__ 2 | #define __LABORATORY__HEADER__INCLUDED__ 3 | 4 | #include "TemperatureSensor.h" 5 | 6 | class Laboratory 7 | { 8 | public: 9 | Laboratory(); 10 | Laboratory(const Laboratory&); 11 | Laboratory& operator=(const Laboratory&); 12 | ~Laboratory(); 13 | 14 | const char* getName() const; 15 | void setName(const char*); 16 | 17 | const TemperatureSensor getSensor(unsigned int) const; 18 | void addSensor(const TemperatureSensor&); 19 | 20 | float getCurrentTemperature() const; 21 | 22 | private: 23 | void copyFrom(const Laboratory&); 24 | void freeData(); 25 | void resize(); 26 | 27 | private: 28 | char* name; 29 | TemperatureSensor* sensors; 30 | unsigned int capacity; 31 | unsigned int size; 32 | }; 33 | 34 | #endif // !__LABORATORY__HEADER__INCLUDED__ 35 | 36 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/PhysicsLab/TemperatureSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEMPERATURE__SENSOR__HEADER__INCLUDED__ 2 | #define __TEMPERATURE__SENSOR__HEADER__INCLUDED__ 3 | 4 | class TemperatureSensor 5 | { 6 | public: 7 | TemperatureSensor(); 8 | TemperatureSensor(const char*); 9 | TemperatureSensor(const TemperatureSensor&); 10 | TemperatureSensor& operator=(const TemperatureSensor&); 11 | ~TemperatureSensor(); 12 | 13 | const char* getDescription() const; 14 | void setDescription(const char*); 15 | 16 | unsigned short getID() const; 17 | void setNewID(); 18 | 19 | float getTemperature() const; 20 | 21 | private: 22 | void copyFrom(const TemperatureSensor&); 23 | void freeData(); 24 | float randomFloat() const; 25 | 26 | private: 27 | char* description; 28 | unsigned int id; 29 | }; 30 | 31 | #endif // !__TEMPERATURE__SENSOR__HEADER__INCLUDED__ 32 | 33 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Appointment_Meeting_problem/Meeting.cpp: -------------------------------------------------------------------------------- 1 | #include "Meeting.h" 2 | #include 3 | 4 | Meeting::Meeting(const char * name, const char * desc, 5 | const char start[], const char end[]) 6 | : Appointment(desc, start, end) 7 | , contact(NULL) 8 | { 9 | contact = new char[strlen(name) + 1]; 10 | strcpy(contact, name); 11 | } 12 | 13 | Meeting::~Meeting() 14 | { 15 | delete[] contact; 16 | } 17 | 18 | Meeting::Meeting(const Meeting & input) 19 | :Appointment(input) 20 | { 21 | copy(input); 22 | } 23 | 24 | Meeting& Meeting::operator=(const Meeting & input) 25 | { 26 | if (this != &input) 27 | { 28 | Appointment::operator=(input); 29 | delete[] contact; 30 | copy(input); 31 | } 32 | return *this; 33 | } 34 | 35 | void Meeting::copy(const Meeting & input) 36 | { 37 | contact = new char[strlen(input.contact) + 1]; 38 | strcpy(contact, input.contact); 39 | } -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/HumiditySensor.cpp: -------------------------------------------------------------------------------- 1 | #include "HumiditySensor.h" 2 | #include 3 | 4 | static const unsigned short HUMIDITY_RANGE = 50; 5 | HumiditySensor::HumiditySensor(const char * descr) :Sensor(descr) 6 | { 7 | } 8 | 9 | HumiditySensor::HumiditySensor(const HumiditySensor& other) : Sensor(other) 10 | { 11 | } 12 | 13 | HumiditySensor & HumiditySensor::operator=(const HumiditySensor& other) 14 | { 15 | if (this != &other) 16 | { 17 | Sensor::operator=(other); 18 | } 19 | 20 | return *this; 21 | } 22 | 23 | HumiditySensor::~HumiditySensor() 24 | { 25 | 26 | } 27 | 28 | unsigned short HumiditySensor::getValue() const 29 | { 30 | double humidity = (rand() %(HUMIDITY_RANGE+1)) +10; 31 | return ((humidity * 100) / HUMIDITY_RANGE); 32 | } 33 | 34 | Sensor * HumiditySensor::clone() const 35 | { 36 | return new HumiditySensor(*this); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/helpFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "helpFunctions.h" 3 | #include "Deck.h" 4 | 5 | using std::cin; 6 | 7 | void readName(char name[], unsigned short size) 8 | { 9 | while (cin && isspace(cin.peek())) 10 | cin.ignore(); 11 | cin.getline(name, size); 12 | if (cin.fail()) 13 | { 14 | cin.clear(); 15 | while (cin && '\n' != cin.get()); 16 | } 17 | cin.clear(); 18 | } 19 | 20 | void countingSort(unsigned int arr[],const unsigned short deck[]) 21 | { 22 | for (size_t i = 0; i < cardsInDeck; i++) 23 | { 24 | arr[deck[i]]++; 25 | } 26 | } 27 | 28 | unsigned short getMostCommonID(unsigned int arr[],unsigned short size) 29 | { 30 | unsigned short ID = 0; 31 | unsigned short IDcnt = 0; 32 | for (size_t i = 0; i < size; i++) 33 | { 34 | if (arr[i] > IDcnt) 35 | { 36 | IDcnt = arr[i]; 37 | ID = i; 38 | } 39 | } 40 | return ID; 41 | } -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Person-Student-Teacher-Tasks/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student.h" 3 | 4 | /* 5 | * overloading example 6 | */ 7 | void fn(int n) 8 | { 9 | std::cout << n << std::endl; 10 | } 11 | 12 | void fn() 13 | { 14 | std::cout << "Hello overload!" << std::endl; 15 | } 16 | 17 | //************************************************************* 18 | 19 | int main() 20 | { 21 | //overloading test; 22 | fn(10); 23 | fn(); 24 | 25 | Person p("Ivan Borisov", 1995); 26 | Person** q; 27 | q = new Person*[1024]; 28 | for (size_t i = 0; i < 2; i++) 29 | { 30 | q[i] = new Person("Asen Ivanov", 1956); 31 | } 32 | for (size_t i = 0; i < 2; i++) 33 | { 34 | q[i]->printInformation(); 35 | } 36 | 37 | std::cout << "**********************************************" << std::endl; 38 | 39 | Student s("Vasi Peycheva", 1995, "Sofia University", "Informatica", 12345); 40 | s.printInformation(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Appointment_Meeting_problem/Appointment.cpp: -------------------------------------------------------------------------------- 1 | #include "Appointment.h" 2 | #include 3 | 4 | 5 | Appointment::Appointment(const char * desc, const char st[], const char en[]) 6 | { 7 | description = new char[strlen(desc) + 1]; 8 | strcpy(description, desc); 9 | strcpy(start, st); 10 | strcpy(end, en); 11 | } 12 | 13 | Appointment::~Appointment() 14 | { 15 | delete[] description; 16 | } 17 | 18 | Appointment::Appointment(const Appointment & input) 19 | { 20 | copy(input); 21 | } 22 | 23 | Appointment& Appointment::operator=(const Appointment & input) 24 | { 25 | if (this != &input) 26 | { 27 | delete[] description; 28 | copy(input); 29 | } 30 | return *this; 31 | } 32 | 33 | void Appointment::copy(const Appointment & input) 34 | { 35 | description = new char[strlen(input.description) + 1]; 36 | strcpy(description, input.description); 37 | strcpy(start, input.start); 38 | strcpy(end, input.end); 39 | } 40 | -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/TemperatureSensor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TemperatureSensor.h" 3 | static const unsigned short TEMPERATURE_RANGE = 35; 4 | 5 | TemperatureSensor::TemperatureSensor(const char * descr) :Sensor(descr) 6 | { 7 | } 8 | 9 | TemperatureSensor::TemperatureSensor(const TemperatureSensor& other):Sensor(other) 10 | { 11 | } 12 | 13 | TemperatureSensor & TemperatureSensor::operator=(const TemperatureSensor& other) 14 | { 15 | if (this != &other) 16 | { 17 | Sensor::operator=(other); 18 | } 19 | 20 | return *this; 21 | } 22 | 23 | TemperatureSensor::~TemperatureSensor() 24 | { 25 | 26 | } 27 | 28 | unsigned short TemperatureSensor::getValue() const 29 | { 30 | double temperature = (rand() % (TEMPERATURE_RANGE+1)); 31 | return ((temperature * 100) / TEMPERATURE_RANGE); 32 | } 33 | 34 | Sensor * TemperatureSensor::clone() const 35 | { 36 | return new TemperatureSensor(*this); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Practice/Practice-13-14 OperatorOverloading/MyString.h: -------------------------------------------------------------------------------- 1 | #ifndef __MYSTRING__HEADER__INCLUDED__ 2 | #define __MYSTRING__HEADER__INCLUDED__ 3 | #include 4 | 5 | class MyString 6 | { 7 | public: 8 | MyString(); 9 | MyString(const char *); 10 | ~MyString(); 11 | MyString(const MyString &); 12 | MyString& operator=(const MyString &); 13 | 14 | unsigned int getStringWeigth() const; 15 | 16 | MyString& operator+=(const MyString &); 17 | MyString& operator*=(int k); 18 | 19 | friend std::istream& operator >> (std::istream&, MyString &); 20 | friend std::ostream& operator << (std::ostream&, const MyString &); 21 | 22 | private: 23 | void clean(); 24 | void copy(const MyString &); 25 | private: 26 | char * str; 27 | }; 28 | 29 | MyString operator+(const MyString &, const MyString &); 30 | bool operator==(const MyString &, const MyString &); 31 | bool operator!=(const MyString &, const MyString &); 32 | 33 | 34 | #endif // !__MYSTRING__HEADER__INCLUDED__ 35 | -------------------------------------------------------------------------------- /Practice/Practice-08-Exam/Exam Task - Solution/Client.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLIENT__HEADER__INCLUDED__ 2 | #define __CLIENT__HEADER__INCLUDED__ 3 | 4 | #include 5 | 6 | static const unsigned MAX_CLIENTNAME_LENGTH = 64; 7 | 8 | class Client { 9 | 10 | public: 11 | Client(); 12 | Client(const char* name); 13 | Client(const char* name, unsigned age, unsigned weight); 14 | Client(const Client& client); 15 | Client& operator=(const Client& client); 16 | ~Client(); 17 | 18 | void serializeClient(std::ofstream& out) const; 19 | void deserializeClient(std::ifstream& in); 20 | void reportClient(std::ofstream& out) const; 21 | 22 | void changeWeight(unsigned newWeight); 23 | 24 | const char* getName() const; 25 | unsigned short getAge() const; 26 | void print() const; 27 | 28 | private: 29 | 30 | void copy(const Client& client); 31 | void clear(); 32 | 33 | private: 34 | unsigned short age; 35 | unsigned short weight; 36 | char* name; 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Practice/Practice-08-Exam/Exam Task - Solution/Fitness.h: -------------------------------------------------------------------------------- 1 | #ifndef __FITNESS__HEDAER__INCLUDED__ 2 | #define __FITNESS__HEDAER__INCLUDED__ 3 | 4 | #include"Client.h" 5 | 6 | class Fitness { 7 | 8 | public: 9 | Fitness(); 10 | Fitness(const char* name); 11 | Fitness(const Fitness& fitness); 12 | Fitness& operator=(const Fitness& fitness); 13 | ~Fitness(); 14 | 15 | bool addClient(const Client& client); 16 | void removeClient(const char* client); 17 | void changeWeight(const char* name, unsigned int kg); 18 | 19 | void serializeFitness() const; 20 | void deserializeFitness(const char* fileName); 21 | void reportFitness() const; 22 | 23 | void sort(); 24 | void print() const; 25 | 26 | 27 | private: 28 | int getClientNumber(const char* name); 29 | 30 | void resize(); 31 | void copy(const Fitness& fitness); 32 | void clear(); 33 | 34 | private: 35 | char* name; 36 | Client* clients; 37 | unsigned currentSize; 38 | unsigned capacity; 39 | }; 40 | 41 | #endif -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Person-Student-Teacher-Tasks/Person.cpp: -------------------------------------------------------------------------------- 1 | #include "Person.h" 2 | #include 3 | 4 | /* 5 | * we can change birthYear,why? 6 | */ 7 | Person::Person(const char * personName,const int birth) 8 | :name(NULL) 9 | ,birthYear(birth) 10 | { 11 | name = new char[strlen(personName) + 1]; 12 | strcpy(name, personName); 13 | } 14 | 15 | /* 16 | * we can change birthYear,why? 17 | */ 18 | Person::Person(const Person & other) 19 | :birthYear(other.birthYear) 20 | { 21 | copy(other); 22 | } 23 | 24 | Person::~Person() 25 | { 26 | clean(); 27 | } 28 | 29 | void Person::printInformation() const 30 | { 31 | std::cout << "Name: " << name << std::endl; 32 | std::cout << "birth year: " << birthYear << std::endl; 33 | } 34 | 35 | void Person::clean() 36 | { 37 | delete[] name; 38 | } 39 | 40 | /* 41 | * we can`t change birthYear,why? 42 | */ 43 | void Person::copy(const Person & other) 44 | { 45 | name = new char[strlen(other.name) + 1]; 46 | strcpy(name, other.name); 47 | } 48 | -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/LabMainProgram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Laboratory.h" 5 | #include "HumiditySensor.h" 6 | #include "LightSensor.h" 7 | #include "TemperatureSensor.h" 8 | 9 | int main() 10 | { 11 | srand(time(nullptr)); 12 | unsigned short test = 0; 13 | Laboratory lab("staq 706"); 14 | lab.addSensor(LightSensor("You")); 15 | lab.addSensor(HumiditySensor("must not")); 16 | lab.addSensor(TemperatureSensor("name")); 17 | lab.addSensor(LightSensor("variables")); 18 | lab.addSensor(HumiditySensor("badly")); 19 | lab.addSensor(TemperatureSensor("because")); 20 | lab.addSensor(LightSensor("you")); 21 | lab.addSensor(HumiditySensor("will not")); 22 | lab.addSensor(TemperatureSensor("take the exam!")); 23 | 24 | while (!lab.isLaboratoryReadyForExperiments()) { 25 | std::cout << "The lab is not ready yet!\n"; 26 | test++; 27 | } 28 | std::cout <<"The lab is ready for experiments\nIt took "< 5 | 6 | static const int maxNameSize = 64; 7 | static const int maxCourseSize = 32; 8 | static unsigned int globalFn = 45000; 9 | 10 | const char * courseNames[] = { "Computer Science", "Informatics", "Mathematics", "Invalid" }; 11 | 12 | enum Courses 13 | { 14 | INVALID_COURSE = -1, 15 | 16 | COMPUTER_SCIENCE, 17 | INFORMATICS, 18 | MATHEMATICS, 19 | 20 | COURSE_COUNT 21 | }; 22 | 23 | struct Student { 24 | char name[maxNameSize]; 25 | Courses course; 26 | unsigned short age; 27 | unsigned int facultyNumber; 28 | }; 29 | 30 | Courses setCourse(const char * course); 31 | 32 | void readData(char data[], int size); 33 | void setStudent(Student & alumni); 34 | 35 | void printStudent(const Student & alumni); 36 | void printStudent2(const Student & alumni, std::ostream& out); 37 | 38 | void serialize(const Student& alumni, std::ofstream& out); 39 | void deserialize(Student & alumni, std::ifstream & in); 40 | 41 | 42 | #endif // !__STUDENT__HEADER__INCLUDED__ 43 | 44 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/ID.cpp: -------------------------------------------------------------------------------- 1 | #include "ID.h" 2 | using namespace std; 3 | 4 | static unsigned short counter; 5 | static const char * fileName = "ID.dat"; 6 | 7 | 8 | bool file_exists() 9 | { 10 | ifstream in(fileName, ios::binary); 11 | if (!in) 12 | return false; 13 | in.close(); 14 | return true; 15 | } 16 | 17 | void initialization() 18 | { 19 | counter = 0; 20 | if (file_exists()) 21 | { 22 | ifstream in(fileName,ios::binary); 23 | if (!in) 24 | { 25 | cerr << "Error opening file " << fileName << " for reading!\n"; 26 | return; 27 | } 28 | 29 | in.read((char*)&counter, sizeof(counter)); 30 | if (in.gcount() != sizeof(counter)) 31 | { 32 | cerr << "Error occurre while reading file: " 33 | << fileName << "\n"; 34 | counter = 0; 35 | } 36 | in.close(); 37 | } 38 | } 39 | 40 | void save_changes() 41 | { 42 | ofstream out(fileName, ios::binary | ios::trunc); 43 | if (!out) 44 | { 45 | cerr << "Error opening file " << fileName << " for writing!"; 46 | return; 47 | } 48 | out.write((const char*)&counter, sizeof(counter)); 49 | out.close(); 50 | } 51 | 52 | unsigned short getID() 53 | { 54 | return ++counter; 55 | } 56 | -------------------------------------------------------------------------------- /Practice/Practice-11-12-Polymorphism/Physist Lab - Polymorphism/Sensor.cpp: -------------------------------------------------------------------------------- 1 | #include "Sensor.h" 2 | #include 3 | 4 | static unsigned int ID = 0; 5 | 6 | Sensor::Sensor(const char * description):id(ID++) 7 | { 8 | this->description = new char[strlen(description) + 1]; 9 | strcpy(this->description, description); 10 | } 11 | 12 | Sensor::Sensor(const Sensor & other) :id(ID++) 13 | { 14 | description = new char[strlen(other.description) + 1]; 15 | strcpy(description,other.description); 16 | } 17 | 18 | Sensor& Sensor::operator=(const Sensor & other) 19 | { 20 | if (this != &other) { 21 | id = ID++; 22 | delete[] description; 23 | description = new char[strlen(other.description) + 1]; 24 | strcpy(other.description, description); 25 | } 26 | 27 | return *this; 28 | } 29 | 30 | Sensor::~Sensor() 31 | { 32 | delete[] description; 33 | } 34 | 35 | const char * Sensor::getDescription() const 36 | { 37 | return description; 38 | } 39 | 40 | void Sensor::setDescription(const char * descr) 41 | { 42 | delete[] description; 43 | description = new char[strlen(descr) + 1]; 44 | strcpy(description, descr); 45 | } 46 | 47 | unsigned short Sensor::getID() const 48 | { 49 | return id; 50 | } 51 | -------------------------------------------------------------------------------- /Seminar/string-class/String.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class String { 6 | private: 7 | char *content; 8 | size_t size; 9 | 10 | public: 11 | String(); 12 | String(const char*); 13 | ~String(); 14 | String(const String&); 15 | const String& operator=(const String&); 16 | 17 | public: 18 | size_t getSize() const; 19 | char* getContent() const; 20 | 21 | const char& at(size_t) const; 22 | char& at(size_t); 23 | 24 | // NOTE: We don't need function "append(const char *)" 25 | // because of implicit conversion using constuctor "String(const char*)". 26 | // Using this conversion every function taking parameter of type "String" 27 | // can take "const char *" parameter instead. 28 | // 29 | // More on subject: 30 | // https://www.quora.com/What-is-the-best-way-to-understand-convert-constructors-in-c++ 31 | 32 | String& append(const String&); 33 | bool compare(const String&) const; 34 | bool find(const String&) const; 35 | 36 | String substr(size_t, size_t) const; 37 | 38 | // Operator for printing class using cout 39 | friend std::ostream& operator<<(std::ostream&, const String&); 40 | 41 | private: 42 | void copy(const char*, size_t); 43 | void free(); 44 | }; 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/main.cpp: -------------------------------------------------------------------------------- 1 | /********************************************** 2 | * 3 | * available commands: 4 | * - cp: create player by given name 5 | * - cc: create card by given color (supported colors: red, black, blue, white, green) and name 6 | * - cd: create deck by given player name and 60 card names 7 | * - report: generate text file of all decks, ordered by color 8 | * 9 | * *all commands (cp, cc, cd) saves the corresponding objects by 10 | * generating binary files with names as follow 11 | * "player.dat", "card.dat" and "deck.dat". 12 | * These files are taking into account for the next opening of the program 13 | */ 14 | 15 | 16 | #include "Player.h" 17 | #include "Card.h" 18 | #include "Deck.h" 19 | 20 | using std::cin; 21 | using std::cout; 22 | 23 | int main() 24 | { 25 | initialization(); 26 | char command[8]; 27 | while (true) 28 | { 29 | cin >> command; 30 | if (stricmp(command, "cc") == 0) 31 | { 32 | createCard(); 33 | } 34 | else if (stricmp(command, "cp") == 0) 35 | { 36 | createPlayer(); 37 | } 38 | else if (stricmp(command, "cd") == 0) 39 | { 40 | createDeck(); 41 | } 42 | else if (stricmp(command, "report") == 0) 43 | { 44 | report(); 45 | } 46 | else 47 | break; 48 | } 49 | save_changes(); 50 | return 0; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Person-Student-Teacher-Tasks/Student.cpp: -------------------------------------------------------------------------------- 1 | #include "Student.h" 2 | #include 3 | 4 | Student::Student(const char * name,const int birthYear, 5 | const char * universityName, const char * courseName, int fn) 6 | :Person(name,birthYear) 7 | ,university(NULL) 8 | ,course(NULL) 9 | ,facultyNumber(fn) 10 | { 11 | university = new char[strlen(universityName) + 1]; 12 | strcpy(university, universityName); 13 | 14 | course = new char[strlen(courseName) + 1]; 15 | strcpy(course, courseName); 16 | } 17 | 18 | Student::Student(const Student & other) 19 | :Person(other) 20 | { 21 | copy(other); 22 | } 23 | 24 | 25 | Student::~Student() 26 | { 27 | clean(); 28 | } 29 | 30 | void Student::printInformation() const 31 | { 32 | Person::printInformation(); 33 | std::cout << "University: " << university << std::endl; 34 | std::cout << "Course: " << course << std::endl; 35 | std::cout << "Faculty number: " << facultyNumber << std::endl; 36 | } 37 | 38 | void Student::clean() 39 | { 40 | delete[] university; 41 | delete[] course; 42 | } 43 | 44 | void Student::copy(const Student & other) 45 | { 46 | university = new char[strlen(other.university) + 1]; 47 | strcpy(university, other.university); 48 | 49 | course = new char[strlen(other.course) + 1]; 50 | strcpy(course, other.course); 51 | 52 | facultyNumber = other.facultyNumber; 53 | } 54 | -------------------------------------------------------------------------------- /Practice/Practice-07-Serialization-Deserialization/Egg.cpp: -------------------------------------------------------------------------------- 1 | #include "Egg.h" 2 | using namespace std; 3 | 4 | 5 | Egg::Egg() 6 | :eggName(NULL) 7 | ,eggSize(0) 8 | {} 9 | 10 | Egg::Egg(const char * name) 11 | :eggName(NULL) 12 | ,eggSize(0) 13 | { 14 | eggSize = strlen(name) + 1; 15 | eggName = new char[eggSize]; 16 | strcpy(eggName, name); 17 | } 18 | 19 | Egg::~Egg() 20 | { 21 | clean(); 22 | } 23 | 24 | 25 | Egg::Egg(const Egg & rhs) 26 | { 27 | copy(rhs); 28 | } 29 | 30 | Egg& Egg::operator=(const Egg & rhs) 31 | { 32 | 33 | if (this != &rhs) 34 | { 35 | clean(); 36 | copy(rhs); 37 | } 38 | return *this; 39 | } 40 | 41 | unsigned int Egg::getEggSize() const 42 | { 43 | return eggSize; 44 | } 45 | 46 | const char * Egg::getEggName() const 47 | { 48 | return eggName; 49 | } 50 | 51 | void Egg::eggPrint(ofstream & out) 52 | { 53 | out << eggName << " " 54 | << eggSize << " \n"; 55 | } 56 | 57 | void Egg::eggSerialization(ofstream & out) 58 | { 59 | out.write((const char *)&eggSize ,sizeof(eggSize)); 60 | out.write(eggName, eggSize); 61 | } 62 | 63 | void Egg::eggDeserialization(ifstream & in) 64 | { 65 | in.read((char*)&eggSize, sizeof(eggSize)); 66 | eggName = new char[eggSize]; 67 | in.read(eggName, eggSize); 68 | } 69 | 70 | void Egg::copy(const Egg &rhs) 71 | { 72 | eggSize = rhs.eggSize; 73 | eggName = new char[eggSize]; 74 | strcpy(eggName, rhs.eggName); 75 | } 76 | 77 | void Egg::clean() 78 | { 79 | delete[] eggName; 80 | } -------------------------------------------------------------------------------- /Practice/Practice-01-Structures/Practice_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Point 4 | { 5 | int x; 6 | int y; 7 | }; 8 | 9 | void setPoint(Point & point) 10 | { 11 | std::cin >> point.x; 12 | std::cin >> point.y; 13 | } 14 | 15 | void printPoint(const Point & point) 16 | { 17 | std::cout << "(" << point.x << "," 18 | << point.y << ")" << std::endl; 19 | } 20 | 21 | int delta(int X0, int X1) 22 | { 23 | return X1 - X0; 24 | } 25 | 26 | double distance(const Point & A, const Point & B) 27 | { 28 | // get the difference between each coordinate 29 | 30 | int deltaX = delta(A.x, B.x); 31 | int deltaY = delta(A.y, B.y); 32 | 33 | double result = sqrt(pow(deltaX, 2) + pow(deltaY, 2)); 34 | 35 | return result; 36 | } 37 | 38 | 39 | void createPointArr(Point *& arr, int size) 40 | { 41 | arr = new(std::nothrow) Point[size]; 42 | if (!arr) 43 | { 44 | std::cerr << "Not enough memory to complete the task!" << std::endl; 45 | return; 46 | } 47 | } 48 | 49 | void initializePointArr(Point * arr,int size) 50 | { 51 | for (size_t i = 0; i < size; i++) 52 | { 53 | setPoint(arr[i]); 54 | } 55 | } 56 | 57 | void printPointArr(Point * arr, int size) 58 | { 59 | for (size_t i = 0; i < size; i++) 60 | { 61 | std::cout << "point p" << i << " coordinates: "; 62 | printPoint(arr[i]); 63 | } 64 | } 65 | 66 | 67 | int main() 68 | { 69 | Point* arr; 70 | createPointArr(arr, 2); 71 | initializePointArr(arr, 2); 72 | printPointArr(arr, 2); 73 | 74 | std::cout << "The distance between the points is: " << distance(arr[0], arr[1]) << std::endl; 75 | 76 | delete[] arr; 77 | 78 | return 0; 79 | } -------------------------------------------------------------------------------- /Practice/Practice-09-10-Inheritance/Person-Student-Teacher-Tasks/Teacher.cpp: -------------------------------------------------------------------------------- 1 | #include "Teacher.h" 2 | #include 3 | 4 | Teacher::Teacher(const char * name, int birthYear, const char * universityName, const char * courseName) 5 | :Person(name, birthYear) 6 | , university(NULL) 7 | , subject(NULL) 8 | { 9 | university = new char[strlen(universityName) + 1]; 10 | strcpy(university, universityName); 11 | 12 | subject = new char[strlen(courseName) + 1]; 13 | strcpy(subject, courseName); 14 | } 15 | 16 | Teacher::Teacher(const Teacher & other) 17 | :Person(other) 18 | { 19 | copy(other); 20 | } 21 | 22 | Teacher::~Teacher() 23 | { 24 | clean(); 25 | } 26 | 27 | void Teacher::setUniversity(const char * universityName) 28 | { 29 | if (!universityName) 30 | { 31 | delete[] university; 32 | university = new char[strlen(universityName) + 1]; 33 | strcpy(university, universityName); 34 | } 35 | } 36 | 37 | void Teacher::setSubject(const char * subjectName) 38 | { 39 | if (!subjectName) 40 | { 41 | delete[] subject; 42 | subject = new char[strlen(subjectName) + 1]; 43 | strcpy(subject, subjectName); 44 | } 45 | } 46 | 47 | void Teacher::printInformation() const 48 | { 49 | Person::printInformation(); 50 | std::cout << "University: " << university << std::endl; 51 | std::cout << "Subject: " << subject << std::endl; 52 | } 53 | 54 | void Teacher::clean() 55 | { 56 | delete[] university; 57 | delete[] subject; 58 | } 59 | 60 | void Teacher::copy(const Teacher & other) 61 | { 62 | university = new char[strlen(other.university) + 1]; 63 | strcpy(university, other.university); 64 | 65 | subject = new char[strlen(other.subject) + 1]; 66 | strcpy(subject, other.subject); 67 | } 68 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/PhysicsLab/TemperatureSensor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TemperatureSensor.h" 3 | 4 | static unsigned int ID = 0; 5 | 6 | TemperatureSensor::TemperatureSensor(): 7 | description(nullptr),id(ID++) 8 | { 9 | //already assigned in initializer list 10 | } 11 | 12 | TemperatureSensor::TemperatureSensor(const TemperatureSensor& other) 13 | { 14 | copyFrom(other); 15 | } 16 | 17 | TemperatureSensor & TemperatureSensor::operator=(const TemperatureSensor& other) 18 | { 19 | if (this != &other) 20 | { 21 | freeData(); 22 | copyFrom(other); 23 | } 24 | 25 | return *this; 26 | } 27 | 28 | TemperatureSensor::~TemperatureSensor() 29 | { 30 | freeData(); 31 | } 32 | 33 | TemperatureSensor::TemperatureSensor(const char * descr):id(ID++) 34 | { 35 | description = new char[strlen(descr) + 1]; 36 | strcpy(description, descr); 37 | } 38 | 39 | const char * TemperatureSensor::getDescription() const 40 | { 41 | if (this->description == nullptr) 42 | { 43 | std::cerr << "Sensor doesn,t have description\n"; 44 | return nullptr; 45 | } 46 | return description; 47 | } 48 | 49 | void TemperatureSensor::setDescription(const char * descr) 50 | { 51 | delete[] description; 52 | description = new char[strlen(descr) + 1]; 53 | strcpy(description, descr); 54 | } 55 | 56 | unsigned short TemperatureSensor::getID() const 57 | { 58 | return this->id; 59 | } 60 | 61 | void TemperatureSensor::setNewID() 62 | { 63 | this->id = ID++; 64 | } 65 | 66 | float TemperatureSensor::randomFloat() const 67 | { 68 | return rand() / (float)RAND_MAX; 69 | } 70 | 71 | float TemperatureSensor::getTemperature() const 72 | { 73 | return randomFloat(); 74 | } 75 | 76 | void TemperatureSensor::copyFrom(const TemperatureSensor& other) 77 | { 78 | this->description = new char[strlen(other.description) + 1]; 79 | strcpy(this->description, other.description); 80 | this->id = other.id; 81 | } 82 | 83 | void TemperatureSensor::freeData() 84 | { 85 | delete[] description; 86 | description = nullptr; 87 | } -------------------------------------------------------------------------------- /Seminar/string-class/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "String.h" 3 | 4 | void testAppend(const String& p, const char* str) { 5 | String newStr; 6 | try { 7 | newStr = p; 8 | newStr.append(str); 9 | } catch (std::invalid_argument& i) { 10 | std::cerr << "Error: " << i.what() << std::endl; 11 | return; 12 | } catch (std::bad_alloc& b) { 13 | std::cerr << "Failed to allocate: " << b.what() << std::endl; 14 | // Can't do anything when allocation fails 15 | // So we let the exception appear to the user. 16 | throw; 17 | } 18 | 19 | std::cout << "'" << p << "' + '" << str << "' is: " << newStr << std::endl; 20 | } 21 | 22 | void testCompare(const String& p, const char* str) { 23 | std::cout << "'" << p << "' and '" << str << "' are the same: " << 24 | (p.compare(str) ? "true" : "false") << std::endl; 25 | } 26 | 27 | void testFind(const String& p, const char* str) { 28 | std::cout << "'" << p << "' has subtring '" << str << "': " << 29 | (p.find(str) ? "true" : "false") << std::endl; 30 | } 31 | 32 | void testSubstring(const String& p, size_t pos, size_t len) { 33 | String sub; 34 | try { 35 | sub = p.substr(pos, len); 36 | } catch(std::out_of_range& e) { 37 | std::cerr << "Error occured: " << e.what() << std::endl; 38 | return; 39 | } 40 | 41 | std::cout << "Substring of '" << p << "' begining from index " << pos << 42 | " and length " << len << " is: " << p.substr(pos, len) << std::endl; 43 | } 44 | 45 | void testIndexing(const String& p, const size_t index) { 46 | char c; 47 | try { 48 | c = p.at(index); 49 | } catch (std::out_of_range& o) { 50 | std::cerr << "Error: " << o.what() << std::endl; 51 | return; 52 | } 53 | 54 | std::cout << "Char on index " << index << " of string '" << p << "' is: " << c << std::endl; 55 | } 56 | 57 | int main() { 58 | String p = "pesho"; 59 | std::cout << "The string is: " << p << std::endl; 60 | 61 | testAppend(p, " i Gosho"); 62 | testCompare(p, "Pesho"); 63 | testFind(p, "esh"); 64 | testIndexing(p, 1); 65 | testSubstring(p, 2, 2); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Seminar/binary-files.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const std::size_t SIZE_NAME = 64; 6 | 7 | struct Human { 8 | char name[SIZE_NAME]; 9 | int age; 10 | bool hasCat; 11 | }; 12 | 13 | bool writeHuman(const char *filename, const Human &h) { 14 | std::ofstream file (filename, std::ios::out | std::ios::binary); 15 | 16 | if ( !file.good() ) { 17 | std::cerr << "Coudln't open file: " << filename << std::endl; 18 | return false; 19 | } 20 | 21 | file.write((const char*)&h, sizeof(h)); 22 | if ( file.bad() ) { 23 | std::cerr << "Dumping of struct Human in file failed" << std::endl; 24 | return false; 25 | } 26 | 27 | file.close(); 28 | return true; 29 | } 30 | 31 | bool readHuman(const char *filename, Human &h) { 32 | std::ifstream file (filename, std::ios::in | std::ios::binary); 33 | if ( !file.good() ) { 34 | std::cerr << "Coudln't open file!" << std::endl; 35 | return false; 36 | } 37 | 38 | Human tempH; 39 | file.read((char*)&tempH, sizeof(tempH)); 40 | if ( file.bad() ) { 41 | std::cerr << "Loading of struct Human from file failed" << std::endl; 42 | return false; 43 | } 44 | 45 | h = tempH; 46 | file.close(); 47 | return true; 48 | } 49 | 50 | bool fileExists(const char *filename) { 51 | std::ifstream f(filename); 52 | return f.good(); 53 | } 54 | 55 | void printHuman(const Human &h) { 56 | std::cout << 57 | "Human {\n\tname: " << h.name << 58 | "\n\tage: " << h.age << 59 | "\n\thas a cat: " << h.hasCat << 60 | "\n}" << std::endl; 61 | } 62 | 63 | int main() { 64 | const char filename[] = "human-dump.bin"; 65 | 66 | Human pesho; 67 | if ( fileExists(filename) ) { 68 | std::cout << "File with Human exists!\n Reading struct..." << std::endl; 69 | readHuman(filename, pesho); 70 | } else { 71 | std::cout << "File with Human DOESN'T exists!\n Writing struct in file..." << std::endl; 72 | char name[] = "Pesho"; 73 | strncpy(pesho.name, name, strlen(name)); 74 | pesho.age = 42; 75 | pesho.hasCat = true; 76 | writeHuman(filename, pesho); 77 | } 78 | 79 | printHuman(pesho); 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/Player.cpp: -------------------------------------------------------------------------------- 1 | #include "Player.h" 2 | using namespace std; 3 | 4 | const char * playerFile = "player.dat"; 5 | 6 | void savePlayer(const Player &player) 7 | { 8 | ofstream out(playerFile, ios::binary | ios::app); 9 | if (out.is_open()) 10 | out.write((const char*)&player, sizeof(Player)); 11 | else 12 | cerr << "Error opening file " << playerFile << " for writing!"; 13 | out.close(); 14 | } 15 | 16 | void createPlayer() 17 | { 18 | Player newPlayer; 19 | readName(newPlayer.name, maxPlayerName); 20 | if (!alreadyExists(newPlayer.name)) 21 | { 22 | newPlayer.ID = getID(); 23 | savePlayer(newPlayer); 24 | } 25 | } 26 | 27 | static bool alreadyExists(char name[]) 28 | { 29 | Player check; 30 | bool flag = false; 31 | ifstream in(playerFile, ios::binary); 32 | if (!in.is_open()) 33 | { 34 | in.close(); 35 | return flag; 36 | } 37 | while (in.eof()) 38 | { 39 | in.read((char*)&check, sizeof(check)); 40 | if (stricmp(check.name, name) == 0) 41 | { 42 | flag = true; 43 | break; 44 | } 45 | } 46 | in.close(); 47 | return flag; 48 | } 49 | 50 | bool playerGetID(Player &player) 51 | { 52 | Player check; 53 | bool flag = false; 54 | ifstream in(playerFile, ios::binary); 55 | if (!in) 56 | { 57 | cerr << "Error opening file " << playerFile << " for reading!"; 58 | return flag; 59 | } 60 | while (!in.eof()) 61 | { 62 | in.read((char *)&check, sizeof(check)); 63 | if (stricmp(player.name, check.name) == 0) 64 | { 65 | flag = true; 66 | player.ID = check.ID; 67 | break; 68 | } 69 | } 70 | in.close(); 71 | 72 | return flag; 73 | } 74 | 75 | bool playerGetName(Player &player) 76 | { 77 | bool flag = false; 78 | Player check; 79 | ifstream in(playerFile, ios::binary); 80 | if (!in) 81 | { 82 | cerr << "Error opening file: " << playerFile << " for reading! \n"; 83 | return flag; 84 | } 85 | while (!in.eof()) 86 | { 87 | in.read((char *)&check, sizeof(check)); 88 | if (check.ID == player.ID) 89 | { 90 | strcpy(player.name, check.name); 91 | flag = true; 92 | break; 93 | } 94 | } 95 | in.close(); 96 | return flag; 97 | } 98 | -------------------------------------------------------------------------------- /Practice/Practice-02-Files/Student.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Student.h" 4 | 5 | using namespace std; 6 | 7 | Courses setCourse(const char * course) 8 | { 9 | for (size_t i = 0; i < COURSE_COUNT; i++) 10 | { 11 | if (stricmp(course, courseNames[i]) == 0) 12 | { 13 | return (Courses)i; 14 | } 15 | } 16 | return INVALID_COURSE; 17 | } 18 | 19 | void readData(char data[], int size) 20 | { 21 | while (cin && isspace(cin.peek())) 22 | cin.ignore(); 23 | cin.getline(data, size); 24 | if (cin.fail()) 25 | { 26 | cin.clear(); 27 | while (cin && '\n' != cin.get()); 28 | } 29 | cin.clear(); 30 | } 31 | 32 | void setStudent(Student & alumni) 33 | { 34 | readData(alumni.name, maxNameSize); 35 | 36 | char * buffer = new char[maxCourseSize]; 37 | readData(buffer, maxCourseSize); 38 | alumni.course = setCourse(buffer); 39 | delete[] buffer; 40 | 41 | cin >> alumni.age; 42 | alumni.facultyNumber = globalFn; 43 | globalFn++; 44 | } 45 | 46 | void printStudent(const Student & alumni) 47 | { 48 | cout << "*************************** \n"; 49 | cout << "Student information: \n \n"; 50 | cout << "name: " << alumni.name << endl; 51 | cout << "course: " << courseNames[alumni.course] << endl; 52 | cout << "age: " << alumni.age << endl; 53 | cout << "faculty number: " << alumni.facultyNumber << endl; 54 | } 55 | 56 | void printStudent2(const Student & alumni, ostream& out=cout) 57 | { 58 | out << alumni.name << ' ' << courseNames[alumni.course] << ' ' << alumni.age << ' ' << alumni.facultyNumber << ' '; 59 | } 60 | 61 | void serialize(const Student& alumni, ofstream& out) 62 | { 63 | out.write((const char *)&alumni, sizeof(Student)); 64 | } 65 | 66 | void deserialize(Student & alumni, ifstream & in) 67 | { 68 | in.read((char *)&alumni, sizeof(Student)); 69 | } 70 | 71 | 72 | 73 | int main() 74 | { 75 | Student s; 76 | setStudent(s); 77 | //printStudent(s); 78 | 79 | ofstream out("students.dat", ios:: binary | ios::trunc); 80 | serialize(s, out); 81 | out.close(); 82 | 83 | Student p; 84 | ifstream in("students.dat", ios::binary); 85 | deserialize(p, in); 86 | in.close(); 87 | 88 | printStudent(p); 89 | 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /Practice/Practice-08-Exam/Exam Task - Solution/Client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"Client.h" 3 | 4 | //BIG4: 5 | 6 | Client::Client() 7 | : age(0) 8 | , weight(0) 9 | , name(nullptr) 10 | {} 11 | 12 | Client::Client(const char* name) 13 | : age(0) 14 | , weight(0) 15 | { 16 | 17 | this->name = new char[strlen(name) + 1]; 18 | strcpy(this->name, name); 19 | } 20 | 21 | Client::Client(const char* name, unsigned age, unsigned weight) { 22 | 23 | this->age = age; 24 | this->weight = weight; 25 | 26 | this->name = new char[strlen(name) + 1]; 27 | strcpy(this->name, name); 28 | } 29 | 30 | Client::Client(const Client& client) { 31 | 32 | copy(client); 33 | } 34 | 35 | Client& Client::operator=(const Client& client) { 36 | 37 | if (this != &client) { 38 | 39 | clear(); 40 | copy(client); 41 | } 42 | 43 | return *this; 44 | } 45 | 46 | Client::~Client() { 47 | 48 | clear(); 49 | } 50 | 51 | //OTHER PUBLIC: 52 | 53 | void Client::serializeClient(std::ofstream& out) const{ 54 | 55 | out.write((const char*)&age, sizeof(age)); 56 | out.write((const char*)&weight, sizeof(weight)); 57 | 58 | unsigned nameLen = strlen(name) + 1; 59 | out.write((const char*)&nameLen, sizeof(nameLen)); 60 | out.write(name, nameLen); 61 | } 62 | 63 | void Client::deserializeClient(std::ifstream& in) { 64 | 65 | in.read((char*)&age, sizeof(age)); 66 | in.read((char*)&weight, sizeof(weight)); 67 | 68 | unsigned nameLen; 69 | in.read((char*)&nameLen, sizeof(nameLen)); 70 | name = new char[nameLen]; 71 | in.read(name, nameLen); 72 | } 73 | 74 | void Client::reportClient(std::ofstream& out) const{ 75 | 76 | out << "Client: " << name 77 | << ", Age: " << age 78 | << ", Weight: " << weight << '\n'; 79 | } 80 | 81 | void Client::changeWeight(unsigned newWeight) { 82 | 83 | weight = newWeight; 84 | } 85 | 86 | const char* Client::getName() const{ 87 | 88 | return name; 89 | } 90 | 91 | unsigned short Client::getAge() const 92 | { 93 | return age; 94 | } 95 | 96 | void Client::print() const { 97 | 98 | std::cout << "Name: "< 3 | #include 4 | 5 | Laboratory::Laboratory(const char* name):size(0),cap(2) 6 | { 7 | this->name = new char[strlen(name) + 1]; 8 | strcpy(this->name, name); 9 | sensors = new Sensor*[cap]; 10 | } 11 | 12 | Laboratory::Laboratory(const Laboratory& other) 13 | { 14 | copyFrom(other); 15 | } 16 | 17 | Laboratory & Laboratory::operator=(const Laboratory& other) 18 | { 19 | if (this != &other) 20 | { 21 | freeData(); 22 | copyFrom(other); 23 | } 24 | 25 | return *this; 26 | } 27 | 28 | Laboratory::~Laboratory() 29 | { 30 | freeData(); 31 | } 32 | 33 | const char * Laboratory::getName() const 34 | { 35 | if (this->name == nullptr) 36 | { 37 | std::cerr << "Laboratory doesn,t have name\n"; 38 | return nullptr; 39 | } 40 | 41 | return this->name; 42 | } 43 | 44 | void Laboratory::setName(const char * newName) 45 | { 46 | name = new char[strlen(newName) + 1]; 47 | strcpy(name, newName); 48 | } 49 | 50 | const Sensor& Laboratory::getSensor(unsigned int index) const 51 | { 52 | if (index > size) 53 | { 54 | throw std::out_of_range("Index is too large"); 55 | } 56 | 57 | return *sensors[index - 1]; 58 | } 59 | 60 | void Laboratory::addSensor(const Sensor& newSensor) 61 | { 62 | if (size >= cap) 63 | { 64 | resize(); 65 | } 66 | 67 | sensors[size] = newSensor.clone(); 68 | size++; 69 | } 70 | 71 | bool Laboratory::isLaboratoryReadyForExperiments() const 72 | { 73 | double values = 0; 74 | for (size_t i = 0; i < size; i++) 75 | { 76 | values += sensors[i]->getValue(); 77 | } 78 | 79 | return ((values / size) > 66); 80 | } 81 | 82 | void Laboratory::copyFrom(const Laboratory& other) 83 | { 84 | this->name = new char[strlen(other.name)+1]; 85 | strcpy(this->name, other.name); 86 | 87 | this->size = other.size; 88 | this->cap = other.cap; 89 | 90 | this->sensors = new Sensor*[other.size]; 91 | for (int i = 0; i < other.cap; i++) 92 | { 93 | this->sensors[i] = other.sensors[i]->clone(); 94 | } 95 | } 96 | 97 | void Laboratory::freeData() 98 | { 99 | delete[] name; 100 | for (int i = 0; i < size; i++) 101 | { 102 | delete sensors[i]; 103 | } 104 | delete[] sensors; 105 | } 106 | 107 | void Laboratory::resize() 108 | { 109 | Sensor** temp = new Sensor*[2 * cap]; 110 | 111 | for (int i = 0; i < size; i++) 112 | { 113 | temp[i] = sensors[i]; 114 | } 115 | 116 | cap *= 2; 117 | 118 | delete[] sensors; 119 | sensors = temp; 120 | } 121 | -------------------------------------------------------------------------------- /Practice/Practice-05-06-Classes-BIG4/PhysicsLab/Laboratory.cpp: -------------------------------------------------------------------------------- 1 | #include "Laboratory.h" 2 | #include 3 | 4 | Laboratory::Laboratory(): 5 | name(nullptr),size(0),capacity(2) 6 | { 7 | //already assigned in initializer list 8 | sensors = new TemperatureSensor[capacity]; 9 | } 10 | 11 | Laboratory::Laboratory(const Laboratory& other) 12 | { 13 | copyFrom(other); 14 | } 15 | 16 | Laboratory & Laboratory::operator=(const Laboratory& other) 17 | { 18 | if (this != &other) 19 | { 20 | freeData(); 21 | copyFrom(other); 22 | } 23 | 24 | return *this; 25 | } 26 | 27 | Laboratory::~Laboratory() 28 | { 29 | freeData(); 30 | } 31 | 32 | const char * Laboratory::getName() const 33 | { 34 | if (this->name == nullptr) 35 | { 36 | std::cerr << "Laboratory doesn,t have name\n"; 37 | return nullptr; 38 | } 39 | 40 | return this->name; 41 | } 42 | 43 | void Laboratory::setName(const char * newName) 44 | { 45 | delete[] name; 46 | 47 | name = new char[strlen(newName) + 1]; 48 | strcpy(name, newName); 49 | } 50 | 51 | const TemperatureSensor Laboratory::getSensor(unsigned int index) const 52 | { 53 | if (index > size) 54 | { 55 | std::cerr << "Too big index, there are not so much elements\'n"; 56 | return TemperatureSensor("Invalid"); 57 | } 58 | 59 | return sensors[index - 1]; 60 | } 61 | 62 | void Laboratory::addSensor(const TemperatureSensor& newSensor) 63 | { 64 | if (size >= capacity) 65 | { 66 | resize(); 67 | } 68 | 69 | sensors[size] = newSensor; 70 | size++; 71 | } 72 | 73 | float Laboratory::getCurrentTemperature() const 74 | { 75 | float sum = 0; 76 | 77 | for (int i = 0; i < size; i++) 78 | { 79 | sum += sensors[i].getTemperature(); 80 | } 81 | 82 | return sum / size; 83 | } 84 | 85 | void Laboratory::copyFrom(const Laboratory& other) 86 | { 87 | this->name = new char[strlen(other.name)+1]; 88 | strcpy(this->name, other.name); 89 | 90 | this->size = other.size; 91 | this->capacity = other.capacity; 92 | 93 | this->sensors = new TemperatureSensor[other.size]; 94 | for (int i = 0; i < other.capacity; i++) 95 | { 96 | this->sensors[i] = other.sensors[i]; 97 | } 98 | } 99 | 100 | void Laboratory::freeData() 101 | { 102 | delete[] name; 103 | name = nullptr; 104 | delete[] sensors; 105 | sensors = nullptr; 106 | } 107 | 108 | void Laboratory::resize() 109 | { 110 | TemperatureSensor* temp = new TemperatureSensor[2 * capacity]; 111 | 112 | for (int i = 0; i < size; i++) 113 | { 114 | temp[i] = sensors[i]; 115 | } 116 | 117 | capacity *= 2; 118 | 119 | delete[] sensors; 120 | sensors = temp; 121 | } 122 | -------------------------------------------------------------------------------- /Practice/Practice-13-14 OperatorOverloading/MyString.cpp: -------------------------------------------------------------------------------- 1 | #include "MyString.h" 2 | 3 | using std::istream; 4 | using std::ostream; 5 | 6 | static const int bufferSize = 1024; 7 | 8 | MyString::MyString() 9 | :str(NULL) 10 | {} 11 | 12 | MyString::MyString(const char * text) 13 | : str(NULL) 14 | { 15 | str = new char[strlen(text) + 1]; 16 | strcpy(str, text); 17 | } 18 | 19 | MyString::~MyString() 20 | { 21 | clean(); 22 | } 23 | 24 | MyString::MyString(const MyString & input) 25 | { 26 | copy(input); 27 | } 28 | 29 | MyString& MyString::operator=(const MyString& input) 30 | { 31 | if (this != &input) 32 | { 33 | clean(); 34 | copy(input); 35 | } 36 | return *this; 37 | } 38 | 39 | unsigned int MyString::getStringWeigth() const 40 | { 41 | unsigned int result = 0; 42 | char * strPtr = str; 43 | while (*strPtr) 44 | { 45 | result += (unsigned int)(*strPtr); 46 | strPtr++; 47 | } 48 | return result; 49 | } 50 | 51 | MyString& MyString::operator+=(const MyString & input) 52 | { 53 | unsigned int length = strlen(str) + strlen(input.str) + 1; 54 | char * newString = new char[length]; 55 | strcpy(newString, str); 56 | strcat(newString, input.str); 57 | clean(); 58 | str = newString; 59 | return *this; 60 | } 61 | 62 | MyString & MyString::operator*=(int k) 63 | { 64 | int newLen = k*strlen(str) + 1; 65 | char * newString = new char[newLen]; 66 | strcpy(newString, str); 67 | 68 | for (size_t i = 0; i < k; i++) 69 | { 70 | strcat(newString, str); 71 | } 72 | 73 | clean(); 74 | str = newString; 75 | 76 | return *this; 77 | } 78 | 79 | MyString operator+(const MyString & lhs, const MyString & rhs) 80 | { 81 | MyString result(lhs); 82 | result += rhs; 83 | return result; 84 | } 85 | 86 | bool operator==(const MyString & lhs, const MyString & rhs) 87 | { 88 | unsigned int lhsWeigth = lhs.getStringWeigth(); 89 | unsigned int rhsWeigth = rhs.getStringWeigth(); 90 | return ((lhsWeigth == rhsWeigth) ? true : false); 91 | } 92 | 93 | bool operator!=(const MyString & lhs, const MyString & rhs) 94 | { 95 | return !(lhs == rhs); 96 | } 97 | 98 | istream& operator >> (istream & in, MyString& input) 99 | { 100 | char * buffer = new char[bufferSize]; 101 | in.getline(buffer, bufferSize); 102 | 103 | delete[] input.str; 104 | input.str = buffer; 105 | 106 | return in; 107 | } 108 | 109 | ostream & operator<<(ostream & out, const MyString & output) 110 | { 111 | out << output.str; 112 | return out; 113 | } 114 | 115 | void MyString::copy(const MyString & input) 116 | { 117 | str = new char[strlen(input.str) + 1]; 118 | strcpy(str, input.str); 119 | } 120 | 121 | void MyString::clean() 122 | { 123 | delete[] str; 124 | str = NULL; 125 | } -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/Card.cpp: -------------------------------------------------------------------------------- 1 | #include "Card.h" 2 | using namespace std; 3 | 4 | static const char * cardFile = "card.dat"; 5 | 6 | void createCard() 7 | { 8 | Card newCard; 9 | char color[16]; 10 | cin >> color; 11 | readName(newCard.name, maxCardName); 12 | if (!alreadyExists(newCard.name)) 13 | { 14 | newCard.color = readColor(color); 15 | newCard.ID = getID(); 16 | saveCard(newCard); 17 | } 18 | } 19 | 20 | void saveCard(const Card & card) 21 | { 22 | ofstream out(cardFile, ios::binary | ios::app); 23 | if (!out) 24 | { 25 | cerr << "Error opening file: " << cardFile << " for writing! \n"; 26 | return; 27 | } 28 | out.write((const char *)&card, sizeof(Card)); 29 | out.close(); 30 | } 31 | 32 | static bool alreadyExists(char name[]) 33 | { 34 | Card check; 35 | bool flag = false; 36 | ifstream in(cardFile, ios::binary); 37 | if (!in.is_open()) 38 | { 39 | in.close(); 40 | return flag; 41 | } 42 | while (in.eof()) 43 | { 44 | in.read((char*)&check, sizeof(check)); 45 | if (stricmp(check.name, name) == 0) 46 | { 47 | flag = true; 48 | break; 49 | } 50 | } 51 | in.close(); 52 | return flag; 53 | } 54 | 55 | bool cardGetID(Card & card) 56 | { 57 | Card check; 58 | bool flag = false; 59 | ifstream in(cardFile, ios::binary); 60 | if (!in) 61 | { 62 | cerr << "Error opening file: " << cardFile << " for reading! \n"; 63 | return flag; 64 | } 65 | while (!in.eof()) 66 | { 67 | in.read((char*)& check, sizeof(check)); 68 | if ((strcmp(check.name, card.name) == 0)) 69 | { 70 | card.color = check.color; 71 | card.ID = check.ID; 72 | flag = true; 73 | break; 74 | } 75 | } 76 | in.close(); 77 | 78 | return flag; 79 | } 80 | 81 | bool cardGetName(Card & card) 82 | { 83 | Card check; 84 | bool flag = false; 85 | ifstream in(cardFile, ios::binary); 86 | if (!in) 87 | { 88 | cerr << "Error opening file: " << cardFile << " for reading! \n"; 89 | return flag; 90 | } 91 | while (!in.eof()) 92 | { 93 | in.read((char*)& check, sizeof(check)); 94 | if (check.ID == card.ID) 95 | { 96 | card.color = check.color; 97 | strcpy(card.name, check.name); 98 | flag = true; 99 | break; 100 | } 101 | } 102 | in.close(); 103 | 104 | return flag; 105 | } 106 | 107 | Color readColor(char color[]) 108 | { 109 | if (stricmp(color, "Red") == 0) 110 | return RED; 111 | else if (stricmp(color, "Black") == 0) 112 | return BLACK; 113 | else if (stricmp(color, "Blue") == 0) 114 | return BLUE; 115 | else if (stricmp(color, "White") == 0) 116 | return WHITE; 117 | else if (stricmp(color, "Green") == 0) 118 | return GREEN; 119 | else 120 | return INVALID_COLOR; 121 | } 122 | -------------------------------------------------------------------------------- /Seminar/string-class/String.cpp: -------------------------------------------------------------------------------- 1 | #include "String.h" 2 | #include 3 | #include 4 | 5 | // 6 | // Big Four 7 | // 8 | 9 | String::String(): content(nullptr), size(0) { 10 | } 11 | 12 | String::String(const char *str): content(nullptr) { 13 | if (!str) { 14 | throw std::invalid_argument("Nullptr passed!"); 15 | } 16 | size_t size = strlen(str); 17 | copy(str, size); 18 | } 19 | 20 | String::~String() { 21 | free(); 22 | } 23 | 24 | String::String(const String& other) { 25 | copy(other.content, other.size); 26 | } 27 | 28 | const String& String::operator=(const String& other) { 29 | if (this == &other) { 30 | return *this; 31 | } 32 | free(); 33 | copy(other.content, other.size); 34 | return *this; 35 | } 36 | 37 | // 38 | // Functionality 39 | // 40 | 41 | size_t String::getSize() const { 42 | return size; 43 | } 44 | 45 | char* String::getContent() const { 46 | char *cloneContent = new char[size + 1]; 47 | strcpy(cloneContent, content); 48 | return cloneContent; 49 | } 50 | 51 | const char& String::at(size_t index) const { 52 | if (index < 0 || index > size) { 53 | throw std::out_of_range("Invalid position in string!"); 54 | } 55 | return content[index]; 56 | } 57 | 58 | char& String::at(size_t index){ 59 | if (index < 0 || index > size) { 60 | throw std::out_of_range("Invalid position in string!"); 61 | } 62 | return content[index]; 63 | } 64 | 65 | String& String::append(const String& other) { 66 | char *oldContent = content; 67 | size_t concatSize = size + other.size; 68 | copy(oldContent, concatSize); 69 | strcat(content, other.content); 70 | delete[] oldContent; 71 | return *this; 72 | } 73 | 74 | bool String::compare(const String& other) const { 75 | return !strcmp(content, other.content); 76 | } 77 | 78 | bool String::find(const String& substr) const { 79 | return strstr(content, substr.content); 80 | } 81 | 82 | String String::substr(const size_t pos, const size_t len) const { 83 | if (pos + len > size) { 84 | throw std::out_of_range("Position and length exceed the size of string!"); 85 | } 86 | 87 | char *temp = new char[len + 1]; 88 | strncpy(temp, content + pos, len); 89 | temp[len] = '\0'; 90 | 91 | String sub(temp); 92 | delete[] temp; 93 | return sub; 94 | } 95 | 96 | // 97 | // Help functions 98 | // 99 | 100 | void String::copy(const char* otherContent, size_t otherSize) { 101 | // NOTE: When using 'new' without nothrow, it throws bad_alloc exception. 102 | // We are responsible for catching this exception. 103 | if (!otherContent || !otherSize) { 104 | content = nullptr; 105 | size = otherSize; 106 | return; 107 | } 108 | content = new char[otherSize + 1]; 109 | strcpy(content, otherContent); 110 | size = otherSize; 111 | } 112 | 113 | void String::free() { 114 | delete[] content; 115 | content = nullptr; 116 | size = 0; 117 | 118 | } 119 | 120 | // 121 | // Friend funcitons 122 | // 123 | 124 | std::ostream& operator<<(std::ostream& out, const String& str) { 125 | return out << str.content; 126 | } 127 | -------------------------------------------------------------------------------- /Practice/Practice-03-04-Exercise--Magic-The-Gathering/Deck.cpp: -------------------------------------------------------------------------------- 1 | #include "Deck.h" 2 | using namespace std; 3 | 4 | static const char * deckFile = "deck.dat"; 5 | static const char * reportFile = "report.txt"; 6 | 7 | static const unsigned short maxArrSize = (1 << 15); 8 | 9 | bool saveDeck(const Deck &deck) 10 | { 11 | ofstream out(deckFile, ios::binary | ios::app); 12 | if (!out) 13 | { 14 | cerr << "Error opening file: " << deckFile << " for writing! \n"; 15 | return false; 16 | } 17 | out.write((const char *)&deck, sizeof(Deck)); 18 | out.close(); 19 | return true; 20 | } 21 | 22 | bool createDeck() 23 | { 24 | Deck newDeck; 25 | 26 | Player loadPlayer; 27 | readName(loadPlayer.name, maxPlayerName); 28 | if (!playerGetID(loadPlayer)) 29 | return false; 30 | else 31 | newDeck.playerID = loadPlayer.ID; 32 | 33 | Card loadCards[cardsInDeck]; 34 | for (int i = 0; i < cardsInDeck; i++) 35 | { 36 | readName(loadCards[i].name, maxCardName); 37 | if (!cardGetID(loadCards[i])) 38 | return false; 39 | else 40 | newDeck.cardID[i] = loadCards[i].ID; 41 | } 42 | newDeck.color = defineColor(loadCards); 43 | 44 | return saveDeck(newDeck); 45 | } 46 | 47 | Color defineColor(Card deck[]) 48 | { 49 | unsigned short colorArr[COLOR_COUNT] = { 0, }; 50 | for (int i = 0; i < cardsInDeck; i++) 51 | { 52 | if (deck[i].color > INVALID_COLOR && deck[i].color < COLOR_COUNT) 53 | colorArr[deck[i].color]++; 54 | } 55 | 56 | unsigned short index = 0; 57 | unsigned short max_color = colorArr[index]; 58 | for (size_t i = 1; i < COLOR_COUNT; i++) 59 | if (max_color < colorArr[i]) 60 | { 61 | max_color = colorArr[i]; 62 | index = i; 63 | } 64 | 65 | return static_cast(index); //don`t hate me! 66 | } 67 | 68 | size_t decksCnt() 69 | { 70 | ifstream in(deckFile, ios::binary); 71 | if (!in) 72 | return 0; 73 | 74 | in.seekg(0, std::ios::end); 75 | std::streamsize size = in.tellg(); 76 | in.close(); 77 | 78 | return (size / sizeof(Deck)); 79 | } 80 | 81 | unsigned short mostCommonCard(const Deck decks[], size_t cnt) 82 | { 83 | unsigned int arrID[maxArrSize] = { 0, }; 84 | for (size_t i = 0; i < cnt; i++) 85 | { 86 | countingSort(arrID, decks[i].cardID); 87 | } 88 | return getMostCommonID(arrID, maxArrSize); 89 | } 90 | 91 | void writeDeckInfo(const Deck &deck, ofstream &out) 92 | { 93 | Card cardCheck; 94 | cardCheck.ID = deck.cardID[0]; 95 | Player playerCheck; 96 | playerCheck.ID = deck.playerID; 97 | if (cardGetName(cardCheck) && playerGetName(playerCheck)) 98 | { 99 | out << "\t Owner: " << playerCheck.name 100 | << " Card: " << cardCheck.name 101 | << " Color: " << colorsName[cardCheck.color] 102 | << " \n"; 103 | } 104 | } 105 | 106 | void writeMostCommonCard(const Deck decks[], size_t cnt ,ofstream &out) 107 | { 108 | Card cardCheck; 109 | cardCheck.ID = mostCommonCard(decks,cnt); 110 | if (cardGetName(cardCheck)) 111 | { 112 | out << "\n \t Most common card: " << cardCheck.name 113 | << " Color: " << colorsName[cardCheck.color] 114 | << " \n "; 115 | } 116 | } 117 | 118 | void report() 119 | { 120 | Deck * allDecks = NULL; 121 | int cnt = decksCnt(); 122 | if (cnt != 0) 123 | { 124 | allDecks = new(nothrow)Deck[cnt]; 125 | if (!allDecks) 126 | { 127 | cerr << "Not enough memory!"; 128 | return; 129 | } 130 | } 131 | 132 | ifstream in(deckFile, ios::binary); 133 | if (!in) 134 | return; 135 | for (int i = 0; i < cnt; i++) 136 | in.read((char *)&allDecks[i], sizeof(Deck)); 137 | in.close(); 138 | 139 | ofstream out(reportFile, ios::trunc); 140 | for (size_t i = 0; i < COLOR_COUNT; i++) 141 | { 142 | out << colorsName[i] << " \n"; 143 | if (allDecks) 144 | { 145 | for (size_t j = 0; j < cnt; j++) 146 | if (allDecks[j].color == i) 147 | writeDeckInfo(allDecks[j], out); 148 | } 149 | 150 | } 151 | writeMostCommonCard(allDecks, cnt, out); 152 | out.close(); 153 | delete[] allDecks; 154 | } 155 | -------------------------------------------------------------------------------- /Practice/Practice-07-Serialization-Deserialization/Basket.cpp: -------------------------------------------------------------------------------- 1 | #include "Basket.h" 2 | #include "fstream" 3 | 4 | using namespace std; 5 | 6 | static const unsigned int initialCapacity = 1; 7 | 8 | 9 | Basket::Basket(const char * name) 10 | :basketName(NULL) 11 | , arr(NULL) 12 | , size(0) 13 | , capacity(initialCapacity) 14 | { 15 | setBasketName(name); 16 | initializeArr(arr,capacity); 17 | } 18 | 19 | Basket::~Basket() 20 | { 21 | clean(); 22 | } 23 | 24 | Basket::Basket(const Basket& input) 25 | :basketName(NULL) 26 | ,arr(NULL) 27 | { 28 | copy(input); 29 | } 30 | 31 | Basket& Basket::operator=(const Basket & input) 32 | { 33 | if (this != &input) 34 | { 35 | clean(); 36 | copy(input); 37 | } 38 | return *this; 39 | } 40 | 41 | void Basket::addEgg(const Egg & input) 42 | { 43 | if (eggExists(input.getEggName()) != -1) 44 | return; 45 | 46 | if (size >= capacity) 47 | resize(); 48 | 49 | arr[size] = input; 50 | size++; 51 | } 52 | 53 | void Basket::removeEgg(const char * eggName) 54 | { 55 | //we search for the index of the element(egg) we want to remove 56 | int removeIndex = eggExists(eggName); 57 | if (removeIndex == -1) 58 | return; 59 | 60 | //if we have only one egg in the basket 61 | if (size == 1) 62 | { 63 | size--; 64 | } 65 | else 66 | { 67 | size--; 68 | arr[removeIndex] = arr[size]; // we swap the egg we want to remove with the last one 69 | } 70 | 71 | if (capacity > (size * 2)) 72 | { 73 | resize(); 74 | } 75 | } 76 | 77 | void Basket::serialization() 78 | { 79 | char contentFile[1024] = "content_"; 80 | strcat(contentFile, basketName); 81 | strcat(contentFile, ".dat"); 82 | 83 | ofstream out(contentFile, ios::binary | ios::trunc); 84 | if (!out) 85 | { 86 | cerr << "Error opening file: " << contentFile << " for writing!\n "; 87 | return; 88 | } 89 | out.write((const char *)& size, sizeof(size)); 90 | for (size_t i = 0; i < size; i++) 91 | { 92 | arr[i].eggSerialization(out); 93 | } 94 | out.close(); 95 | } 96 | 97 | void Basket::deserialization() 98 | { 99 | char contentFile[1024] = "content_"; 100 | strcat(contentFile, basketName); 101 | strcat(contentFile, ".dat"); 102 | ifstream in(contentFile, ios::binary); 103 | if (!in) 104 | return; 105 | 106 | unsigned int eggsInFile = 0; 107 | in.read((char *)&eggsInFile, sizeof(eggsInFile)); 108 | 109 | Egg newEgg; 110 | for (size_t i = 0; i < eggsInFile; i++) 111 | { 112 | newEgg.eggDeserialization(in); 113 | addEgg(newEgg); 114 | } 115 | 116 | in.close(); 117 | } 118 | 119 | void Basket::reportBasket() 120 | { 121 | char contentFile[1024] = "report_"; 122 | strcat(contentFile, basketName); 123 | strcat(contentFile, ".txt"); 124 | 125 | ofstream out(contentFile, ios::trunc); 126 | for (size_t i = 0; i < size; i++) 127 | { 128 | arr[i].eggPrint(out); 129 | } 130 | out.close(); 131 | } 132 | 133 | void Basket::setBasketName(const char * name) 134 | { 135 | basketName = new char[strlen(name) +1]; 136 | strcpy(basketName, name); 137 | } 138 | 139 | void Basket::initializeArr(Egg *&arr, int &capacity) 140 | { 141 | arr = new Egg[capacity]; 142 | } 143 | 144 | void Basket::copy(const Basket & input) 145 | { 146 | setBasketName(input.basketName); 147 | capacity = input.capacity; 148 | initializeArr(arr, capacity); 149 | size = input.size; 150 | for (size_t i = 0; i < size; i++) 151 | { 152 | arr[i] = input.arr[i]; 153 | } 154 | } 155 | 156 | void Basket::resize() 157 | { 158 | Egg * newArr; 159 | int newCapacity; 160 | 161 | if (size >= capacity) 162 | { 163 | newCapacity = size * 2; 164 | } 165 | else 166 | { 167 | newCapacity = capacity / 2; 168 | } 169 | 170 | 171 | initializeArr(newArr, newCapacity); 172 | for (size_t i = 0; i < size; i++) 173 | { 174 | newArr[i] = arr[i]; 175 | } 176 | delete[] arr; 177 | arr = newArr; 178 | capacity = newCapacity; 179 | } 180 | 181 | void Basket::clean() 182 | { 183 | delete[] basketName; 184 | delete[] arr; 185 | } 186 | 187 | //if the egg exists, will return his index in the array 188 | int Basket::eggExists(const char * input) 189 | { 190 | for (size_t i = 0; i < size; i++) 191 | { 192 | if (stricmp(input,arr[i].getEggName()) == 0) 193 | { 194 | return i; 195 | } 196 | } 197 | 198 | return -1; 199 | } -------------------------------------------------------------------------------- /Practice/Practice-02-Files/Point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const char * pointsFileTxt = "points.txt"; 7 | const char * pointsFileBin = "points.dat"; 8 | 9 | 10 | struct Point 11 | { 12 | int x; 13 | int y; 14 | }; 15 | 16 | void setPoint(Point & point) 17 | { 18 | std::cin >> point.x; 19 | std::cin >> point.y; 20 | } 21 | 22 | void printPoint(const Point & point) 23 | { 24 | std::cout << "(" << point.x << "," 25 | << point.y << ")" << std::endl; 26 | } 27 | 28 | int delta(int X0, int X1) 29 | { 30 | return X1 - X0; 31 | } 32 | 33 | double distance(const Point & A, const Point & B) 34 | { 35 | // get the difference between each coordinate 36 | 37 | int deltaX = delta(A.x, B.x); 38 | int deltaY = delta(A.y, B.y); 39 | 40 | double result = sqrt(pow(deltaX, 2) + pow(deltaY, 2)); 41 | 42 | return result; 43 | } 44 | 45 | 46 | void createPointArr(Point *& arr, int size) 47 | { 48 | arr = new(std::nothrow) Point[size]; 49 | if (!arr) 50 | { 51 | std::cerr << "Not enough memory to complete the task!" << std::endl; 52 | return; 53 | } 54 | } 55 | 56 | void initializePointArr(Point * arr, int size) 57 | { 58 | for (size_t i = 0; i < size; i++) 59 | { 60 | setPoint(arr[i]); 61 | } 62 | } 63 | 64 | void printPointArr(Point * arr, int size) 65 | { 66 | for (size_t i = 0; i < size; i++) 67 | { 68 | std::cout << "point p" << i << " coordinates: "; 69 | printPoint(arr[i]); 70 | } 71 | } 72 | 73 | /***************************************************************/ 74 | 75 | unsigned int fileSize(const char * fileName) 76 | { 77 | ifstream in(fileName, ios::ate | ios::binary); // when is in append mode, put ptr is in the end of the file 78 | if (!in.is_open()) 79 | { 80 | cerr << "Error while opening file for writing!"; 81 | return 0; 82 | } 83 | 84 | streampos size = in.tellg(); 85 | in.close(); 86 | 87 | return size; 88 | } 89 | 90 | void writePoints(Point * arr, int size, const char * fileName) 91 | { 92 | ofstream write; 93 | write.open(fileName, ios::trunc); 94 | if (!write.good()) //if the stream is broken 95 | { 96 | cerr << "Error while opening file for writing!"; 97 | return; 98 | } 99 | 100 | write << size << '\n'; 101 | 102 | for (size_t i = 0; i < size; i++) 103 | { 104 | write << arr[i].x << ' ' << arr[i].y << ' '; 105 | } 106 | 107 | write.close(); 108 | } 109 | 110 | Point * readPoints(int& size, const char * fileName) 111 | { 112 | ifstream in; 113 | in.open(fileName); 114 | if (!in.good()) 115 | { 116 | cerr << "Error while opening file for reading!"; 117 | return nullptr; 118 | } 119 | 120 | in >> size; 121 | 122 | Point * arr; 123 | createPointArr(arr, size); 124 | 125 | for (size_t i = 0; i < size; i++) 126 | { 127 | in >> arr[i].x >> arr[i].y; 128 | } 129 | 130 | in.close(); 131 | 132 | return arr; 133 | } 134 | 135 | void serialization(Point * arr, int size, const char * fileName) 136 | { 137 | ofstream out(fileName, ios::binary | ios::trunc); 138 | if (!out.is_open()) 139 | { 140 | cerr << "Error while opening file for writing!"; 141 | return; 142 | } 143 | 144 | for (size_t i = 0; i < size; i++) 145 | { 146 | out.write((const char *)&arr[i], sizeof(Point)); 147 | } 148 | 149 | out.close(); 150 | } 151 | 152 | Point * deserialization(int & size, const char * fileName) 153 | { 154 | ifstream in(fileName, ios::binary); 155 | if (!in.is_open()) 156 | { 157 | cerr << "Error while opening file for reading!"; 158 | return nullptr; 159 | } 160 | 161 | //get the size of the array 162 | 163 | size = fileSize(fileName) / sizeof(Point); 164 | 165 | Point * arr; 166 | createPointArr(arr, size); 167 | 168 | for (size_t i = 0; i < size; i++) 169 | { 170 | in.read((char *)&arr[i], sizeof(Point)); 171 | } 172 | 173 | in.close(); 174 | return arr; 175 | } 176 | 177 | /*********************************************************/ 178 | 179 | int main() 180 | { 181 | Point* arr; 182 | int size = 2; 183 | createPointArr(arr, size); 184 | initializePointArr(arr, size); 185 | printPointArr(arr, size); 186 | std::cout << "The distance between the points is: " << distance(arr[0], arr[1]) << std::endl; 187 | 188 | //save data in text file 189 | writePoints(arr, size, pointsFileTxt); 190 | //read data from text file 191 | readPoints(size,pointsFileTxt); 192 | 193 | //save data in binary file 194 | serialization(arr, size, pointsFileBin); 195 | //read data from binary file 196 | deserialization(size, pointsFileBin); 197 | 198 | 199 | delete[] arr; 200 | return 0; 201 | } -------------------------------------------------------------------------------- /Practice/Practice-08-Exam/Exam Task - Solution/Fitness.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"Fitness.h" 4 | 5 | Fitness::Fitness() 6 | :name(nullptr) 7 | , clients(nullptr) 8 | , currentSize(0) 9 | , capacity(2) 10 | { 11 | clients = new Client[capacity]; 12 | } 13 | 14 | Fitness::Fitness(const char* name) 15 | :name(nullptr) 16 | , clients(nullptr) 17 | , currentSize(0) 18 | , capacity(2) 19 | { 20 | 21 | this->name = new char[strlen(name) + 1]; 22 | strcpy(this->name, name); 23 | clients = new Client[capacity]; 24 | } 25 | 26 | Fitness::Fitness(const Fitness& fitness) { 27 | 28 | copy(fitness); 29 | } 30 | 31 | Fitness& Fitness::operator=(const Fitness& fitness) { 32 | 33 | if (this != &fitness) { 34 | 35 | clear(); 36 | copy(fitness); 37 | } 38 | 39 | return *this; 40 | } 41 | 42 | Fitness::~Fitness() { 43 | 44 | clear(); 45 | } 46 | 47 | //OTHER PUBLIC: 48 | 49 | bool Fitness::addClient(const Client& client) 50 | { 51 | //Check if exists Client with same name, 52 | if (getClientNumber(client.getName()) >= 0) { 53 | 54 | std::cerr << "Client already exists!\n"; 55 | return false; 56 | } 57 | 58 | if (currentSize == capacity) { 59 | /* Try to allocate twice bigger block of memory for Clients 60 | if 'new' throws exception we would catch it and we will know that 61 | there is not enough memory and stop adding a client and send property message to user 62 | */ 63 | try { 64 | resize(); 65 | } 66 | catch (const std::exception& ex) 67 | { 68 | std::cerr << "Memory couldn't be allocated!\n"; 69 | return false; 70 | } 71 | } 72 | 73 | 74 | clients[currentSize++] = client; 75 | return true; 76 | } 77 | 78 | void Fitness::removeClient(const char* clientName) { 79 | 80 | int clientNum = getClientNumber(clientName); 81 | 82 | if (clientNum < 0) { 83 | 84 | std::cerr << "Cannot remove inexistent client!\n"; 85 | return; 86 | } 87 | 88 | currentSize--; 89 | clients[clientNum] = clients[currentSize]; 90 | } 91 | 92 | void Fitness::changeWeight(const char* name,unsigned int kg) { 93 | 94 | int clientNum = getClientNumber(name); 95 | 96 | clients[clientNum].changeWeight(kg); 97 | } 98 | 99 | void Fitness::serializeFitness() const { 100 | 101 | char filename[1024]; 102 | strcpy(filename, name); 103 | strcat(filename, ".dat"); 104 | 105 | std::ofstream out(filename, std::ios::trunc | std::ios::binary); 106 | 107 | if (!out) { 108 | 109 | std::cerr << "serialization error\n"; 110 | return; 111 | } 112 | 113 | out.write((const char*)&capacity, sizeof(capacity)); 114 | out.write((const char*)¤tSize, sizeof(currentSize)); 115 | 116 | unsigned nameLen = strlen(name) + 1; 117 | 118 | out.write((const char*)&nameLen, sizeof(nameLen)); 119 | out.write(name, nameLen); 120 | 121 | for (size_t i = 0; i < currentSize; ++i) 122 | clients[i].serializeClient(out); 123 | 124 | out.close(); 125 | } 126 | 127 | void Fitness::deserializeFitness(const char* fileName) { 128 | std::ifstream in(fileName, std::ios::in | std::ios::binary); 129 | 130 | if (!in) { 131 | 132 | std::cerr << "deserialization error\n"; 133 | return; 134 | } 135 | 136 | in.read((char*)&capacity, sizeof(capacity)); 137 | in.read((char*)¤tSize, sizeof(currentSize)); 138 | 139 | unsigned nameLen; 140 | in.read((char*)&nameLen, sizeof(nameLen)); 141 | 142 | name = new char[nameLen]; 143 | in.read(name, nameLen); 144 | 145 | clients = new Client[capacity]; 146 | 147 | for (size_t i = 0; i < currentSize; ++i) 148 | clients[i].deserializeClient(in); 149 | 150 | in.close(); 151 | } 152 | 153 | void Fitness::reportFitness() const { 154 | 155 | char filename[1024]; 156 | strcpy(filename, name); 157 | strcat(filename, ".txt"); 158 | 159 | std::ofstream out(filename, std::ios::trunc); 160 | 161 | if (!out) { 162 | 163 | std::cerr << "report error\n"; 164 | return; 165 | } 166 | 167 | for (size_t i = 0; i < currentSize; ++i) 168 | clients[i].reportClient(out); 169 | 170 | out.close(); 171 | } 172 | 173 | void Fitness::sort() 174 | { 175 | for (size_t i = 1; i < currentSize; i++) 176 | { 177 | Client temp = clients[i]; 178 | size_t index = i; 179 | while (index > 0 && temp.getAge() < clients[index - 1].getAge()) 180 | { 181 | clients[index] = clients[index - 1]; 182 | index--; 183 | } 184 | clients[index] = temp; 185 | } 186 | } 187 | 188 | void Fitness::print() const { 189 | 190 | std::cout << name << " cap: " << capacity << " szie: " << currentSize << ": \n"; 191 | for (size_t i = 0; i < currentSize; ++i) 192 | clients[i].print(); 193 | } 194 | //PRIVATE: 195 | 196 | int Fitness::getClientNumber(const char* clientName) { 197 | 198 | if (currentSize == 0) 199 | return -1; 200 | 201 | int counter = 0; 202 | if (currentSize > 0) 203 | while (counter < currentSize && _stricmp(clients[counter].getName(), clientName)) 204 | ++counter; 205 | 206 | if (counter == currentSize) 207 | counter = -1; 208 | 209 | return counter; 210 | } 211 | 212 | void Fitness::resize() { 213 | 214 | Client* temp = new Client[capacity * 2]; 215 | for (size_t i = 0; i < capacity; ++i) 216 | temp[i] = clients[i]; 217 | 218 | delete[] clients; 219 | clients = temp; 220 | temp = nullptr; 221 | 222 | capacity *= 2; 223 | } 224 | 225 | void Fitness::copy(const Fitness& fitness) { 226 | 227 | name = new char[strlen(fitness.name) + 1]; 228 | strcpy(name, fitness.name); 229 | 230 | capacity = fitness.capacity; 231 | currentSize = fitness.currentSize; 232 | 233 | clients = new Client[capacity]; 234 | for (size_t i = 0; i < currentSize; ++i) 235 | clients[i] = fitness.clients[i]; 236 | } 237 | 238 | void Fitness::clear() { 239 | 240 | delete[] name; 241 | delete[] clients; 242 | } -------------------------------------------------------------------------------- /Seminar/simple-files.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a program that uses command line arguments to interact with text files. 3 | * 4 | * Its arguments are: 5 | * - the filename of the file with which it operates 6 | * - command, that dictates what will it do with the file. Commands: write, read, append 7 | * - text, which is used only if the commands are 'write' or 'append', and is the new content, that is written to the file. 8 | * 9 | * If the command is 'read' it prints the content of the file to stdout. 10 | * If it is 'write', then the current content of the file is discated and the new content(given as argument) is placed there. 11 | * If it is 'append', the new content is added at the end of the file, without deleting old content. 12 | * 13 | * NOTE: If you spot a mistake, submit a pull request with the fix to the repo. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | const std::size_t SIZE_BUFF = 256; 21 | 22 | enum Command { 23 | write, 24 | append, 25 | read 26 | }; 27 | 28 | bool writeInFile(const char *filename, const char * content) { 29 | std::ofstream file; 30 | file.open (filename, std::ios::out | std::ios::trunc); 31 | 32 | // Checking if file has opened 33 | if ( !file.good() ) { 34 | std::cerr << "Couldn't open file: " << filename << std::endl; 35 | return false; 36 | } 37 | 38 | file << content; 39 | 40 | // Checking if writing failed 41 | if ( file.bad() ) { 42 | std::cerr << "Failed when writing in file: " << filename << std::endl; 43 | return false; 44 | } 45 | 46 | file.close(); 47 | return true; 48 | } 49 | 50 | bool appendToFile(const char *filename, const char *newContent) { 51 | std::ofstream file; 52 | file.open (filename, std::ios::out | std::ios::app); 53 | if (! file.good() ) { 54 | std::cerr << "Couldn't open file: " << filename << std::endl; 55 | return false; 56 | } 57 | 58 | file << newContent; 59 | if ( file.bad() ) { 60 | std::cerr << "Failed when writing in file: " << filename << std::endl; 61 | return false; 62 | } 63 | 64 | file.close(); 65 | return true; 66 | } 67 | 68 | bool printFromFile(const char* filename) { 69 | char line[SIZE_BUFF]; 70 | std::ifstream file (filename); 71 | 72 | if (! file.good() ) { 73 | std::cerr << "Couldn't open file: " << filename << std::endl; 74 | return false; 75 | } 76 | 77 | while ( file.getline(line, SIZE_BUFF) ) { 78 | // Checking if reading failed 79 | if ( file.bad() ) { 80 | std::cerr << "Failed reading from file: " << filename << std::endl; 81 | return false; 82 | } 83 | std::cout << line << std::endl; 84 | } 85 | 86 | file.close(); 87 | return true; 88 | } 89 | 90 | bool readFromFile(const char *filename, char *&content) { 91 | std::ifstream file(filename); 92 | if (! file.good() ) { 93 | std::cerr << "Couldn't open file: " << filename << std::endl; 94 | return false; 95 | } 96 | 97 | file.seekg(0, std::ios::end); 98 | std::size_t sizeFile = file.tellg(); 99 | file.seekg(0, std::ios::beg); 100 | 101 | char *contentTemp = new (std::nothrow) char[sizeFile]; 102 | if (! contentTemp ) { 103 | file.close(); 104 | std::cerr << "Failed to allocate space for file content: " << filename << std::endl; 105 | return false; 106 | } 107 | 108 | file.read(contentTemp, sizeFile); 109 | if ( file.bad() ) { 110 | std::cerr << "Failed reading from file: " << filename << std::endl; 111 | return false; 112 | } 113 | file.close(); 114 | 115 | // Assigning when we are sure all oprations don't fail 116 | content = contentTemp; 117 | return true; 118 | } 119 | 120 | bool fileExists(const char *filename) { 121 | std::ifstream f(filename); 122 | bool exists = f.good(); 123 | f.close(); 124 | return exists; 125 | } 126 | 127 | int main(int argc, char *argv[]) { 128 | if ( argc < 3 || argc > 4 ) { 129 | std::cout << 130 | "\nUsage: " << argv[0] << " filename command [content]\n" << 131 | "\t- filename = File with which to operate. Example: 'D:\\\\myfolder\\\\myfile.txt'\n" << 132 | "\t- command = Commands are: 'write', 'read' and 'append'\n" << 133 | "\t- content = New content for the file. Optional argument for 'write' and 'append'\n" << std::endl; 134 | return 1; 135 | } 136 | 137 | char 138 | *filename = argv[1], 139 | *command = argv[2], 140 | *content = nullptr; 141 | 142 | // Only if third command-line argument exists 143 | if ( argv[3] ) { 144 | content = argv[3]; 145 | } 146 | 147 | Command filecmd; 148 | if (! strcmp(command, "write") ) { 149 | if (!content) { 150 | std::cerr << "Needs content argument when writing!" << std::endl; 151 | return 1; 152 | } 153 | 154 | filecmd = Command::write; 155 | } else if (! strcmp(command, "append") ) { 156 | if (!content) { 157 | std::cerr << "Needs content argument when appending!" << std::endl; 158 | return 1; 159 | } 160 | 161 | filecmd = Command::append; 162 | } else if (! strcmp(command, "read") ) { 163 | if (content) { 164 | std::cout << "No need for content argument when reading!" << std::endl; 165 | } 166 | 167 | filecmd = Command::read; 168 | } else { 169 | std::cerr << "Wrong command! Commands are: 'write', 'read' and 'append'" << std::endl; 170 | return 1; 171 | } 172 | 173 | if (! fileExists(filename) && filecmd == Command::read) { 174 | std::cerr << "File '" << filename << "' DOESN'T exists!" << std::endl; 175 | return 1; 176 | } 177 | 178 | bool operationSuccess = false; 179 | switch ( filecmd ) { 180 | case Command::write: 181 | operationSuccess = writeInFile(filename, content); 182 | std::cout << "Written to file!" << std::endl; 183 | break; 184 | case Command::append: 185 | operationSuccess = appendToFile(filename, content); 186 | std::cout << "Appended to file!" << std::endl; 187 | break; 188 | case Command::read: 189 | std::cout << "File content:" << std::endl; 190 | operationSuccess = printFromFile(filename); 191 | break; 192 | } 193 | 194 | if ( operationSuccess ) { 195 | return 0; 196 | } else { 197 | return 1; 198 | } 199 | } 200 | --------------------------------------------------------------------------------