Cleaning up and adding comments
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
// Option 1 (Standard): Console Text Editor.
|
// 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 "option1.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -16,6 +17,9 @@ bool running = true;
|
|||||||
int lastIndex = 0;
|
int lastIndex = 0;
|
||||||
std::string deletedLine;
|
std::string deletedLine;
|
||||||
|
|
||||||
|
// Undo the last text modification.
|
||||||
|
// Reverses INSERT/DELETE actions by applying the inverse operation.
|
||||||
|
// Moves reversed action into redoStack.
|
||||||
void Undo()
|
void Undo()
|
||||||
{
|
{
|
||||||
if (!undoStack.IsEmpty()) {
|
if (!undoStack.IsEmpty()) {
|
||||||
@@ -29,6 +33,10 @@ void Undo()
|
|||||||
redoStack.Push(action);
|
redoStack.Push(action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Redo the last undone modification.
|
||||||
|
// Re-applies an action previously undone.
|
||||||
|
// Pushes the executed action back into undoStack.
|
||||||
void Redo()
|
void Redo()
|
||||||
{
|
{
|
||||||
if (!redoStack.IsEmpty()) {
|
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()
|
int RunApp()
|
||||||
{
|
{
|
||||||
// Implement the Console Text Editor application logic here
|
// Implement the Console Text Editor application logic here
|
||||||
@@ -62,31 +72,56 @@ int RunApp()
|
|||||||
}
|
}
|
||||||
case 3: {
|
case 3: {
|
||||||
std::cout << "----------Current document----------" << std::endl;
|
std::cout << "----------Current document----------" << std::endl;
|
||||||
for (int i = 0; i < document.GetSize(); i++)
|
for (int i = 0; i < document.GetSize(); i++) {
|
||||||
std::cout << i + 1 << ". " << document.GetAtIndex(i) << std::endl;
|
std::cout << document.GetAtIndex(i) << std::endl;
|
||||||
std::cout << "------------------------------------\n\n";
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build a single print job containing the entire document.
|
||||||
|
// Snapshot is stored as a single string and enqueued.
|
||||||
case 4: {
|
case 4: {
|
||||||
for (int i = 0; i < document.GetSize(); ++i)
|
std::cout << "----------Add print job----------" << std::endl;
|
||||||
printQueue.Enqueue(document.GetAtIndex(i));
|
|
||||||
|
|
||||||
std::cout << "----------Printing queue-----------" << std::endl;
|
|
||||||
|
|
||||||
while (!printQueue.IsEmpty())
|
|
||||||
std::cout << printQueue.Dequeue() << std::endl;
|
|
||||||
std::cout << "------------------------------------\n\n";
|
|
||||||
|
|
||||||
|
if (document.GetSize() == 0) {
|
||||||
|
std::cout << "Document is empty - nothing added to print queue." << std::endl;
|
||||||
break;
|
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;
|
std::cout << "----------UNDO----------" <<std::endl;
|
||||||
Undo();
|
Undo();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 6: {
|
case 7: {
|
||||||
std::cout << "----------REDO----------" <<std::endl;
|
std::cout << "----------REDO----------" <<std::endl;
|
||||||
Redo();
|
Redo();
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -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
|
#pragma once
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,19 @@
|
|||||||
#include "SharedLib.h"
|
#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)
|
void TDoublyLinkedList::Append(const std::string& line)
|
||||||
{
|
{
|
||||||
auto* newNode = new Node(line);
|
auto* newNode = new Node(line);
|
||||||
@@ -17,6 +29,7 @@ void TDoublyLinkedList::Append(const std::string& line)
|
|||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert a new line at the beginning.
|
||||||
void TDoublyLinkedList::Prepend(const std::string& line)
|
void TDoublyLinkedList::Prepend(const std::string& line)
|
||||||
{
|
{
|
||||||
auto* newNode = new Node(line);
|
auto* newNode = new Node(line);
|
||||||
@@ -43,6 +56,7 @@ TDoublyLinkedList::Node* TDoublyLinkedList::NavigateToNode(const int index) cons
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Removes the node at the given index and updates head/tail if needed.
|
||||||
void TDoublyLinkedList::Remove(const int index)
|
void TDoublyLinkedList::Remove(const int index)
|
||||||
{
|
{
|
||||||
auto* node = NavigateToNode(index);
|
auto* node = NavigateToNode(index);
|
||||||
|
|||||||
@@ -2,13 +2,16 @@
|
|||||||
#define TDOUBLYLINKEDLIST_H
|
#define TDOUBLYLINKEDLIST_H
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#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 {
|
class TDoublyLinkedList {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// Internal node storing a single line of text
|
||||||
|
// and links to previous and next nodes.
|
||||||
struct Node {
|
struct Node {
|
||||||
std::string line;
|
std::string line;
|
||||||
Node* next;
|
Node* next;
|
||||||
@@ -45,12 +48,21 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
TDoublyLinkedList() : head(nullptr), tail(nullptr), size(0) {}
|
TDoublyLinkedList() : head(nullptr), tail(nullptr), size(0) {}
|
||||||
~TDoublyLinkedList() = default;
|
~TDoublyLinkedList();
|
||||||
|
|
||||||
void Append(const std::string &line);
|
void Append(const std::string &line);
|
||||||
void Prepend(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;
|
[[nodiscard]] Node* NavigateToNode(int index) const;
|
||||||
|
|
||||||
|
// Removes a node at the given index.
|
||||||
|
// Updates links and frees the removed node.
|
||||||
void Remove(int index);
|
void Remove(int index);
|
||||||
|
|
||||||
|
// Returns the text stored at the given index.
|
||||||
|
// Uses NavigateToNode internally.
|
||||||
[[nodiscard]] std::string GetAtIndex(int index) const;
|
[[nodiscard]] std::string GetAtIndex(int index) const;
|
||||||
void InsertAtIndex(int index, const std::string &line);
|
void InsertAtIndex(int index, const std::string &line);
|
||||||
[[nodiscard]] int GetSize() const;
|
[[nodiscard]] int GetSize() const;
|
||||||
|
|||||||
@@ -10,8 +10,13 @@ enum EnumActionType {
|
|||||||
DELETE
|
DELETE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Simple fixed-size stack used for undo/redo.
|
||||||
|
// Stores actions describing line insert/delete operations.
|
||||||
class TStack {
|
class TStack {
|
||||||
private:
|
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 {
|
struct TAction {
|
||||||
EnumActionType action;
|
EnumActionType action;
|
||||||
std::string text;
|
std::string text;
|
||||||
@@ -24,9 +29,8 @@ private:
|
|||||||
public:
|
public:
|
||||||
TStack() = default;
|
TStack() = default;
|
||||||
~TStack() = default;
|
~TStack() = default;
|
||||||
|
void Push(const TAction& action); // Adds a new action to the top of the stack.
|
||||||
void Push(const TAction& action);
|
TAction Pop(); // Removes and returns the most recent action.
|
||||||
TAction Pop();
|
|
||||||
[[nodiscard]] TAction Peek() const;
|
[[nodiscard]] TAction Peek() const;
|
||||||
[[nodiscard]] bool IsEmpty() const;
|
[[nodiscard]] bool IsEmpty() const;
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|||||||
@@ -5,7 +5,9 @@
|
|||||||
#include "TDoublyLinkedList.h"
|
#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 {
|
class TTreeQueue {
|
||||||
private:
|
private:
|
||||||
std::string queue[MAX_SIZE];
|
std::string queue[MAX_SIZE];
|
||||||
@@ -17,8 +19,8 @@ public:
|
|||||||
TTreeQueue() = default;
|
TTreeQueue() = default;
|
||||||
~TTreeQueue() = default;
|
~TTreeQueue() = default;
|
||||||
|
|
||||||
void Enqueue(const std::string& text);
|
void Enqueue(const std::string& text); // Adds a new job at the tail of the queue.
|
||||||
std::string Dequeue();
|
std::string Dequeue(); // Removes and returns the next job in FIFO order.
|
||||||
[[nodiscard]] int GetTail() const;
|
[[nodiscard]] int GetTail() const;
|
||||||
[[nodiscard]] std::string Peek() const;
|
[[nodiscard]] std::string Peek() const;
|
||||||
[[nodiscard]] bool IsEmpty() const;
|
[[nodiscard]] bool IsEmpty() const;
|
||||||
|
|||||||
@@ -5,11 +5,11 @@
|
|||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <numbers>
|
|
||||||
|
|
||||||
|
// Displays the main menu and reads user choice.
|
||||||
int Utils::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: ";
|
"\n\nChoice: ";
|
||||||
int choice;
|
int choice;
|
||||||
std::cin >> choice;
|
std::cin >> choice;
|
||||||
@@ -17,6 +17,9 @@ int Utils::Choice()
|
|||||||
return 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)
|
int Utils::Insert(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < document.GetSize(); i++) {
|
for (int i = 0; i < document.GetSize(); i++) {
|
||||||
@@ -58,6 +61,8 @@ void Utils::PrintList(const TDoublyLinkedList &document)
|
|||||||
std::cout << "\n\n";
|
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)
|
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;
|
std::cout << "Enter the number of the line you want to remove" <<std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user