diff options
Added my work from todays exam
8 files changed, 370 insertions, 0 deletions
diff --git a/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp b/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp new file mode 100644 index 0000000..84845d8 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.cpp @@ -0,0 +1,167 @@ +#include "NaturalNumbers.h" +#include <cstring> + +/* Private */ + +void NaturalNumbers::copyFrom(const NaturalNumbers& other) { + this->nameLength = other.nameLength; + this->elementsCount = other.elementsCount; + this->name = new char[this->nameLength]; + this->elements = new int[this->elementsCount]; + strcpy(this->name, other.name); + for (unsigned i = 0; i < this->elementsCount; i++) + this->elements[i] = other.elements[i]; +} + +void NaturalNumbers::free() { + delete[] elements; + delete[] name; +} + +void NaturalNumbers::move(NaturalNumbers&& other) { + this->elementsCount = other.elementsCount; + this->nameLength = other.nameLength; + + this->name = other.name; + other.name = nullptr; + this->elements = other.elements; + other.elements = nullptr; +} + +void NaturalNumbers::resize() { + unsigned newLength = (this->elementsLength == 0) ? 8 : this->elementsLength << 1; + + int* largerElementsArray = new int[newLength]; + for (int i = 0; i < this->elementsLength; i++) + largerElementsArray[i] = this->elements[i]; + + delete[] this->elements; + this->elements = largerElementsArray; + this->elementsLength = newLength; +} + +int NaturalNumbers::indexOfNum(int number) const { + for (unsigned i = 0; i < this->elementsCount; i++) { + if (this->elements[i] == number) + return i; + } + return -1; +} + +/* Public */ + +NaturalNumbers::NaturalNumbers() : NaturalNumbers(nullptr, 0, "Empty set") +{} + +NaturalNumbers::NaturalNumbers(const int* elements, unsigned elementsCount, const char* name) { + this->elementsCount = this->elementsLength = 0; + this->elements = nullptr; + + for (unsigned i = 0; i < elementsCount; i++) { + this->addElement(elements[i]); + } + + this->name = nullptr; + this->set_name(name); +} + +NaturalNumbers::~NaturalNumbers() { + this->free(); +} + +NaturalNumbers::NaturalNumbers(const NaturalNumbers& other) { + copyFrom(other); +} + +NaturalNumbers& NaturalNumbers::operator=(const NaturalNumbers& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +bool NaturalNumbers::operator[](int number) const { + return this->indexOfNum(number) > -1; +} + +NaturalNumbers& NaturalNumbers::operator+=(const NaturalNumbers& other) { + for (unsigned i = 0; i < other.elementsCount; i++) { + this->addElement(other.elements[i]); + } + + unsigned newNameLength = 19 + this->nameLength + other.nameLength; + char* newName = new char[newNameLength]; + strcat(newName, "Union of \'"); + strcat(newName, this->name); + strcat(newName, "\' and \'"); + strcat(newName, other.name); + strcat(newName, "\'"); + + delete[] this->name; + this->name = newName; + this->nameLength = newNameLength; + + return *this; +} + +const int* NaturalNumbers::get_elements() const { + return this->elements; +} + +bool NaturalNumbers::addElement(int number) { + if (indexOfNum(number) == -1 && number > -1) { + if (this->elementsLength == this->elementsCount) + this->resize(); + + this->elements[this->elementsCount++] = number; + return true; + } + return false; +} + +unsigned NaturalNumbers::get_elementsCount() const { + return this->elementsCount; +} + +const char* NaturalNumbers::get_name() const { + return this->name; +} + +void NaturalNumbers::set_name(const char* newName) { + this->nameLength = strlen(newName); + delete[] this->name; + this->name = new char[this->nameLength]; + strcpy(this->name, newName); +} + +/* External functions */ + +NaturalNumbers operator+(const NaturalNumbers& lhs, const NaturalNumbers& rhs) { + NaturalNumbers copy(lhs); + copy += rhs; + return copy; +} + +std::ostream& operator<<(std::ostream& ostr, const NaturalNumbers& numbers) { + ostr << numbers.get_name() << " {"; + if (numbers.get_elementsCount() > 0) { + for (int i = 0; i < numbers.get_elementsCount() - 1; i++) + ostr << numbers.get_elements()[i] << ", "; + ostr << numbers.get_elements()[numbers.get_elementsCount() - 1]; + } + ostr << "}"; + return ostr; +} + +std::istream& operator>>(std::istream& istr, NaturalNumbers& numbers) { + unsigned count, buffer; + istr >> count; + while (count > 0) { + istr >> buffer; + numbers.addElement(buffer); + count--; + } + + return istr; +} diff --git a/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.h b/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.h new file mode 100644 index 0000000..6282c1d --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex1/NaturalNumbers.h @@ -0,0 +1,41 @@ +#include <istream> +#include <ostream> + +class NaturalNumbers { + char* name; + unsigned nameLength; + + int* elements; + unsigned elementsCount; + unsigned elementsLength; + + void copyFrom(const NaturalNumbers& other); + void free(); + void move(NaturalNumbers&& other); + + void resize(); + int indexOfNum(int number) const; + +public: + NaturalNumbers(); + NaturalNumbers(const int* elements, unsigned elementsCount, const char* name); + + ~NaturalNumbers(); + NaturalNumbers(const NaturalNumbers& other); + NaturalNumbers& operator=(const NaturalNumbers& other); + + bool operator[](int number) const; + NaturalNumbers& operator+=(const NaturalNumbers& other); + + const int* get_elements() const; + bool addElement(int number); + unsigned get_elementsCount() const; + + const char* get_name() const; + void set_name(const char* newName); +}; + +NaturalNumbers operator+(const NaturalNumbers& lhs, const NaturalNumbers& rhs); + +std::ostream& operator<<(std::ostream& ostr, const NaturalNumbers& numbers); +std::istream& operator>>(std::istream& istr, NaturalNumbers& numbers); diff --git a/Year 1/Object-oriented Programming/Exam 1/ex1/source.cpp b/Year 1/Object-oriented Programming/Exam 1/ex1/source.cpp new file mode 100644 index 0000000..5f741df --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex1/source.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include "NaturalNumbers.h" + +int main() { + int numArr1[] = { 1, 2, 3 }, numArr2[] = { -1, 0, 2, 3, 4 }, numArr3[] = { -1000, 0, -9, -9, 10, 0, 0, 0, 1, -46 }; + NaturalNumbers nums, nums1(numArr1, 3, "Nums1"), nums2(numArr2, 5, "Nums2"), nums3(numArr3, 10, "Nums3"); + std::cout << nums << nums1 << nums2 << nums3 << std::endl; + nums1 += nums2; + std::cout << nums2 << std::endl; + NaturalNumbers nums4 = nums2 + nums3; + std::cout << nums4 << std::endl; + NaturalNumbers nums5; + std::cin >> nums5; + std::cout << nums5 << " " << nums5[13] << std::endl; +} diff --git a/Year 1/Object-oriented Programming/Exam 1/ex2/Book.cpp b/Year 1/Object-oriented Programming/Exam 1/ex2/Book.cpp new file mode 100644 index 0000000..c487e41 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex2/Book.cpp @@ -0,0 +1,32 @@ +#include "Book.h" +#include <cstring> + +Book::Book() +{} + +Book::Book(const char* name, const char* author, unsigned count) { + this->set_name(name); + this->set_author(author); + this->set_count(count); +} + +const char* Book::get_name() const { + return this->name; +} +void Book::set_name(const char* newName) { + strcpy(this->name, newName); +} + +const char* Book::get_author() const { + return this->author; +} +void Book::set_author(const char* newAuthor) { + strcpy(this->author, newAuthor); +} + +unsigned Book::get_count() const { + return this->count; +} +void Book::set_count(unsigned newCount) { + this->count = newCount; +} diff --git a/Year 1/Object-oriented Programming/Exam 1/ex2/Book.h b/Year 1/Object-oriented Programming/Exam 1/ex2/Book.h new file mode 100644 index 0000000..849d0bd --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex2/Book.h @@ -0,0 +1,18 @@ +class Book { + char name[100]; + char author[100]; + unsigned count; // тираж + +public: + Book(); + Book(const char* name, const char* author, unsigned count); + + const char* get_name() const; + void set_name(const char* newName); + + const char* get_author() const; + void set_author(const char* newAuthor); + + unsigned get_count() const; + void set_count(unsigned newCount); +}; diff --git a/Year 1/Object-oriented Programming/Exam 1/ex2/BookList.cpp b/Year 1/Object-oriented Programming/Exam 1/ex2/BookList.cpp new file mode 100644 index 0000000..32d39f0 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex2/BookList.cpp @@ -0,0 +1,65 @@ +#include "BookList.h" + +/* Private */ + +void BookList::resize() { + this->bookLength = (this->bookLength == 0) ? 8 : this->bookLength << 1; + + Book* newBooks = new Book[this->bookLength]; + for (unsigned i = 0; i < this->bookCount; i++) + newBooks[i] = this->books[i]; + delete[] this->books; + this->books = newBooks; +} + +void BookList::readFromFile(std::ifstream& inFile) { + char nameBuffer[100], authorBuffer[100]; + unsigned countBuffer; + while(!inFile.eof()) { + inFile.read(nameBuffer, sizeof(nameBuffer)); + inFile.read(authorBuffer, sizeof(authorBuffer)); + inFile.read((char*)&countBuffer, sizeof(countBuffer)); + this->addBook(Book(nameBuffer, authorBuffer, countBuffer)); + } +} + +void BookList::writeToFile(std::ofstream& outFile) const { + unsigned countBuffer; + for (int i = 0; i < this->bookCount; i++) { + outFile.write(this->books[i].get_name(), sizeof(this->books[i].get_name())); + outFile.write(this->books[i].get_author(), sizeof(this->books[i].get_author())); + countBuffer = this->books[i].get_count(); + outFile.write((const char*)&countBuffer, sizeof(countBuffer)); + } +} + +void BookList::writePopularToFile(std::ofstream& outFile) const { + for (int i = 0; i < this->bookCount; i++) { + if (this->books[i].get_count() >= 1000) { + outFile << this->books[i].get_name() << " " << this->books[i].get_author() << " " << this->books[i].get_count() << std::endl; + } + } +} + +BookList::BookList() +{} + +// Не ми стигна времето, но другите функции са аналогични на тeзи двете + +bool BookList::readFromFile(const char* filePath) { + std::ifstream file(filePath, std::ios::binary); + if (!file.is_open()) + return false; + this->readFromFile(file); + file.close(); + return true; +} + +bool BookList::writePopularToFile(const char* filePath) const { + std::ofstream file(filePath); + if (!file.is_open()) + return false; + this->writePopularToFile(file); + file.close(); + return true; +} diff --git a/Year 1/Object-oriented Programming/Exam 1/ex2/BookList.h b/Year 1/Object-oriented Programming/Exam 1/ex2/BookList.h new file mode 100644 index 0000000..0d1d415 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex2/BookList.h @@ -0,0 +1,24 @@ +#include "Book.h" +#include <fstream> + +class BookList { + Book* books; + unsigned bookCount; + unsigned bookLength; + + void resize(); + + void readFromFile(std::ifstream& inFile); + void writeToFile(std::ofstream& outFile) const; + void writePopularToFile(std::ofstream& outFile) const; + +public: + BookList(); + BookList(const Book* books); + + bool addBook(const Book& book); + + bool readFromFile(const char* filePath); + bool writeToFile(const char* filePath) const; + bool writePopularToFile(const char* filePath) const; +}; diff --git a/Year 1/Object-oriented Programming/Exam 1/ex2/source.cpp b/Year 1/Object-oriented Programming/Exam 1/ex2/source.cpp new file mode 100644 index 0000000..522ca39 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 1/ex2/source.cpp @@ -0,0 +1,8 @@ +#include <iostream> +#include "BookList.h" + +int main() { + BookList bookList; + bookList.readFromFile("library.dat"); + bookList.writePopularToFile("popular.txt"); +} |
