diff options
Added my work from todays exam
1 files changed, 65 insertions, 0 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/Exam 1/ex2/BookList.cpp | 65 |
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; +} |
