Website Summary Files Commits Branches @commit @diff @stats
path: root / Utills
diff options
context:
space:
mode:
Commit5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Files52f913ec9532fd07c8f679ccf1e68876d1fc9ff2 /Utills
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar.gz
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.zip
Parent459d3ac280aafdba703930455f3200b6e7b0f31b (diff)
sem 11
diff --git a/Utills/homework_1_SI/Домашна_работа_1.pdf b/Utills/homework_1_SI/Домашна_работа_1.pdf
new file mode 100644
index 0000000..a7a27ba
--- /dev/null
+++ b/Utills/homework_1_SI/Домашна_работа_1.pdf
Binary files 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 <iostream>
+#include <fstream>
+#include <cmath>
+
+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 <iostream>
+#include <fstream>
+#include <cstring>
+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 <iostream>
+#include <fstream>
+#include <cstring>
+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/допълнително упражнение/Source.cpp b/Utills/допълнително упражнение/Source.cpp
new file mode 100644
index 0000000..17e6f4e
--- /dev/null
+++ b/Utills/допълнително упражнение/Source.cpp
@@ -0,0 +1,12 @@
+#include<iostream>
+#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/допълнително упражнение/Task1/LogFile.cpp b/Utills/допълнително упражнение/Task1/LogFile.cpp
new file mode 100644
index 0000000..7be397b
--- /dev/null
+++ b/Utills/допълнително упражнение/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/допълнително упражнение/Task1/LogFile.h b/Utills/допълнително упражнение/Task1/LogFile.h
new file mode 100644
index 0000000..258c7d5
--- /dev/null
+++ b/Utills/допълнително упражнение/Task1/LogFile.h
@@ -0,0 +1,24 @@
+#pragma once
+#include<fstream>
+#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/допълнително упражнение/Task1/Timestamp.cpp b/Utills/допълнително упражнение/Task1/Timestamp.cpp
new file mode 100644
index 0000000..28ff933
--- /dev/null
+++ b/Utills/допълнително упражнение/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/допълнително упражнение/Task1/Timestamp.h b/Utills/допълнително упражнение/Task1/Timestamp.h
new file mode 100644
index 0000000..e437187
--- /dev/null
+++ b/Utills/допълнително упражнение/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/допълнително упражнение/User.cpp b/Utills/допълнително упражнение/User.cpp
new file mode 100644
index 0000000..998b91f
--- /dev/null
+++ b/Utills/допълнително упражнение/User.cpp
@@ -0,0 +1,97 @@
+#include "User.h"
+#include<cstring>
+#include<fstream>
+#include<iostream>
+
+#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/допълнително упражнение/User.h b/Utills/допълнително упражнение/User.h
new file mode 100644
index 0000000..4f94c7f
--- /dev/null
+++ b/Utills/допълнително упражнение/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/допълнително упражнение/chatSystem.cpp b/Utills/допълнително упражнение/chatSystem.cpp
new file mode 100644
index 0000000..c982451
--- /dev/null
+++ b/Utills/допълнително упражнение/chatSystem.cpp
@@ -0,0 +1,80 @@
+#include "chatSystem.h"
+#include<cstring>
+#include<fstream>
+
+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/допълнително упражнение/chatSystem.h b/Utills/допълнително упражнение/chatSystem.h
new file mode 100644
index 0000000..1f21bf3
--- /dev/null
+++ b/Utills/допълнително упражнение/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/допълнително упражнение/задачи за допълнително упражнение СИ.pdf b/Utills/допълнително упражнение/задачи за допълнително упражнение СИ.pdf
new file mode 100644
index 0000000..111878f
--- /dev/null
+++ b/Utills/допълнително упражнение/задачи за допълнително упражнение СИ.pdf
Binary files differ
diff --git a/Utills/допълнително упражнение/функции от по - висок ред/demo.cpp b/Utills/допълнително упражнение/функции от по - висок ред/demo.cpp
new file mode 100644
index 0000000..951b7d2
--- /dev/null
+++ b/Utills/допълнително упражнение/функции от по - висок ред/demo.cpp
@@ -0,0 +1,89 @@
+#include<iostream>
+#include<vector>
+
+template<class T>
+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<class T>
+bool compareIntegerValues(const Entity<T>& fst, const Entity<T>& snd) {
+ return fst.getIntegerData() < snd.getIntegerData();
+}
+
+template<class T>
+bool compareCharValues(const Entity<T>& fst, const Entity<T>& snd) {
+ return fst.getCharData() < snd.getCharData();
+}
+
+template<class T>
+bool compareAbstractValues(const Entity<T>& fst, const Entity<T>& snd) {
+ return fst.getAbstractData() < snd.getAbstractData();
+}
+
+template<class T>
+void sort(std::vector<Entity<T>>& collection, bool (*lessThan)(const Entity<T>&, const Entity<T>&)) {
+ 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<Entity<double>> 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();
+}