Cleaning up and adding comments

This commit is contained in:
Christopher Sanden
2025-11-18 13:23:54 +01:00
parent d1fa8eda6b
commit e77d7ff21e
14 changed files with 202 additions and 103 deletions

View File

@@ -1,43 +1,38 @@
// Option 1 (Standard): Console Text Editor.
//
#include <iostream>
#include <string>
#include "option1.h"
#include <limits>
#include "SharedLib.h"
#include "TLinkedList.h"
#include "TPerson.h"
// Assignment specific helpers in option1.h
// Entry point for Category 2, Option 1 (Cruise Ship Manifest).
// Steps:
// 1) Load names from DATA/random_names.txt into employee and guest lists
// 2) Merge-sort both lists alphabetically (lastName, firstName)
// 3) Convert guests to an array and quick-sort by cabinSize, then lastName
// 4) Allow the user to search (binary search) by surname in the chosen list
int RunApp()
{
/* Path to the names data file
This is MY absolute path -- change to your local path for this to read properly
something like "C:\Users\Username\FolderYouSavedTheSubmissionIn\Exam\IKT203Exam\DATA\random_names.txt"
Double slash is needed for string to pass the correct file path */
const std::string filename = "C:\\Users\\csand\\IKT203\\Exam\\IKT203Exam\\DATA\\random_names.txt";
// Path to the names data file.
// IMPORTANT: working directory must be set so that "DATA/random_names.txt" resolves correctly.
const std::string filename = "DATA/random_names.txt";
pack("Reading names and grouping them.");
// Call the utility function with the name callback
readNamesFromFile(filename, onNameRead);
pack("Finished reading names.");
/////////////////////////// Merge sorting ///////////////////////////
// Sort both employee and guest linked lists alphabetically
// using the linked-list merge sort implementation in TLinkedList.
e.Sort();
g.Sort();
pack("Sorting.");
// Attempt at "beautifying" the terminal output somewhat
pack("Employees merge sorted alphabetically.");
TPerson* employeeAlphaSort[e.GetSize()];
const int employeeSize = e.GetSize();
auto** employeeAlphaSort = new TPerson*[employeeSize];
printline();
for (int i = 0; i < e.GetSize(); i++) {
std::cout << "[" << i << "] " << e.GetAtIndex(i).lastName << ", " << e.GetAtIndex(i).firstName
@@ -45,8 +40,9 @@ int RunApp()
employeeAlphaSort[i] = new TPerson(e.GetAtIndex(i));
}
printline();
pack("Guests merger sorted alphabetically.");
TPerson* guestAlphaSort[g.GetSize()];
pack("Guests merge sorted alphabetically.");
const int guestSize = g.GetSize();
auto** guestAlphaSort = new TPerson*[guestSize];
printline();
for (int i = 0; i < g.GetSize(); i++) {
std::cout << "[" << i << "] " << g.GetAtIndex(i).lastName << ", " << g.GetAtIndex(i).firstName
@@ -55,15 +51,15 @@ int RunApp()
}
printline();
/////////////////////////// Quick sorting ///////////////////////////
// Build an array of guests and quick-sort it by:
// 1) cabinSize (ascending), then 2) lastName.
// This array is used to optimise cabin assignment.
// creating array from guest linked list
auto** guestList = new TPerson*[guestCount];
for (int i = 0; i < guestCount; i++) {
guestList[i] = new TPerson(g.GetAtIndex(i));
}
// Quicksorting the guestlist array
Utils::QuickSort(guestList, 0, guestCount - 1);
pack("Guests quick sorted by 1) cabinsize, 2) lastname.");
@@ -74,19 +70,16 @@ int RunApp()
}
printline();
/////////////////////////// Binary search ///////////////////////////
// Let the user choose whether to search employees or guests,
// then perform binary search on the corresponding alphabetically
// sorted array and print all matches with that surname.
int choice;
std::string target;
std::cout << "What list do you want to search through: \n [1] Employee\n [2] Guest" << std::endl;
std::cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter surname to search: " << std::endl;
std::cout << "Enter name to search for: " << std::endl;
std::getline(std::cin, target);
switch (choice) {
@@ -100,21 +93,31 @@ switch (choice) {
}
}
/////////////////////////// Cleanup before exit ///////////////////////////
for (int i = 0; i < guestCount; i++) {
// Delete all dynamically allocated TPerson objects from:
// - alphabetical employee array
// - alphabetical guest array
// - quick-sorted guestList array
// Then clear the linked lists to avoid memory leaks.
for (int i = 0; i < employeeSize; ++i)
delete employeeAlphaSort[i];
delete[] employeeAlphaSort;
for (int i = 0; i < guestSize; ++i)
delete guestAlphaSort[i];
delete[] guestAlphaSort;
for (int i = 0; i < guestCount; ++i)
delete guestList[i];
}
delete[] guestList;
while (e.GetSize() > 0)
e.Remove(0);
while (g.GetSize() > 0)
g.Remove(0);
pack("Cleaned up memory");
return 0;
return 0;
}