Cleaning up and adding comments
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
// option1.h : Option 1 (Standard): Console Text Editor.
|
||||
// Option 1 (Standard): Cruise Ship Manifest.
|
||||
// Uses linked lists, merge sort, quick sort, and binary search
|
||||
// to manage guest and employee manifests from random_names.txt.
|
||||
|
||||
#pragma once
|
||||
|
||||
@@ -7,8 +9,13 @@
|
||||
#include "TLinkedList.h"
|
||||
#include "TPerson.h"
|
||||
|
||||
// Global lists and counters used across the assignment:
|
||||
// - 'e' stores EMPLOYEE records
|
||||
// - 'g' stores GUEST records
|
||||
// - guestCount / employCount track how many were loaded
|
||||
inline TLinkedList g, e;
|
||||
inline int guestCount, employCount = 0;
|
||||
inline int guestCount = 0;
|
||||
inline int employCount = 0;
|
||||
|
||||
|
||||
|
||||
@@ -42,9 +49,13 @@ static bool NameReadCallback(const int aIndex, const int aTotalCount, const std:
|
||||
}
|
||||
*/
|
||||
|
||||
// *Inspired* by the provided NameReadCallback given above
|
||||
// Callback used by readNamesFromFile.
|
||||
// - Creates a TPerson with status (EMPLOYEE or GUEST)
|
||||
// - First 1500 entries are EMPLOYEE, the rest are GUEST
|
||||
// - Appends each person to the appropriate linked list and updates counters
|
||||
static bool onNameRead(const int aIndex, const int aTotalCount, const std::string& aFirstName, const std::string& aLastName)
|
||||
{
|
||||
// Determine status based on index: first 1500 are employees, rest are guests.
|
||||
const ENumStatus status = (aIndex < 1500) ? EMPLOYEE : GUEST;
|
||||
|
||||
const TPerson p(aFirstName, aLastName, status);
|
||||
@@ -64,7 +75,10 @@ static bool onNameRead(const int aIndex, const int aTotalCount, const std::strin
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Binary-search helper:
|
||||
// - Performs binary search on a sorted array of TPerson* (alphabetical by last name)
|
||||
// - 'target' is the surname entered by the user
|
||||
// - Expands left/right from the first match to find and print all matches
|
||||
inline void SearchAndPrint(TPerson** targetArray, int arraySize, const std::string& target)
|
||||
{
|
||||
int index = Utils::BinarySearch(targetArray, 0, arraySize - 1, target);
|
||||
@@ -77,10 +91,12 @@ inline void SearchAndPrint(TPerson** targetArray, int arraySize, const std::stri
|
||||
int left = index - 1;
|
||||
int right = index + 1;
|
||||
|
||||
// Move left while neighbouring entries share the same first or last name
|
||||
while (left >= 0 && (targetArray[left]->firstName == target || targetArray[left]->lastName == target)) {
|
||||
--left;
|
||||
}
|
||||
|
||||
// Move right while neighbouring entries share the same first or last name
|
||||
while (right < arraySize && (targetArray[right]->firstName == target || targetArray[right]->lastName == target)) {
|
||||
++right;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user