diff options
Added my work from todays exam
5 files changed, 147 insertions, 0 deletions
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"); +} |
