Completed part 2
This commit is contained in:
@@ -3,7 +3,9 @@
|
||||
<component name="MaterialThemeProjectNewConfig">
|
||||
<option name="metadata">
|
||||
<MTProjectMetadataState>
|
||||
<option name="userId" value="54f3be3a:19a5567208e:-7fff" />
|
||||
<option name="migrated" value="true" />
|
||||
<option name="pristineConfig" value="false" />
|
||||
<option name="userId" value="54f3be3a:19a5567208e:-7ffe" />
|
||||
</MTProjectMetadataState>
|
||||
</option>
|
||||
</component>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -4,8 +4,98 @@
|
||||
|
||||
#ifndef OPTION1_H
|
||||
#define OPTION1_H
|
||||
#include "TLinkedList.h"
|
||||
#include "TPerson.h"
|
||||
|
||||
inline TLinkedList g, e;
|
||||
inline int guestCount, employCount = 0;
|
||||
|
||||
|
||||
|
||||
int RunApp();
|
||||
|
||||
|
||||
inline void printline()
|
||||
{
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
}
|
||||
|
||||
inline void pack(const std::string& line)
|
||||
{
|
||||
std::cout << "\n\n\n" << std::endl;
|
||||
printline();
|
||||
std::cout << line << std::endl;
|
||||
printline();
|
||||
std::cout << "\n\n\n" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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, const 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);
|
||||
employCount++;
|
||||
}
|
||||
else {
|
||||
g.Append(p);
|
||||
guestCount++;
|
||||
}
|
||||
|
||||
std::cout << "[" <<aIndex << "] " << aLastName << ", " << aFirstName << " | status: " << (status == 1 ? "Employee" : "Guest")
|
||||
<< " | cabin size: " << p.cabinSize << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
inline void SearchAndPrint(TPerson** targetArray, int arraySize, const std::string& target)
|
||||
{
|
||||
int index = Utils::BinarySearch(targetArray, 0, arraySize - 1, target);
|
||||
|
||||
if (index == -1) {
|
||||
std::cout << "No match found" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
int left = index - 1;
|
||||
int right = index + 1;
|
||||
|
||||
while (left >= 0 && (targetArray[left]->firstName == target || targetArray[left]->lastName == target)) {
|
||||
--left;
|
||||
}
|
||||
|
||||
while (right < arraySize && (targetArray[right]->firstName == target || targetArray[right]->lastName == target)) {
|
||||
++right;
|
||||
}
|
||||
|
||||
for (int i = left + 1; i < right; ++i) {
|
||||
std::cout << "Match found: \nName: " << targetArray[i]->firstName << " "
|
||||
<< targetArray[i]->lastName << " | status: " << (targetArray[i]->status == 0 ? "Guest" : "Employee")
|
||||
<< " | cabinsize: " << targetArray[i]->cabinSize << "\n" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // OPTION1_H
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
|
||||
TLinkedList::~TLinkedList()
|
||||
{
|
||||
const Node* cur = head;
|
||||
Node* cur = head;
|
||||
while (cur) {
|
||||
const Node* next = cur->next;
|
||||
Node* next = cur->next;
|
||||
delete cur;
|
||||
cur = next;
|
||||
}
|
||||
head = nullptr;
|
||||
tail = nullptr;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
void TLinkedList::Append(const TPerson& person)
|
||||
@@ -92,8 +95,7 @@ TPerson TLinkedList::GetAtIndex(const int index) const
|
||||
{
|
||||
if (index < 0 || index >= size) {
|
||||
std::cout << "Index out of range\n";
|
||||
// return dummy
|
||||
return {"N/A", "N/A", GUEST};
|
||||
return {};
|
||||
}
|
||||
|
||||
Node* cur = head;
|
||||
@@ -147,6 +149,8 @@ TLinkedList::Node *TLinkedList::MergeList(Node *a, Node *b)
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Time complexity O(n log n) at all times
|
||||
/// Does NOT sort in place, so more memory is needed to complete
|
||||
TLinkedList::Node *TLinkedList::MergeSort(Node *head)
|
||||
{
|
||||
if (head == nullptr || head->next == nullptr)
|
||||
@@ -160,8 +164,6 @@ TLinkedList::Node *TLinkedList::MergeSort(Node *head)
|
||||
front = MergeSort(front);
|
||||
back = MergeSort(back);
|
||||
return MergeList(front, back);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void TLinkedList::Sort()
|
||||
@@ -175,13 +177,4 @@ void TLinkedList::Sort()
|
||||
tail = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
#include "TPerson.h"
|
||||
|
||||
#include <utility>
|
||||
#include "Utils.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
TPerson::TPerson() : firstName("N/A"),lastName("N/A"), status(GUEST), cabinSize(Utils::RandomInt(1, 4)) {}
|
||||
|
||||
TPerson::TPerson(std::string f, std::string l, const ENumStatus s) : firstName(std::move(f)), lastName(std::move(l)), status(s), cabinSize(Utils::RandomInt(1, 4)) {}
|
||||
|
||||
|
||||
|
||||
@@ -14,9 +14,10 @@ struct TPerson {
|
||||
std::string firstName;
|
||||
std::string lastName;
|
||||
ENumStatus status;
|
||||
int cabinSize = Utils::RandomInt(1, 4);
|
||||
int cabinSize{};
|
||||
|
||||
TPerson(std::string f, std::string l, ENumStatus s) : firstName(std::move(f)), lastName(std::move(l)), status(s){}
|
||||
TPerson();
|
||||
TPerson(std::string , std::string , ENumStatus);
|
||||
~TPerson() = default;
|
||||
|
||||
bool operator<(const TPerson& other) const
|
||||
@@ -26,6 +27,7 @@ struct TPerson {
|
||||
// same last name → compare first name
|
||||
return firstName < other.firstName;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include "Utils.h"
|
||||
|
||||
#include "TDoublyLinkedList.h"
|
||||
#include "TStack.h"
|
||||
#include "TPerson.h"
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
#include "TDoublyLinkedList.h"
|
||||
#include "TStack.h"
|
||||
|
||||
#include <numbers>
|
||||
|
||||
int Utils::Choice()
|
||||
{
|
||||
@@ -15,7 +14,6 @@ int Utils::Choice()
|
||||
int choice;
|
||||
std::cin >> choice;
|
||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
//std::cout << "\n=====================\n";
|
||||
return choice;
|
||||
}
|
||||
|
||||
@@ -89,5 +87,78 @@ int Utils::RandomInt(const int min, const int max)
|
||||
if (max <= min)
|
||||
return 0;
|
||||
|
||||
return min + rand() % (max - min + 1); // <---- Limited randomness, but again
|
||||
return min + rand() % (max - min + 1); // <---- Limited randomness, but again
|
||||
} // sufficient for this use case
|
||||
|
||||
bool Utils::CompareLastnames(const TPerson *a, const TPerson *b)
|
||||
{
|
||||
if (a->cabinSize < b->cabinSize)
|
||||
return true;
|
||||
if (a->cabinSize > b->cabinSize)
|
||||
return false;
|
||||
return a->lastName < b->lastName;
|
||||
}
|
||||
|
||||
|
||||
int Utils::Partition(TPerson **arr, const int startIndex, const int endIndex)
|
||||
{
|
||||
TPerson *pivot = arr[endIndex];
|
||||
int i = startIndex - 1;
|
||||
|
||||
for (int j = startIndex; j < endIndex; j++) {
|
||||
if (CompareLastnames(arr[j], pivot)) {
|
||||
i++;
|
||||
std::swap(arr[i], arr[j]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::swap(arr[i + 1], arr[endIndex]);
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
/// Time complexity **on average** is O(n log n) but worst case it O(n^2)
|
||||
/// depending on where in the range the pivot lands -- If pivot is at either extreme
|
||||
/// the algorithm has to search through the entire list for every value it sorts -- n^2
|
||||
/// However it does sort in-place, meaning no extra memory is needed
|
||||
void Utils::QuickSort(TPerson** arr, const int low, const int high)
|
||||
{
|
||||
if (low < high) {
|
||||
int p = Partition(arr, low, high);
|
||||
QuickSort(arr, low, p - 1);
|
||||
QuickSort(arr, p + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
/// Time complexity of the binary search is O(log n)
|
||||
/// However the included fallback search is O(n)
|
||||
int Utils::BinarySearch(TPerson** arr, int p1, int p2, const std::string &target)
|
||||
{
|
||||
const int origStart = p1;
|
||||
const int origEnd = p2;
|
||||
|
||||
|
||||
while (p1 <= p2) {
|
||||
const int newP = (p1 + p2) / 2;
|
||||
std::string currentFirst = arr[newP]->firstName;
|
||||
std::string currentLast = arr[newP]->lastName;
|
||||
|
||||
if (target == currentFirst || target == currentLast)
|
||||
return newP;
|
||||
|
||||
if (target > currentLast)
|
||||
p1 = newP + 1;
|
||||
|
||||
else
|
||||
p2 = newP - 1;
|
||||
}
|
||||
|
||||
/// Extra to search for firstname in the event that no matches were found
|
||||
/// Disregard this section if you're purely looking at the
|
||||
/// binary search understanding and implementation
|
||||
for (int i = origStart; i <= origEnd; i++) {
|
||||
if (arr[i]->firstName == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
#include "TDoublyLinkedList.h"
|
||||
#include "TPerson.h"
|
||||
#include "TStack.h"
|
||||
|
||||
struct TPerson;
|
||||
|
||||
class Utils {
|
||||
public:
|
||||
@@ -11,16 +13,11 @@ class Utils {
|
||||
static void PrintList(const TDoublyLinkedList &document);
|
||||
static int RemoveLine(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index);
|
||||
static int RandomInt(int, int);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static int Partition(TPerson** arr, int startIndex, int endIndex);
|
||||
static void QuickSort(TPerson**, int, int);
|
||||
static bool CompareLastnames(const TPerson*, const TPerson*);
|
||||
static int BinarySearch(TPerson**, int, int, const std::string&);
|
||||
static int CountMatches(TPerson**, int, const std::string&);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user