Cleaning up and adding comments

This commit is contained in:
Christopher Sanden
2025-11-20 15:24:00 +01:00
parent e77d7ff21e
commit a014e4ca73
7 changed files with 102 additions and 27 deletions

View File

@@ -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 <iostream>
@@ -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----------" <<std::endl;
Undo();
break;
}
case 6: {
case 7: {
std::cout << "----------REDO----------" <<std::endl;
Redo();
break;

View File

@@ -1,4 +1,7 @@
// option1.h : Option 1 (Standard): Console Text Editor.
// Option 1: Console Text Editor
// Uses a doubly linked list for storing document lines,
// two stacks for undo/redo operations,
// and a queue for print-job management.
#pragma once

View File

@@ -3,7 +3,19 @@
#include "SharedLib.h"
TDoublyLinkedList::~TDoublyLinkedList()
{
const Node* cur = head;
while (cur) {
const Node* next = cur->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);

View File

@@ -2,13 +2,16 @@
#define TDOUBLYLINKEDLIST_H
#include <string>
#include <utility>
#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;

View File

@@ -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();

View File

@@ -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;

View File

@@ -5,11 +5,11 @@
#include <ctime>
#include <iostream>
#include <limits>
#include <numbers>
// 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" <<std::endl;