From a014e4ca73d664f17e9125a4ca260c1677b57719 Mon Sep 17 00:00:00 2001 From: Christopher Sanden Date: Thu, 20 Nov 2025 15:24:00 +0100 Subject: [PATCH] Cleaning up and adding comments --- .../Portfolio/Assignment-01/option1.cpp | 65 ++++++++++++++----- .../Portfolio/Assignment-01/option1.h | 5 +- .../Portfolio/SharedLib/TDoublyLinkedList.cpp | 14 ++++ .../Portfolio/SharedLib/TDoublyLinkedList.h | 18 ++++- Exam/IKT203Exam/Portfolio/SharedLib/TStack.h | 10 ++- .../Portfolio/SharedLib/TTreeQueue.h | 8 ++- Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp | 9 ++- 7 files changed, 102 insertions(+), 27 deletions(-) diff --git a/Exam/IKT203Exam/Portfolio/Assignment-01/option1.cpp b/Exam/IKT203Exam/Portfolio/Assignment-01/option1.cpp index 4791764..4f6efb0 100644 --- a/Exam/IKT203Exam/Portfolio/Assignment-01/option1.cpp +++ b/Exam/IKT203Exam/Portfolio/Assignment-01/option1.cpp @@ -1,5 +1,6 @@ // Option 1 (Standard): Console Text Editor. -// +// Implements the user-facing console loop for the text editor. +// Handles line editing, undo/redo logic, and print job queue operations. #include "option1.h" #include @@ -16,6 +17,9 @@ bool running = true; int lastIndex = 0; std::string deletedLine; +// Undo the last text modification. +// Reverses INSERT/DELETE actions by applying the inverse operation. +// Moves reversed action into redoStack. void Undo() { if (!undoStack.IsEmpty()) { @@ -29,6 +33,10 @@ void Undo() redoStack.Push(action); } } + +// Redo the last undone modification. +// Re-applies an action previously undone. +// Pushes the executed action back into undoStack. void Redo() { if (!redoStack.IsEmpty()) { @@ -43,7 +51,9 @@ void Redo() } } - +// Main menu loop for the Console Text Editor. +// Provides editing operations, queueing print jobs, +// and demonstrating FIFO behavior through job processing. int RunApp() { // Implement the Console Text Editor application logic here @@ -62,31 +72,56 @@ int RunApp() } case 3: { std::cout << "----------Current document----------" << std::endl; - for (int i = 0; i < document.GetSize(); i++) - std::cout << i + 1 << ". " << document.GetAtIndex(i) << std::endl; - std::cout << "------------------------------------\n\n"; + for (int i = 0; i < document.GetSize(); i++) { + std::cout << document.GetAtIndex(i) << std::endl; + } + std::cout << std::endl; break; } + + // Build a single print job containing the entire document. + // Snapshot is stored as a single string and enqueued. case 4: { - for (int i = 0; i < document.GetSize(); ++i) - printQueue.Enqueue(document.GetAtIndex(i)); - - std::cout << "----------Printing queue-----------" << std::endl; - - while (!printQueue.IsEmpty()) - std::cout << printQueue.Dequeue() << std::endl; - std::cout << "------------------------------------\n\n"; + std::cout << "----------Add print job----------" << std::endl; + if (document.GetSize() == 0) { + std::cout << "Document is empty - nothing added to print queue." << std::endl; break; } - case 5: { + std::string job; + for (int i = 0; i < document.GetSize(); i++) { + job += document.GetAtIndex(i) + "\n"; + } + printQueue.Enqueue(job); + std::cout << "Print job added to queue." << std::endl; + break; + } + + // Dequeue and print the next print job. + // Demonstrates FIFO (First-In-First-Out) queue behavior. + case 5: { + if (printQueue.IsEmpty()) { + std::cout << "No prints jobs in queue." << std::endl; + break; + } + + std::string job = printQueue.Dequeue(); + std::cout << "----------Printing job-----------" << std::endl; + std::cout << job << std::endl; + std::cout << "------------------------------------\n\n"; + break; + + } + + + case 6: { std::cout << "----------UNDO----------" <GetNext(); + delete cur; + cur = next; + } + head = tail = nullptr; + size = 0; +} +// Append a new line at the end of the list. void TDoublyLinkedList::Append(const std::string& line) { auto* newNode = new Node(line); @@ -17,6 +29,7 @@ void TDoublyLinkedList::Append(const std::string& line) size++; } +// Insert a new line at the beginning. void TDoublyLinkedList::Prepend(const std::string& line) { auto* newNode = new Node(line); @@ -43,6 +56,7 @@ TDoublyLinkedList::Node* TDoublyLinkedList::NavigateToNode(const int index) cons return node; } +// Removes the node at the given index and updates head/tail if needed. void TDoublyLinkedList::Remove(const int index) { auto* node = NavigateToNode(index); diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/TDoublyLinkedList.h b/Exam/IKT203Exam/Portfolio/SharedLib/TDoublyLinkedList.h index cf9c1c4..04746ad 100644 --- a/Exam/IKT203Exam/Portfolio/SharedLib/TDoublyLinkedList.h +++ b/Exam/IKT203Exam/Portfolio/SharedLib/TDoublyLinkedList.h @@ -2,13 +2,16 @@ #define TDOUBLYLINKEDLIST_H #include #include -#include "SharedLib.h" - +// Doubly linked list used to store document lines. +// Supports insertion, removal, and indexed access. +// Chosen because it allows efficient updates in the middle of the document. class TDoublyLinkedList { private: + // Internal node storing a single line of text + // and links to previous and next nodes. struct Node { std::string line; Node* next; @@ -45,12 +48,21 @@ private: public: TDoublyLinkedList() : head(nullptr), tail(nullptr), size(0) {} - ~TDoublyLinkedList() = default; + ~TDoublyLinkedList(); void Append(const std::string &line); void Prepend(const std::string& line); + + // Returns pointer to node at given index. + // Linear traversal; used internally by Remove and InsertAtIndex. [[nodiscard]] Node* NavigateToNode(int index) const; + + // Removes a node at the given index. + // Updates links and frees the removed node. void Remove(int index); + + // Returns the text stored at the given index. + // Uses NavigateToNode internally. [[nodiscard]] std::string GetAtIndex(int index) const; void InsertAtIndex(int index, const std::string &line); [[nodiscard]] int GetSize() const; diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/TStack.h b/Exam/IKT203Exam/Portfolio/SharedLib/TStack.h index e15cc0c..4315f29 100644 --- a/Exam/IKT203Exam/Portfolio/SharedLib/TStack.h +++ b/Exam/IKT203Exam/Portfolio/SharedLib/TStack.h @@ -10,8 +10,13 @@ enum EnumActionType { DELETE }; +// Simple fixed-size stack used for undo/redo. +// Stores actions describing line insert/delete operations. class TStack { private: + // Describes a single text-edit action. + // 'action' indicates INSERT or DELETE, + // 'text' stores the affected line, and 'index' is the line position. struct TAction { EnumActionType action; std::string text; @@ -24,9 +29,8 @@ private: public: TStack() = default; ~TStack() = default; - - void Push(const TAction& action); - TAction Pop(); + void Push(const TAction& action); // Adds a new action to the top of the stack. + TAction Pop(); // Removes and returns the most recent action. [[nodiscard]] TAction Peek() const; [[nodiscard]] bool IsEmpty() const; void Clear(); diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/TTreeQueue.h b/Exam/IKT203Exam/Portfolio/SharedLib/TTreeQueue.h index 487e518..e73b8fb 100644 --- a/Exam/IKT203Exam/Portfolio/SharedLib/TTreeQueue.h +++ b/Exam/IKT203Exam/Portfolio/SharedLib/TTreeQueue.h @@ -5,7 +5,9 @@ #include "TDoublyLinkedList.h" - +// Circular array-based queue implementation. +// Used in Cat 1 to store print jobs (each job is a full document snapshot). +// Demonstrates FIFO behavior through enqueue/dequeue operations. class TTreeQueue { private: std::string queue[MAX_SIZE]; @@ -17,8 +19,8 @@ public: TTreeQueue() = default; ~TTreeQueue() = default; - void Enqueue(const std::string& text); - std::string Dequeue(); + void Enqueue(const std::string& text); // Adds a new job at the tail of the queue. + std::string Dequeue(); // Removes and returns the next job in FIFO order. [[nodiscard]] int GetTail() const; [[nodiscard]] std::string Peek() const; [[nodiscard]] bool IsEmpty() const; diff --git a/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp b/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp index 16cdadc..880141e 100644 --- a/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp +++ b/Exam/IKT203Exam/Portfolio/SharedLib/Utils.cpp @@ -5,11 +5,11 @@ #include #include #include -#include +// Displays the main menu and reads user choice. int Utils::Choice() { - std::cout << "========\n1. Add line\n2. Remove line\n3. Print current document\n4. Print queue\n5. Undo\n6. Redo\n0. Exit" + std::cout << "========\n1. Add line\n2. Remove line\n3. View current1 document\n4. Print queue\n5. Process print job\n6. Undo\n7. Redo\n0. Exit" "\n\nChoice: "; int choice; std::cin >> choice; @@ -17,6 +17,9 @@ int Utils::Choice() return choice; } +// Inserts a new line into the document. +// Records the action into the undo stack. +// 'index' determines insert location; defaults to end of document. int Utils::Insert(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index) { for (int i = 0; i < document.GetSize(); i++) { @@ -58,6 +61,8 @@ void Utils::PrintList(const TDoublyLinkedList &document) std::cout << "\n\n"; } +// Removes a line chosen by the user. +// Action is pushed to undo stack for reversibility. int Utils::RemoveLine(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index) { std::cout << "Enter the number of the line you want to remove" <