Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Exam 1 / ex2 / BookList.cpp
diff options
context:
space:
mode:
Commit9f61b59ca6ceb76cd634240d118f5771c0534f3d (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-20 18:43:21 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-20 18:43:21 +0300
Files3fa15098faa79b5df0927e92dae0125c447ac1be /Year 1/Object-oriented Programming/Exam 1/ex2/BookList.cpp
uni-projects-9f61b59ca6ceb76cd634240d118f5771c0534f3d.tar
uni-projects-9f61b59ca6ceb76cd634240d118f5771c0534f3d.tar.gz
uni-projects-9f61b59ca6ceb76cd634240d118f5771c0534f3d.zip
Parent26cf424c42f9cdc172191faa5b2d1c1fcf350143 (diff)
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.cpp65
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;
+}