From 5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 10 May 2022 20:06:53 +0300 Subject: sem 11 --- Sem. 09/MyString (with move)/MyString.cpp | 143 +++++++++++++++++++++ Sem. 09/MyString (with move)/MyString.h | 44 +++++++ Sem. 10/Examples/Optional/Optional.hpp | 74 +++++++++++ Sem. 10/Examples/Optional/Source2.cpp | 19 +++ Sem. 10/Examples/Pair/Pair.hpp | 43 +++++++ Sem. 10/Examples/Pair/Source2.cpp | 14 ++ Sem. 11/Examples/Person/Person.cpp | 76 +++++++++++ Sem. 11/Examples/Person/Person.h | 27 ++++ Sem. 11/Examples/Source2.cpp | 10 ++ Sem. 11/Examples/Student/Student.cpp | 34 +++++ Sem. 11/Examples/Student/Student.h | 25 ++++ ...\320\260\320\261\320\276\321\202\320\260_1.pdf" | Bin 0 -> 811843 bytes Utills/k_1_SI/task1.cpp | 138 ++++++++++++++++++++ Utills/k_1_SI/task2_read.cpp | 55 ++++++++ Utills/k_1_SI/task2_write.cpp | 84 ++++++++++++ .../Source.cpp" | 12 ++ .../Task1/LogFile.cpp" | 92 +++++++++++++ .../Task1/LogFile.h" | 24 ++++ .../Task1/Timestamp.cpp" | 5 + .../Task1/Timestamp.h" | 9 ++ .../User.cpp" | 97 ++++++++++++++ .../User.h" | 26 ++++ .../chatSystem.cpp" | 80 ++++++++++++ .../chatSystem.h" | 31 +++++ ...5\320\275\320\270\320\265 \320\241\320\230.pdf" | Bin 0 -> 69150 bytes .../demo.cpp" | 89 +++++++++++++ ...\320\260\320\261\320\276\321\202\320\260_1.pdf" | Bin 811843 -> 0 bytes k_1_SI/task1.cpp | 138 -------------------- k_1_SI/task2_read.cpp | 55 -------- k_1_SI/task2_write.cpp | 84 ------------ .../Source.cpp" | 12 -- .../Task1/LogFile.cpp" | 92 ------------- .../Task1/LogFile.h" | 24 ---- .../Task1/Timestamp.cpp" | 5 - .../Task1/Timestamp.h" | 9 -- .../User.cpp" | 97 -------------- .../User.h" | 26 ---- .../chatSystem.cpp" | 80 ------------ .../chatSystem.h" | 31 ----- ...5\320\275\320\270\320\265 \320\241\320\230.pdf" | Bin 69150 -> 0 bytes .../demo.cpp" | 89 ------------- 41 files changed, 1251 insertions(+), 742 deletions(-) create mode 100644 Sem. 09/MyString (with move)/MyString.cpp create mode 100644 Sem. 09/MyString (with move)/MyString.h create mode 100644 Sem. 10/Examples/Optional/Optional.hpp create mode 100644 Sem. 10/Examples/Optional/Source2.cpp create mode 100644 Sem. 10/Examples/Pair/Pair.hpp create mode 100644 Sem. 10/Examples/Pair/Source2.cpp create mode 100644 Sem. 11/Examples/Person/Person.cpp create mode 100644 Sem. 11/Examples/Person/Person.h create mode 100644 Sem. 11/Examples/Source2.cpp create mode 100644 Sem. 11/Examples/Student/Student.cpp create mode 100644 Sem. 11/Examples/Student/Student.h create mode 100644 "Utills/homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" create mode 100644 Utills/k_1_SI/task1.cpp create mode 100644 Utills/k_1_SI/task2_read.cpp create mode 100644 Utills/k_1_SI/task2_write.cpp create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" create mode 100644 "Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" delete mode 100644 "homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" delete mode 100644 k_1_SI/task1.cpp delete mode 100644 k_1_SI/task2_read.cpp delete mode 100644 k_1_SI/task2_write.cpp delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" delete mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" diff --git a/Sem. 09/MyString (with move)/MyString.cpp b/Sem. 09/MyString (with move)/MyString.cpp new file mode 100644 index 0000000..a037d3d --- /dev/null +++ b/Sem. 09/MyString (with move)/MyString.cpp @@ -0,0 +1,143 @@ +#include "MyString.h" +#include +#include + +void MyString::copyFrom(const MyString& other) +{ + str = new char[strlen(other.str) + 1]; + strcpy(str, other.str); + size = other.size; +} +void MyString::free() +{ + delete[] str; +} + +MyString::MyString() +{ + str = new char[1]; + str[0] = '\0'; + size = 0; +} + +MyString::MyString(const char* data) +{ + if(data == nullptr) + { + str = new char[1]; + str[0] = '\0'; + size = 0; + } + else + { + size = strlen(data); + str = new char[size + 1]; + strcpy(str, data); + } +} + +MyString::MyString(const MyString& other) +{ + copyFrom(other); +} +MyString& MyString::operator=(const MyString& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +MyString::~MyString() +{ + free(); +} + + +size_t MyString::getSize() const +{ + return size; +} +void MyString::concat(const MyString& other) +{ + char* temp = new char[getSize() + other.getSize() + 1]; + strcpy(temp, str); + strcat(temp, other.str); + + delete[] str; + str = temp; + size = size + other.getSize(); +} + +const char* MyString::c_str() const +{ + return str; +} + +MyString& MyString::operator+=(const MyString& other) +{ + concat(other); + return *this; +} + +MyString operator+(const MyString& lhs, const MyString& rhs) +{ + MyString copyOfLeft(lhs); + copyOfLeft += rhs; + return copyOfLeft; +} + +std::ostream& operator<<(std::ostream& stream, const MyString& str) +{ + stream << str.str; + return stream; +} + +std::istream& operator>>(std::istream& stream, MyString& str) +{ + delete[] str.str; + char buff[1024]; + stream >> buff; + + str.size = strlen(buff); + str.str = new char[str.size + 1]; + strcpy(str.str, buff); + + return stream; +} + +bool operator==(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) == 0; +} +bool operator<=(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) <= 0; + +} +bool operator<(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) < 0; + +} + +MyString::MyString(MyString&& otherString) +{ + str = otherString.str; + size = otherString.size; + otherString.str = nullptr; + std::cout << "move constr" << std::endl; +} + +MyString& MyString::operator=(MyString&& otherString) +{ + if (this != &otherString) + { + free(); + str = otherString.str; + otherString.str = nullptr; + size = otherString.size; + } + return *this; +} diff --git a/Sem. 09/MyString (with move)/MyString.h b/Sem. 09/MyString (with move)/MyString.h new file mode 100644 index 0000000..b37733a --- /dev/null +++ b/Sem. 09/MyString (with move)/MyString.h @@ -0,0 +1,44 @@ +#pragma once + +#include +class MyString +{ + char* str; + size_t size; + + void copyFrom(const MyString& other); + void free(); + +public: + MyString(); + MyString(const char*); + + MyString(const MyString&); + + MyString(MyString&&); //move constructor that accepts rvalue; + + MyString& operator=(const MyString& other); + MyString& operator=(MyString&& other); //move op= that accepts rvalue; + + ~MyString(); + + size_t getSize() const; + void concat(const MyString& other); + + const char* c_str() const; + + MyString& operator+=(const MyString& other); + + friend std::ostream& operator<<(std::ostream& stream, const MyString& str); + friend std::istream& operator>>(std::istream& stream, MyString& str); + +}; + +MyString operator+(const MyString& lhs, const MyString& rhs); + +bool operator==(const MyString& lhs, const MyString& rhs); +bool operator<=(const MyString& lhs, const MyString& rhs); +bool operator<(const MyString& lhs, const MyString& rhs); + + +//MyString name = "sdfsdf" \ No newline at end of file diff --git a/Sem. 10/Examples/Optional/Optional.hpp b/Sem. 10/Examples/Optional/Optional.hpp new file mode 100644 index 0000000..9e3ab0f --- /dev/null +++ b/Sem. 10/Examples/Optional/Optional.hpp @@ -0,0 +1,74 @@ +#pragma once +#include +#include +template +class Optional +{ + T* data; //DYN MEM + + void copyFrom(const Optional& other); + void free(); +public: + Optional(); + Optional(const T& obj); + + bool containsData() const; + const T& getData() const; + void setData(const T& el); + void clear(); + + +}; + +template +Optional::Optional() : data(nullptr) +{} + +template +Optional::Optional(const T& obj) : data(nullptr) +{ + setData(obj); +} + +template +bool Optional::containsData() const +{ + return data != nullptr; +} + +template +const T& Optional::getData() const +{ + if (!containsData()) + throw std::logic_error("No data in optional"); + return *data; +} + +template +void Optional::setData(const T& el) +{ + delete data; + data = new T(el);// copy const; +} + +template +void Optional::copyFrom(const Optional& other) +{ + if (other.containsData()) + data = new T(*other.data); + else + data = nullptr; +} + +template +void Optional::free() +{ + delete data; +} + +template +void Optional::clear() +{ + free(); + data = nullptr; +} diff --git a/Sem. 10/Examples/Optional/Source2.cpp b/Sem. 10/Examples/Optional/Source2.cpp new file mode 100644 index 0000000..7c3e21b --- /dev/null +++ b/Sem. 10/Examples/Optional/Source2.cpp @@ -0,0 +1,19 @@ +#include +#include "Optional.hpp" + + + +int main() +{ + Optional d; //no value + + d.setData(3); + + d.getData(); + + d.clear(); + + + std::cout << d.containsData(); + +} diff --git a/Sem. 10/Examples/Pair/Pair.hpp b/Sem. 10/Examples/Pair/Pair.hpp new file mode 100644 index 0000000..adf16b7 --- /dev/null +++ b/Sem. 10/Examples/Pair/Pair.hpp @@ -0,0 +1,43 @@ +#pragma once + +template +class Pair +{ + T first; + D second; +public: + Pair() = default; + Pair(const T& first, const D& second); + const T& getFirst() const; + const D& getSecond() const; + + void setFirst(const T& newValue); + void setSecond(const D& newValue); +}; + +template +Pair::Pair(const T& first, const D& second) : first(first), second(second) +{} + +template +const T& Pair::getFirst() const +{ + return first; +} +template +const D& Pair::getSecond() const +{ + return second; +} + +template +void Pair::setFirst(const T& newValue) +{ + first = newValue; +} + +template +void Pair::setSecond(const D& newValue) +{ + second = newValue; +} \ No newline at end of file diff --git a/Sem. 10/Examples/Pair/Source2.cpp b/Sem. 10/Examples/Pair/Source2.cpp new file mode 100644 index 0000000..39229da --- /dev/null +++ b/Sem. 10/Examples/Pair/Source2.cpp @@ -0,0 +1,14 @@ +#include +#include "Pair.hpp" + + + +int main() +{ + Pair p(3, 'd'); + + std::cout << p.getSecond(); + + p.setSecond('a'); + +} diff --git a/Sem. 11/Examples/Person/Person.cpp b/Sem. 11/Examples/Person/Person.cpp new file mode 100644 index 0000000..a0cc714 --- /dev/null +++ b/Sem. 11/Examples/Person/Person.cpp @@ -0,0 +1,76 @@ +#pragma once +#include "Person.h" +#include +#pragma warning (disable:4996) + + + +void Person::copyFrom(const Person& other) +{ + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); + age = other.age; +} + +void Person::free() +{ + delete[] name; +} + +Person::Person(const char* name, int age) +{ + setName(name); + setAge(age); +} + +Person::Person(const Person& other) +{ + copyFrom(other); +} + +Person& Person::operator=(const Person& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +const char* Person::getName() const +{ + return name; +} + +int Person::getAge() const +{ + return age; +} + +void Person::setName(const char* name) +{ + if (name == nullptr || this->name == name) + return; + + delete[] this->name; + size_t nameLen = strlen(name); + this->name = new char[nameLen + 1]; + strcpy(this->name, name); +} + +void Person::setAge(int age) +{ + this->age = age; +} + + +Person::~Person() +{ + free(); +} + +void Person::print() const +{ + std::cout << name << " " << age << std::endl; +} + diff --git a/Sem. 11/Examples/Person/Person.h b/Sem. 11/Examples/Person/Person.h new file mode 100644 index 0000000..826b671 --- /dev/null +++ b/Sem. 11/Examples/Person/Person.h @@ -0,0 +1,27 @@ +#pragma once +#include +class Person +{ + char* name = nullptr; + int age = 0; + + void copyFrom(const Person& other); + void free(); + +public: + Person() = default; + Person(const char* name, int age); + + Person(const Person& other); + Person& operator=(const Person& other); + + const char* getName() const; + int getAge() const; + + ~Person(); + void print() const; + +protected: + void setName(const char* name); + void setAge(int age); +}; diff --git a/Sem. 11/Examples/Source2.cpp b/Sem. 11/Examples/Source2.cpp new file mode 100644 index 0000000..b6196dd --- /dev/null +++ b/Sem. 11/Examples/Source2.cpp @@ -0,0 +1,10 @@ +#include +#include "Student/Student.h" + + + +int main() +{ + Student s2("Fsdf", 34,4); + +} diff --git a/Sem. 11/Examples/Student/Student.cpp b/Sem. 11/Examples/Student/Student.cpp new file mode 100644 index 0000000..8b5b04c --- /dev/null +++ b/Sem. 11/Examples/Student/Student.cpp @@ -0,0 +1,34 @@ +#include "Student.h" + +Student::Student(const char* name, int age, size_t fn) : Person(name, age) +{ + setFn(fn); +} + +size_t Student::getFn() const +{ + return fn; +} + +void Student::setFn(size_t fn) +{ + + if (fn < 100) + fn = 100; + this->fn = fn; +} + +// The compiler will make: + +//Student(const Student& other) : Person(other), fn(other.fn), grade(other.grade) OK!!! +//{} + +//Student& operator=(const Student& other) OK!!!! +//{ +// if (this != &other) +// { +// Person::operator=(other); //handles the deletion and copying of the base class +// fn = other.fn; +// } +// return *this; +//} \ No newline at end of file diff --git a/Sem. 11/Examples/Student/Student.h b/Sem. 11/Examples/Student/Student.h new file mode 100644 index 0000000..af99e30 --- /dev/null +++ b/Sem. 11/Examples/Student/Student.h @@ -0,0 +1,25 @@ +#include +#include "../Person/Person.h" + +class Student : public Person +{ + size_t fn = 0; + size_t grade = 2; +public: + Student() = default; + Student(const char* name, int age, size_t fn); + + size_t getFn() const; + void setFn(size_t fn); + + + // No need for big 4, since there is NO dyn. memory in Student + // The compiler will generate valid: + + //Student(const Student& other); + //Student& operator=(const Student& other); //no need + // ~Student() + + //See on the .cpp file how the compiler creates them + +}; diff --git "a/Utills/homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" "b/Utills/homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" new file mode 100644 index 0000000..a7a27ba Binary files /dev/null and "b/Utills/homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" differ diff --git a/Utills/k_1_SI/task1.cpp b/Utills/k_1_SI/task1.cpp new file mode 100644 index 0000000..77ad5f5 --- /dev/null +++ b/Utills/k_1_SI/task1.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + +using namespace std; + +struct Point +{ + int x; + int y; + Point() : Point(0,0){} + + Point(int x, int y) + { + this->x = x; + this->y = y; + } + + double getDistance(const Point& other) const + { + int dx = x - other.x; + int dy = y - other.y; + return sqrt(dx * dx + dy * dy); + } +}; + +void print(const Point& p) +{ + std::cout << "[ x: " << p.x << ", y: " << p.y <<" ]"; +} + +struct Rectangle +{ +private: + Point points[4]; + +public: + Rectangle() = default; //the same as Rectangle() {} + + void setPoint(size_t index, int x, int y) + { + if(index > 3) + return; + + points[index].x = x; + points[index].y = y; + + } + const Point& getPoint(size_t index) const + { + if(index > 3) + return {}; //some Error. This is a refference to a temporary memory, so a better solution would be to throw an exception. + return points[index]; + } + double getPer() const + { + return 2 * points[0].getDistance(points[1]) + 2 * points[1].getDistance(points[2]); + } +}; + +void print(const Rectangle& rect) +{ + for(int i = 0; i < 4; i++) + { + print(rect.getPoint(i)); + } +} + +void swap(Rectangle& tr1, Rectangle& tr2) +{ + Rectangle temp = tr1; + tr1 = tr2; + tr2 = temp; +} + +void selectionSort(Rectangle* triangles, double* perimeters, unsigned size) +{ + for (unsigned i = 0; i < size - 1; i++) + { + //findMin + int minElementIndex = i; + for (unsigned j = i + 1; j < size; j++) + { + if (perimeters[j] < perimeters[minElementIndex]) + minElementIndex = j; + } + + if (minElementIndex != i) + { + swap(perimeters[i], perimeters[minElementIndex]); + swap(triangles[i], triangles[minElementIndex]); + } + } +} + +int main() +{ + ifstream file("rects.txt"); + + if(!file.is_open()) + { + std::cout << "Error while opening the file!"; + return -1; + } + + int n; + file >> n; + + Rectangle* rects = new Rectangle[n]; + double* perimeters = new double[n]; //this should be size_t, but we are using a double returning function for getDist. + + for(size_t i = 0; i < n; i++) + { + int x1, y1, x3, y3; + file >> x1 >> y1 >> x3 >> y3; + + rects[i].setPoint(0, x1, y1); + rects[i].setPoint(1, x1, y3); + rects[i].setPoint(2, x3, y3); + rects[i].setPoint(3, x3, y1); + + perimeters[i] = rects[i].getPer(); + + } + + selectionSort(rects, perimeters, n); //selection sort, because we want min number of copies (or swaps) + + for(size_t i = 0; i < n; i++) + { + print(rects[i]); + std::cout <<", Per: " << perimeters[i] << std::endl; + } + + + delete[] rects, perimeters; + return 0; +} + diff --git a/Utills/k_1_SI/task2_read.cpp b/Utills/k_1_SI/task2_read.cpp new file mode 100644 index 0000000..e6a8312 --- /dev/null +++ b/Utills/k_1_SI/task2_read.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +using namespace std; + +const size_t MAX_NAME_SIZE = 22; +struct Student +{ + //gender should be BEFORE fn so the struct would be 28 bytes. + char name[MAX_NAME_SIZE + 1]; + bool isMale; + size_t fn; +}; + + +size_t getFileSize(ifstream& f) +{ + size_t currentPos = f.tellg(); + f.seekg(0, ios::end); + size_t size = f.tellg(); + + f.seekg(currentPos); + return size; +} + +void readFromFile(Student*& ptr, size_t& studentsCount, ifstream& f) +{ + size_t sizeOfFile = getFileSize(f); + studentsCount = sizeOfFile / sizeof(Student); + ptr = new Student[studentsCount]; + f.read((char*)ptr, sizeOfFile); +} + + +int main() +{ + Student* arr; + size_t count; + ifstream file("students.dat"); + + if(!file.is_open()) + { + std::cout << "Error while opening the file!" << std::endl; + return -1; + } + + readFromFile(arr, count, file); + + for (int i = 0; i < count; i++) + std::cout << "Name: " << arr[i].name << ", fn: " << arr[i].fn << ", is male: " << arr[i].isMale << std::endl; + + delete[] arr; + + return 0; +} diff --git a/Utills/k_1_SI/task2_write.cpp b/Utills/k_1_SI/task2_write.cpp new file mode 100644 index 0000000..f320557 --- /dev/null +++ b/Utills/k_1_SI/task2_write.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +using namespace std; + +const size_t MAX_NAME_SIZE = 22; +struct Student +{ + //gender should be BEFORE fn so the struct would be 28 bytes. + char name[MAX_NAME_SIZE + 1]; + bool isMale; + size_t fn; +}; + +bool isLower(char ch) +{ + return ch >= 'a' && ch <= 'z'; +} + +bool areAllLowerCase(const char* str) +{ + while(*str) + { + if(!isLower(*str)) + return false; + str++; + } + return true; +} + +bool isUpper(char ch) +{ + return ch >= 'A' && ch <= 'Z'; +} + +bool isNameValid(const char* name) +{ + size_t len = strlen(name); + if(len == 0 || len > MAX_NAME_SIZE) + return false; + return isUpper(*name) && areAllLowerCase(name + 1); +} + +void initStudent(Student& st, const char* name, size_t fn, bool isMale) +{ + if(!isNameValid(name)) + strcpy(st.name, "Unknown"); + else + strcpy(st.name, name); + + st.isMale = isMale; + st.fn = fn; +} + +void saveToFile(const Student* students, size_t count, ofstream& file) +{ + file.write((const char*)students, count * sizeof(Student)); +} + + +int main() +{ + const size_t studentsCount = 4; + Student* arr = new Student[studentsCount]; + initStudent(arr[0], "Ivan", 1, true); + initStudent(arr[1], "Petur", 2, true); + initStudent(arr[2], "Alex", 3, true); + initStudent(arr[3], "Katerina", 4, false); + + ofstream file("students.dat", ios::binary); + + if(!file.is_open()) + { + std::cout << "Error while opening the file!" << std::endl; + delete[] arr; //! + return -1; + } + + saveToFile(arr, studentsCount, file); + + delete[] arr; + + return 0; +} diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" new file mode 100644 index 0000000..17e6f4e --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" @@ -0,0 +1,12 @@ +#include +#include"chatSystem.h" + +int main() { + chatSystem data; + + data.addUser({ "Ivan", "1234", "iv@abv.bg" }); + data.addUser({ "Qvor", "4321", "qv@abv.bg" }); + + data.sendMessage("last example", "Ivan", "Qvor"); + data.viewMessages("Qvor", "Ivan"); +} \ No newline at end of file diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" new file mode 100644 index 0000000..7be397b --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" @@ -0,0 +1,92 @@ +#include "LogFile.h" + +void LogFile::readTime(std::ifstream& inFile, Timestamp& obj) const { + char isLogin; + + inFile >> isLogin; + + obj.isLogin = (isLogin == '+'); + + inFile >> obj.hour >> obj.minutes >> obj.seconds; +} + + +LogFile::LogFile() { + size = 0; +} + +LogFile::LogFile(const char* filePath) { + std::ifstream inFile(filePath); + + if (!inFile.is_open()) { + size = 0; + return; + } + + inFile >> size; + + for (int i = 0; i < size; i++) + readTime(inFile, data[i]); +} + + +int LogFile::getLoginsCountAfterHour(short hours, short minutes, short seconds) const { + int ctr = 0; + int totalSeconds = hours * 3600 + minutes * 60 + seconds; + for (int i = 0; i < size; i++) { + if (data[i].toSeconds() > totalSeconds) { + ctr = i + 1; + break; + } + } + + return size - ctr; +} + +int LogFile::getLoginsCountBetweenHours(short h1, short m1, short s1, short h2, short m2, short s2) const { + return getLoginsCountAfterHour(h1, m1, s1) - getLoginsCountAfterHour(h2, m2, s2); +} + +int LogFile::getLongestActivePeriod() const { + int max = 0; + bool isFirstLogOut = !data[0].isLogin; + bool isLastLogIn = data[size - 1].isLogin; + + if (isFirstLogOut) + max = data[0].toSeconds(); + + if (isLastLogIn) { + int lastTimeInterval = 23 * 3600 + 59 * 60 + 59 - data[size - 1].toSeconds(); + + if (max < lastTimeInterval) + max = lastTimeInterval; + } + + for (int i = isFirstLogOut; i < size - isLastLogIn; i += 2) { + int diff = data[i + 1].toSeconds() - data[i].toSeconds(); + if (diff > max) + max = diff; + } + + return max; +} + +bool LogFile::loggedInHour(int hour) const { + int left = 0; + int right = size-1; + + while (left <= right) { + int mid = left + (right - left) / 2; + if (hour == data[mid].hour) { + return true; + } + + if (hour < data[mid].hour) { + right = mid - 1; + } + else { + left = mid + 1; + } + } + return false; +} \ No newline at end of file diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" new file mode 100644 index 0000000..258c7d5 --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" @@ -0,0 +1,24 @@ +#pragma once +#include +#include "Timestamp.h" +const int MAX_FILE_SIZE = 100; +class LogFile { +private: + size_t size; + Timestamp data[MAX_FILE_SIZE]; + + void readTime(std::ifstream& inFile, Timestamp& obj) const; + +public: + LogFile(); + + LogFile(const char* filePath); + + int getLoginsCountAfterHour(short hours, short minutes, short seconds) const; + + int getLoginsCountBetweenHours(short h1, short m1, short s1, short h2, short m2, short s2) const; + + int getLongestActivePeriod() const; + + bool loggedInHour(int hour) const; +}; \ No newline at end of file diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" new file mode 100644 index 0000000..28ff933 --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" @@ -0,0 +1,5 @@ +#include "Timestamp.h" + +int Timestamp::toSeconds() const{ + return hour * 3600 + minutes * 60 + seconds; +} \ No newline at end of file diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" new file mode 100644 index 0000000..e437187 --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" @@ -0,0 +1,9 @@ +#pragma once +struct Timestamp { + bool isLogin; + unsigned short hour; + unsigned short minutes; + unsigned short seconds; + + int toSeconds() const; +}; \ No newline at end of file diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" new file mode 100644 index 0000000..998b91f --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" @@ -0,0 +1,97 @@ +#include "User.h" +#include +#include +#include + +#pragma warning(disable : 4996) + +void generatePath(char* path, const char* bigger, const char* smaller) { + strcpy(path, bigger); + strcat(path, "-"); + strcat(path, smaller); + strcat(path, ".bat"); +} + +void User::createFileName(char* fileName, const User& other) const { + if (strcmp(name, other.name) == 1) { + generatePath(fileName, name, other.name); + } + else { + generatePath(fileName, other.name, name); + } +} + +User::User() : name(""), password(""), email(""){} + +User::User(const char* n, const char* pass, const char* mail) { + if (strlen(n) > NAME_LEN || strlen(pass) > PASS_LEN || strlen(mail) > MAIL_LEN) { + strcpy(name, ""); + strcpy(password, ""); + strcpy(email, ""); + + return; + } + + strcpy(name, n); + strcpy(password, pass); + strcpy(email, mail); +} + +void User::sendMessage(const char* message, const User& other) const { + char filePath[50]; + createFileName(filePath, other); + + std::ofstream outFile(filePath, std::ios::binary | std::ios::app); + + if (!outFile.is_open()) + return; + + outFile.write(name, sizeof(name)); + + int length = strlen(message); + + outFile.write((const char*)&length, sizeof(length)); + + outFile.write(message, length); + + outFile.close(); +} + +void User::viewMessage(const User& other) const { + char filePath[50]; + createFileName(filePath, other); + + std::ifstream inFile(filePath, std::ios::binary); + + if (!inFile.is_open()) + return; + + char buff[NAME_LEN + 1]; + + while (!inFile.eof()) { + inFile.read(buff, sizeof(buff)); + + if (inFile.eof()) + return; + + int length = 0; + + inFile.read((char*)&length, sizeof(length)); + + char* msg = new char[length + 1]; + + inFile.read(msg, length); + + msg[length] = '\0'; + + std::cout << buff << ": " << msg << std::endl; + + delete[] msg; + } + + inFile.close(); +} + +const char* User::getName() const { + return name; +} diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" new file mode 100644 index 0000000..4f94c7f --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" @@ -0,0 +1,26 @@ +#pragma once + +const int NAME_LEN = 20; +const int MAIL_LEN = 12; +const int PASS_LEN = 8; + +class User { +private: + // N символа за име +1 за терминираща нула! + char name[NAME_LEN + 1]; + char password[PASS_LEN + 1]; + char email[MAIL_LEN + 1]; + + void createFileName(char* fileName, const User& other) const; +public: + User(); + + User(const char* n, const char* pass, const char* email); + + void sendMessage(const char* message, const User& u) const; + + void viewMessage(const User& u) const; + + const char* getName() const; +}; + diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" new file mode 100644 index 0000000..c982451 --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" @@ -0,0 +1,80 @@ +#include "chatSystem.h" +#include +#include + +void chatSystem::save() const { + std::ofstream outFile(FILE_PATH, std::ios::binary); + + outFile.write((const char*)this, sizeof(chatSystem)); +} + +void chatSystem::read() { + std::ifstream inFile(FILE_PATH, std::ios::binary); + + inFile.read((char*)this, sizeof(chatSystem)); +} + +void chatSystem::searchIndexes(int& firstIndex, int& secondIndex, const char* fstName, const char* sndName) const { + for (int i = 0; i < size; i++) { + if (strcmp(fstName, data[i].getName()) == 0) { + firstIndex = i; + } + + if (strcmp(sndName, data[i].getName()) == 0) { + secondIndex = i; + } + + if (firstIndex != -1 && secondIndex != -1) + break; + } +} + +chatSystem::chatSystem() { + size = 0; + read(); +} + +void chatSystem::addUser(const User& arg) { + if (size > 99) + return; + + for (int i = 0; i < size; i++) { + if (strcmp(arg.getName(), data[i].getName()) == 0) { + return; + } + } + + data[size++] = arg; +} + +void chatSystem::sendMessage(const char* msg, const char* fstName, const char* sndName) const { + int firstIndex = -1; + int secondIndex = -1; + + searchIndexes(firstIndex, secondIndex, fstName, sndName); + + if (firstIndex == -1 || secondIndex == -1) + return; + + data[firstIndex].sendMessage(msg, data[secondIndex]); +} + +void chatSystem::viewMessages(const char* fstName, const char* sndName) const { + int firstIndex = -1; + int secondIndex = -1; + + searchIndexes(firstIndex, secondIndex, fstName, sndName); + + if (firstIndex == -1 || secondIndex == -1) + return; + + data[firstIndex].viewMessage(data[secondIndex]); +} + +int chatSystem::getSize() const { + return size; +} + +chatSystem::~chatSystem() { + save(); +} diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" new file mode 100644 index 0000000..1f21bf3 --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" @@ -0,0 +1,31 @@ +#pragma once +#include"User.h" + +const char FILE_PATH[] = "database.bat"; + +class chatSystem { +private: + User data[100]; + size_t size; + + void save() const; + + void read(); + + void searchIndexes(int& firstIndex, int& secondIndex, const char* fstName, const char* sndName) const; + +public: + chatSystem(); + + void addUser(const User& arg); + + void sendMessage(const char* msg, const char* fstName, const char* sndName) const; + + void viewMessages(const char* sender, const char* reciever) const; + + int getSize() const; + + ~chatSystem(); + +}; + diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" new file mode 100644 index 0000000..111878f Binary files /dev/null and "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" differ diff --git "a/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" new file mode 100644 index 0000000..951b7d2 --- /dev/null +++ "b/Utills/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" @@ -0,0 +1,89 @@ +#include +#include + +template +class Entity { +private: + int integerData; + char symbolData; + T abstractData; + +public: + Entity(int iArg, char cArg, const T& aArg) : integerData(iArg), symbolData(cArg), abstractData(aArg) {} + + int getIntegerData() const { + return integerData; + } + + char getCharData() const { + return symbolData; + } + + const T& getAbstractData() const { + return abstractData; + } + + void print() const { + std::cout << integerData << " " << symbolData << " " << abstractData << std::endl; + } +}; + +template +bool compareIntegerValues(const Entity& fst, const Entity& snd) { + return fst.getIntegerData() < snd.getIntegerData(); +} + +template +bool compareCharValues(const Entity& fst, const Entity& snd) { + return fst.getCharData() < snd.getCharData(); +} + +template +bool compareAbstractValues(const Entity& fst, const Entity& snd) { + return fst.getAbstractData() < snd.getAbstractData(); +} + +template +void sort(std::vector>& collection, bool (*lessThan)(const Entity&, const Entity&)) { + for (size_t i = 0; i < collection.size(); i++) { + size_t minElementIndex = i; + + for (size_t j = i + 1; j < collection.size(); j++) { + if (lessThan(collection[j], collection[minElementIndex])) { + minElementIndex = j; + } + } + + if (minElementIndex != i) + std::swap(collection[i], collection[minElementIndex]); + } +} + +char generateRandomLetter() { + return (char)(rand() % 26 + 'a'); +} + +double generateRandomDouble() { + return (double)rand() / RAND_MAX + rand() % 256; +} + +int main() { + std::vector> collection; + + for (size_t i = 0; i < 100; i++) + collection.push_back({ rand() % 10000, generateRandomLetter(), generateRandomDouble() }); + + + // Sort integer values + // sort(collection, compareIntegerValues); + + // Sort char values + // sort(collection, compareCharValues); + + // Sort abstract values + sort(collection, compareAbstractValues); + + + for (size_t i = 0; i < 100; i++) + collection[i].print(); +} diff --git "a/homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" "b/homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" deleted file mode 100644 index a7a27ba..0000000 Binary files "a/homework_1_SI/\320\224\320\276\320\274\320\260\321\210\320\275\320\260_\321\200\320\260\320\261\320\276\321\202\320\260_1.pdf" and /dev/null differ diff --git a/k_1_SI/task1.cpp b/k_1_SI/task1.cpp deleted file mode 100644 index 77ad5f5..0000000 --- a/k_1_SI/task1.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include - -using namespace std; - -struct Point -{ - int x; - int y; - Point() : Point(0,0){} - - Point(int x, int y) - { - this->x = x; - this->y = y; - } - - double getDistance(const Point& other) const - { - int dx = x - other.x; - int dy = y - other.y; - return sqrt(dx * dx + dy * dy); - } -}; - -void print(const Point& p) -{ - std::cout << "[ x: " << p.x << ", y: " << p.y <<" ]"; -} - -struct Rectangle -{ -private: - Point points[4]; - -public: - Rectangle() = default; //the same as Rectangle() {} - - void setPoint(size_t index, int x, int y) - { - if(index > 3) - return; - - points[index].x = x; - points[index].y = y; - - } - const Point& getPoint(size_t index) const - { - if(index > 3) - return {}; //some Error. This is a refference to a temporary memory, so a better solution would be to throw an exception. - return points[index]; - } - double getPer() const - { - return 2 * points[0].getDistance(points[1]) + 2 * points[1].getDistance(points[2]); - } -}; - -void print(const Rectangle& rect) -{ - for(int i = 0; i < 4; i++) - { - print(rect.getPoint(i)); - } -} - -void swap(Rectangle& tr1, Rectangle& tr2) -{ - Rectangle temp = tr1; - tr1 = tr2; - tr2 = temp; -} - -void selectionSort(Rectangle* triangles, double* perimeters, unsigned size) -{ - for (unsigned i = 0; i < size - 1; i++) - { - //findMin - int minElementIndex = i; - for (unsigned j = i + 1; j < size; j++) - { - if (perimeters[j] < perimeters[minElementIndex]) - minElementIndex = j; - } - - if (minElementIndex != i) - { - swap(perimeters[i], perimeters[minElementIndex]); - swap(triangles[i], triangles[minElementIndex]); - } - } -} - -int main() -{ - ifstream file("rects.txt"); - - if(!file.is_open()) - { - std::cout << "Error while opening the file!"; - return -1; - } - - int n; - file >> n; - - Rectangle* rects = new Rectangle[n]; - double* perimeters = new double[n]; //this should be size_t, but we are using a double returning function for getDist. - - for(size_t i = 0; i < n; i++) - { - int x1, y1, x3, y3; - file >> x1 >> y1 >> x3 >> y3; - - rects[i].setPoint(0, x1, y1); - rects[i].setPoint(1, x1, y3); - rects[i].setPoint(2, x3, y3); - rects[i].setPoint(3, x3, y1); - - perimeters[i] = rects[i].getPer(); - - } - - selectionSort(rects, perimeters, n); //selection sort, because we want min number of copies (or swaps) - - for(size_t i = 0; i < n; i++) - { - print(rects[i]); - std::cout <<", Per: " << perimeters[i] << std::endl; - } - - - delete[] rects, perimeters; - return 0; -} - diff --git a/k_1_SI/task2_read.cpp b/k_1_SI/task2_read.cpp deleted file mode 100644 index e6a8312..0000000 --- a/k_1_SI/task2_read.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include -using namespace std; - -const size_t MAX_NAME_SIZE = 22; -struct Student -{ - //gender should be BEFORE fn so the struct would be 28 bytes. - char name[MAX_NAME_SIZE + 1]; - bool isMale; - size_t fn; -}; - - -size_t getFileSize(ifstream& f) -{ - size_t currentPos = f.tellg(); - f.seekg(0, ios::end); - size_t size = f.tellg(); - - f.seekg(currentPos); - return size; -} - -void readFromFile(Student*& ptr, size_t& studentsCount, ifstream& f) -{ - size_t sizeOfFile = getFileSize(f); - studentsCount = sizeOfFile / sizeof(Student); - ptr = new Student[studentsCount]; - f.read((char*)ptr, sizeOfFile); -} - - -int main() -{ - Student* arr; - size_t count; - ifstream file("students.dat"); - - if(!file.is_open()) - { - std::cout << "Error while opening the file!" << std::endl; - return -1; - } - - readFromFile(arr, count, file); - - for (int i = 0; i < count; i++) - std::cout << "Name: " << arr[i].name << ", fn: " << arr[i].fn << ", is male: " << arr[i].isMale << std::endl; - - delete[] arr; - - return 0; -} diff --git a/k_1_SI/task2_write.cpp b/k_1_SI/task2_write.cpp deleted file mode 100644 index f320557..0000000 --- a/k_1_SI/task2_write.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include -using namespace std; - -const size_t MAX_NAME_SIZE = 22; -struct Student -{ - //gender should be BEFORE fn so the struct would be 28 bytes. - char name[MAX_NAME_SIZE + 1]; - bool isMale; - size_t fn; -}; - -bool isLower(char ch) -{ - return ch >= 'a' && ch <= 'z'; -} - -bool areAllLowerCase(const char* str) -{ - while(*str) - { - if(!isLower(*str)) - return false; - str++; - } - return true; -} - -bool isUpper(char ch) -{ - return ch >= 'A' && ch <= 'Z'; -} - -bool isNameValid(const char* name) -{ - size_t len = strlen(name); - if(len == 0 || len > MAX_NAME_SIZE) - return false; - return isUpper(*name) && areAllLowerCase(name + 1); -} - -void initStudent(Student& st, const char* name, size_t fn, bool isMale) -{ - if(!isNameValid(name)) - strcpy(st.name, "Unknown"); - else - strcpy(st.name, name); - - st.isMale = isMale; - st.fn = fn; -} - -void saveToFile(const Student* students, size_t count, ofstream& file) -{ - file.write((const char*)students, count * sizeof(Student)); -} - - -int main() -{ - const size_t studentsCount = 4; - Student* arr = new Student[studentsCount]; - initStudent(arr[0], "Ivan", 1, true); - initStudent(arr[1], "Petur", 2, true); - initStudent(arr[2], "Alex", 3, true); - initStudent(arr[3], "Katerina", 4, false); - - ofstream file("students.dat", ios::binary); - - if(!file.is_open()) - { - std::cout << "Error while opening the file!" << std::endl; - delete[] arr; //! - return -1; - } - - saveToFile(arr, studentsCount, file); - - delete[] arr; - - return 0; -} diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" deleted file mode 100644 index 8845349..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Source.cpp" +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include"chatSystem.h" - -int main() { - chatSystem data; - - data.addUser({ "Ivan", "1234", "iv@abv.bg" }); - data.addUser({ "Qvor", "4321", "qv@abv.bg" }); - - data.sendMessage("last example", "Ivan", "Qvor"); - data.viewMessages("Qvor", "Ivan"); -} \ No newline at end of file diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" deleted file mode 100644 index 01fa25e..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.cpp" +++ /dev/null @@ -1,92 +0,0 @@ -#include "LogFile.h" - -void LogFile::readTime(std::ifstream& inFile, Timestamp& obj) const { - char isLogin; - - inFile >> isLogin; - - obj.isLogin = (isLogin == '+'); - - inFile >> obj.hour >> obj.minutes >> obj.seconds; -} - - -LogFile::LogFile() { - size = 0; -} - -LogFile::LogFile(const char* filePath) { - std::ifstream inFile(filePath); - - if (!inFile.is_open()) { - size = 0; - return; - } - - inFile >> size; - - for (int i = 0; i < size; i++) - readTime(inFile, data[i]); -} - - -int LogFile::getLoginsCountAfterHour(short hours, short minutes, short seconds) const { - int ctr = 0; - int totalSeconds = hours * 3600 + minutes * 60 + seconds; - for (int i = 0; i < size; i++) { - if (data[i].toSeconds() > totalSeconds) { - ctr = i + 1; - break; - } - } - - return size - ctr; -} - -int LogFile::getLoginsCountBetweenHours(short h1, short m1, short s1, short h2, short m2, short s2) const { - return getLoginsCountAfterHour(h1, m1, s1) - getLoginsCountAfterHour(h2, m2, s2); -} - -int LogFile::getLongestActivePeriod() const { - int max = 0; - bool isFirstLogOut = !data[0].isLogin; - bool isLastLogIn = data[size - 1].isLogin; - - if (isFirstLogOut) - max = data[0].toSeconds(); - - if (isLastLogIn) { - int lastTimeInterval = 23 * 3600 + 59 * 60 + 59 - data[size - 1].toSeconds(); - - if (max < lastTimeInterval) - max = lastTimeInterval; - } - - for (int i = isFirstLogOut; i < size - isLastLogIn; i += 2) { - int diff = data[i + 1].toSeconds() - data[i].toSeconds(); - if (diff > max) - max = diff; - } - - return max; -} - -bool LogFile::loggedInHour(int hour) const { - int left = 0; - int right = size-1; - - while (left <= right) { - int mid = left + (right - left) / 2; - if (hour == data[mid].hour) { - return true; - } - - if (hour < data[mid].hour) { - right = mid - 1; - } - else { - left = mid + 1; - } - } - return false; -} \ No newline at end of file diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" deleted file mode 100644 index 8c021f8..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/LogFile.h" +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include -#include "Timestamp.h" -const int MAX_FILE_SIZE = 100; -class LogFile { -private: - size_t size; - Timestamp data[MAX_FILE_SIZE]; - - void readTime(std::ifstream& inFile, Timestamp& obj) const; - -public: - LogFile(); - - LogFile(const char* filePath); - - int getLoginsCountAfterHour(short hours, short minutes, short seconds) const; - - int getLoginsCountBetweenHours(short h1, short m1, short s1, short h2, short m2, short s2) const; - - int getLongestActivePeriod() const; - - bool loggedInHour(int hour) const; -}; \ No newline at end of file diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" deleted file mode 100644 index a874eb8..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.cpp" +++ /dev/null @@ -1,5 +0,0 @@ -#include "Timestamp.h" - -int Timestamp::toSeconds() const{ - return hour * 3600 + minutes * 60 + seconds; -} \ No newline at end of file diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" deleted file mode 100644 index 912d69a..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/Task1/Timestamp.h" +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -struct Timestamp { - bool isLogin; - unsigned short hour; - unsigned short minutes; - unsigned short seconds; - - int toSeconds() const; -}; \ No newline at end of file diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" deleted file mode 100644 index 7f5a53d..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.cpp" +++ /dev/null @@ -1,97 +0,0 @@ -#include "User.h" -#include -#include -#include - -#pragma warning(disable : 4996) - -void generatePath(char* path, const char* bigger, const char* smaller) { - strcpy(path, bigger); - strcat(path, "-"); - strcat(path, smaller); - strcat(path, ".bat"); -} - -void User::createFileName(char* fileName, const User& other) const { - if (strcmp(name, other.name) == 1) { - generatePath(fileName, name, other.name); - } - else { - generatePath(fileName, other.name, name); - } -} - -User::User() : name(""), password(""), email(""){} - -User::User(const char* n, const char* pass, const char* mail) { - if (strlen(n) > NAME_LEN || strlen(pass) > PASS_LEN || strlen(mail) > MAIL_LEN) { - strcpy(name, ""); - strcpy(password, ""); - strcpy(email, ""); - - return; - } - - strcpy(name, n); - strcpy(password, pass); - strcpy(email, mail); -} - -void User::sendMessage(const char* message, const User& other) const { - char filePath[50]; - createFileName(filePath, other); - - std::ofstream outFile(filePath, std::ios::binary | std::ios::app); - - if (!outFile.is_open()) - return; - - outFile.write(name, sizeof(name)); - - int length = strlen(message); - - outFile.write((const char*)&length, sizeof(length)); - - outFile.write(message, length); - - outFile.close(); -} - -void User::viewMessage(const User& other) const { - char filePath[50]; - createFileName(filePath, other); - - std::ifstream inFile(filePath, std::ios::binary); - - if (!inFile.is_open()) - return; - - char buff[NAME_LEN + 1]; - - while (!inFile.eof()) { - inFile.read(buff, sizeof(buff)); - - if (inFile.eof()) - return; - - int length = 0; - - inFile.read((char*)&length, sizeof(length)); - - char* msg = new char[length + 1]; - - inFile.read(msg, length); - - msg[length] = '\0'; - - std::cout << buff << ": " << msg << std::endl; - - delete[] msg; - } - - inFile.close(); -} - -const char* User::getName() const { - return name; -} diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" deleted file mode 100644 index ab729cd..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/User.h" +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -const int NAME_LEN = 20; -const int MAIL_LEN = 12; -const int PASS_LEN = 8; - -class User { -private: - // N символа за име +1 за терминираща нула! - char name[NAME_LEN + 1]; - char password[PASS_LEN + 1]; - char email[MAIL_LEN + 1]; - - void createFileName(char* fileName, const User& other) const; -public: - User(); - - User(const char* n, const char* pass, const char* email); - - void sendMessage(const char* message, const User& u) const; - - void viewMessage(const User& u) const; - - const char* getName() const; -}; - diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" deleted file mode 100644 index 416b1f8..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.cpp" +++ /dev/null @@ -1,80 +0,0 @@ -#include "chatSystem.h" -#include -#include - -void chatSystem::save() const { - std::ofstream outFile(FILE_PATH, std::ios::binary); - - outFile.write((const char*)this, sizeof(chatSystem)); -} - -void chatSystem::read() { - std::ifstream inFile(FILE_PATH, std::ios::binary); - - inFile.read((char*)this, sizeof(chatSystem)); -} - -void chatSystem::searchIndexes(int& firstIndex, int& secondIndex, const char* fstName, const char* sndName) const { - for (int i = 0; i < size; i++) { - if (strcmp(fstName, data[i].getName()) == 0) { - firstIndex = i; - } - - if (strcmp(sndName, data[i].getName()) == 0) { - secondIndex = i; - } - - if (firstIndex != -1 && secondIndex != -1) - break; - } -} - -chatSystem::chatSystem() { - size = 0; - read(); -} - -void chatSystem::addUser(const User& arg) { - if (size > 99) - return; - - for (int i = 0; i < size; i++) { - if (strcmp(arg.getName(), data[i].getName()) == 0) { - return; - } - } - - data[size++] = arg; -} - -void chatSystem::sendMessage(const char* msg, const char* fstName, const char* sndName) const { - int firstIndex = -1; - int secondIndex = -1; - - searchIndexes(firstIndex, secondIndex, fstName, sndName); - - if (firstIndex == -1 || secondIndex == -1) - return; - - data[firstIndex].sendMessage(msg, data[secondIndex]); -} - -void chatSystem::viewMessages(const char* fstName, const char* sndName) const { - int firstIndex = -1; - int secondIndex = -1; - - searchIndexes(firstIndex, secondIndex, fstName, sndName); - - if (firstIndex == -1 || secondIndex == -1) - return; - - data[firstIndex].viewMessage(data[secondIndex]); -} - -int chatSystem::getSize() const { - return size; -} - -chatSystem::~chatSystem() { - save(); -} diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" deleted file mode 100644 index cf7332a..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/chatSystem.h" +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include"User.h" - -const char FILE_PATH[] = "database.bat"; - -class chatSystem { -private: - User data[100]; - size_t size; - - void save() const; - - void read(); - - void searchIndexes(int& firstIndex, int& secondIndex, const char* fstName, const char* sndName) const; - -public: - chatSystem(); - - void addUser(const User& arg); - - void sendMessage(const char* msg, const char* fstName, const char* sndName) const; - - void viewMessages(const char* sender, const char* reciever) const; - - int getSize() const; - - ~chatSystem(); - -}; - diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" deleted file mode 100644 index 111878f..0000000 Binary files "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\320\267\320\260\320\264\320\260\321\207\320\270 \320\267\320\260 \320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265 \320\241\320\230.pdf" and /dev/null differ diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" deleted file mode 100644 index d5e5e4e..0000000 --- "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include - -template -class Entity { -private: - int integerData; - char symbolData; - T abstractData; - -public: - Entity(int iArg, char cArg, const T& aArg) : integerData(iArg), symbolData(cArg), abstractData(aArg) {} - - int getIntegerData() const { - return integerData; - } - - char getCharData() const { - return symbolData; - } - - const T& getAbstractData() const { - return abstractData; - } - - void print() const { - std::cout << integerData << " " << symbolData << " " << abstractData << std::endl; - } -}; - -template -bool compareIntegerValues(const Entity& fst, const Entity& snd) { - return fst.getIntegerData() < snd.getIntegerData(); -} - -template -bool compareCharValues(const Entity& fst, const Entity& snd) { - return fst.getCharData() < snd.getCharData(); -} - -template -bool compareAbstractValues(const Entity& fst, const Entity& snd) { - return fst.getAbstractData() < snd.getAbstractData(); -} - -template -void sort(std::vector>& collection, bool (*lessThan)(const Entity&, const Entity&)) { - for (size_t i = 0; i < collection.size(); i++) { - size_t minElementIndex = i; - - for (size_t j = i + 1; j < collection.size(); j++) { - if (lessThan(collection[j], collection[minElementIndex])) { - minElementIndex = j; - } - } - - if (minElementIndex != i) - std::swap(collection[i], collection[minElementIndex]); - } -} - -char generateRandomLetter() { - return (char)(rand() % 26 + 'a'); -} - -double generateRandomDouble() { - return (double)rand() / RAND_MAX + rand() % 256; -} - -int main() { - std::vector> collection; - - for (size_t i = 0; i < 100; i++) - collection.push_back({ rand() % 10000, generateRandomLetter(), generateRandomDouble() }); - - - // Sort integer values - // sort(collection, compareIntegerValues); - - // Sort char values - // sort(collection, compareCharValues); - - // Sort abstract values - sort(collection, compareAbstractValues); - - - for (size_t i = 0; i < 100; i++) - collection[i].print(); -} -- cgit v1.3.1-23-gd82d