Completed part 2

This commit is contained in:
Christopher Sanden
2025-11-06 22:48:14 +01:00
parent 8d533b9131
commit e761ac0e23
8 changed files with 282 additions and 100 deletions

View File

@@ -4,44 +4,14 @@
#include <iostream>
#include <string>
#include "option1.h"
#include <limits>
#include "SharedLib.h"
#include "TLinkedList.h"
#include "TPerson.h"
TLinkedList g, e;
/**
* @brief Callback function to process one name.
static bool NameReadCallback(const int aIndex, const int aTotalCount, const std::string& aFirstName, const std::string& aLastName)
{
std::cout << "Reading Name " << (aIndex + 1) << " of " << aTotalCount << ": "
<< aFirstName << " " << aLastName << "\n";
// We only want to read 10 names (index 0 through 9)
// Return false when aIndex is 9 to stop the loop after this one.
return (aIndex < 9);
}
*/
// *Inspired* by the provided NameReadCallback given above
static bool onNameRead(const int aIndex, int aTotalCount, const std::string& aFirstName, const std::string& aLastName)
{
const ENumStatus status = (aIndex < 1500) ? EMPLOYEE : GUEST;
const TPerson p(aFirstName, aLastName, status);
if (status == EMPLOYEE)
e.Append(p);
else
g.Append(p);
std::cout << "[" <<aIndex << "] " << aLastName << ", " << aFirstName << " | status: " << (status == 1 ? "Employee" : "Guest")
<< " | cabin size: " << p.cabinSize << std::endl;
std::cout << "---------------------------------------------------------------" << std::endl;
return true;
}
// Assignment specific helpers in option1.h
int RunApp()
@@ -52,39 +22,99 @@ int RunApp()
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";
std::cout << "Reading names and grouping them: " << "\n" << std::endl;
pack("Reading names and grouping them.");
// Call the utility function with the name callback
readNamesFromFile(filename, onNameRead);
std::cout << "\nFinished reading names.\n\nSorting." << std::endl;
pack("Finished reading names.");
// Merge sorting
/////////////////////////// Merge sorting ///////////////////////////
e.Sort();
g.Sort();
pack("Sorting.");
// Attempt at "beautifying" the terminal output somewhat
std::cout << "\n\n\n---------------------------------------------------------------" << std::endl;
std::cout << "Employees merge sorted alphabetically" << std::endl;
std::cout << "---------------------------------------------------------------" << std::endl;
pack("Employees merge sorted alphabetically.");
TPerson* employeeAlphaSort[e.GetSize()];
printline();
for (int i = 0; i < e.GetSize(); i++) {
std::cout << "[" << i << "] " << e.GetAtIndex(i).lastName << ", " << e.GetAtIndex(i).firstName
<< " | status: Employee | cabin size: " << e.GetAtIndex(i).cabinSize << std::endl;
std::cout << "---------------------------------------------------------------" << std::endl;
//std::cout << "[" << i << "] " << e.GetAtIndex(i).lastName << ", " << e.GetAtIndex(i).firstName
//<< " | status: Employee | cabin size: " << e.GetAtIndex(i).cabinSize << std::endl;
employeeAlphaSort[i] = new TPerson(e.GetAtIndex(i));
}
std::cout << "\n\n\n---------------------------------------------------------------" << std::endl;
std::cout << "Guests merger sorted alphabetically" << std::endl;
std::cout << "---------------------------------------------------------------" << std::endl;
printline();
pack("Guests merger sorted alphabetically.");
TPerson* guestAlphaSort[g.GetSize()];
printline();
for (int i = 0; i < g.GetSize(); i++) {
std::cout << "[" << i << "] " << g.GetAtIndex(i).lastName << ", " << g.GetAtIndex(i).firstName
<< " | status: Guest | cabin size: " << g.GetAtIndex(i).cabinSize << std::endl;
std::cout << "---------------------------------------------------------------" << std::endl;
//std::cout << "[" << i << "] " << g.GetAtIndex(i).lastName << ", " << g.GetAtIndex(i).firstName
//<< " | status: Guest | cabin size: " << g.GetAtIndex(i).cabinSize << std::endl;
guestAlphaSort[i] = new TPerson(g.GetAtIndex(i));
}
printline();
/////////////////////////// Quick sorting ///////////////////////////
// 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.");
printline();
for (int i = 0; i < guestCount; i++) {
std::cout << "[" << i << "] " << guestList[i]->lastName << ", " << guestList[i]->firstName
<< " | cabinSize: " << guestList[i]->cabinSize << std::endl;
}
printline();
/////////////////////////// Binary search ///////////////////////////
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::getline(std::cin, target);
switch (choice) {
case 1: SearchAndPrint(employeeAlphaSort, employCount, target); break;
case 2: SearchAndPrint(guestAlphaSort, guestCount, target); break;
default: {
std::cout << "Choice invalid" << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
/////////////////////////// Cleanup before exit ///////////////////////////
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;
}