pretty output for list and search

This commit is contained in:
Iurii Tatishchev 2024-06-18 18:22:41 -07:00
parent 62016d94a3
commit a2746fe6de
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369
7 changed files with 166 additions and 38 deletions

View File

@ -1,6 +1,7 @@
#ifndef INC_08_TEAM_PROJECT_BINARYTREE_H
#define INC_08_TEAM_PROJECT_BINARYTREE_H
#include <functional>
#include "BinaryTreeNode.h"
template<typename T>
@ -23,7 +24,7 @@ public:
void preOrder(void visit(const T&)) const { _preorder(visit, root); }
void inOrder(void visit(const T&)) const { _inorder(visit, root); }
void inOrder(std::function<void(const T&)> visit) const { _inorder(visit, root); }
void postOrder(void visit(const T&)) const { _postorder(visit, root); }
@ -41,7 +42,7 @@ private:
// internal traverse
void _preorder(void visit(const T&), BinaryTreeNode<T>* nodePtr) const;
void _inorder(void visit(const T&), BinaryTreeNode<T>* nodePtr) const;
void _inorder(std::function<void(const T&)> visit, BinaryTreeNode<T>* nodePtr) const;
void _postorder(void visit(const T&), BinaryTreeNode<T>* nodePtr) const;
@ -72,7 +73,7 @@ void BinaryTree<T>::_preorder(void visit(const T&), BinaryTreeNode<T>* nodePtr)
}
template<typename T>
void BinaryTree<T>::_inorder(void visit(const T&), BinaryTreeNode<T>* nodePtr) const
void BinaryTree<T>::_inorder(std::function<void(const T&)> visit, BinaryTreeNode<T>* nodePtr) const
{
if (nodePtr) // != NULL
{

81
CPU.cpp
View File

@ -1,7 +1,86 @@
#include <iostream>
#include "CPU.h"
#include "utils.h"
using std::string, std::ostream, std::endl;
using std::string, std::ostream, std::endl, std::cout;
void display(const CPU &cpu) {
// CPU ID
// width is different because bold characters are counted but invisible
string boldCenteredCpuId = centeredStr(boldStr(cpu.cpuId), 46);
printTableHeader({39}, {boldCenteredCpuId});
static const vector<int> widths = {18, 20};
// Release Year
cout << "| ";
cout.width(widths[0] + 7);
cout << std::left << boldStr("Release Year");
cout << "|";
cout.width(widths[1] - 1);
cout << std::right << cpu.releaseYear;
cout << " |\n";
// Core Count
cout << "| ";
cout.width(widths[0] + 7);
cout << std::left << boldStr("Core Count");
cout << "|";
cout.width(widths[1] - 1);
cout << std::right << cpu.coreCount;
cout << " |\n";
// Architecture
cout << "| ";
cout.width(widths[0] + 7);
cout << std::left << boldStr("Architecture");
cout << "|";
cout.width(widths[1] - 1);
cout << std::right << cpu.architecture;
cout << " |\n";
// Base Clock
cout << "| ";
cout.width(widths[0] + 7);
cout << std::left << boldStr("Base Clock (GHz)");
cout << "|";
cout.width(widths[1] - 1);
cout.precision(2);
cout << std::fixed << std::right << cpu.baseClock;
cout << " |\n";
printTableFooter(widths);
}
void iDisplay(const CPU &cpu, int level) {
for (int i = 0; i < level; i++)
cout << " ";
cout << cpu.cpuId << std::endl;
}
void rowDisplay(const CPU &cpu, const std::vector<int> &widths) {
cout << "| ";
cout.width(widths[0] - 1);
cout << std::left << cpu.cpuId;
cout << "|";
cout.width(widths[1] - 1);
cout << std::right << cpu.releaseYear;
cout << " |";
cout.width(widths[2] - 1);
cout << std::right << cpu.coreCount;
cout << " | ";
cout.width(widths[3] - 1);
cout << std::left << cpu.architecture;
cout << "|";
cout.width(widths[4] - 1);
cout.precision(2);
cout << std::fixed << std::right << cpu.baseClock;
cout << " |\n";
}
ostream &operator<<(ostream &os, const CPU &cpu) {
os << "CPU ID: " << cpu.cpuId << std::endl;

17
CPU.h
View File

@ -2,6 +2,7 @@
#define INC_08_TEAM_PROJECT_CPU_H
#include <string>
#include <vector>
class CPU {
private:
@ -55,13 +56,13 @@ public:
};
// Friend function declarations
friend void display(const CPU &cpu);
friend void iDisplay(const CPU &cpu, int level) {
for (int i = 1; i < level; i++)
std::cout << "..";
std::cout << level << "). " << cpu.cpuId << std::endl;
};
friend void iDisplay(const CPU &cpu, int level);
// Display the CPU object as a table row
friend void rowDisplay(const CPU &cpu, const std::vector<int> &widths);
friend std::ostream &operator<<(std::ostream &os, const CPU &cpu);
@ -71,6 +72,12 @@ public:
};
void display(const CPU &cpu);
void iDisplay(const CPU &cpu, int level);
void rowDisplay(const CPU &cpu, const std::vector<int> &widths);
std::ostream &operator<<(std::ostream &os, const CPU &cpu);
/*~*~*~*

View File

@ -46,13 +46,27 @@ void DisplayManager<T>::displayAll() const {
template<typename T>
void DisplayManager<T>::displayTree() const {
std::cout << "CPU Binary Search Tree:" << std::endl;
std::cout << "All CPUs: " << std::endl;
static const std::vector<string> headers = {
"CPU ID",
"Release Year",
"Core Count",
"Architecture",
"Base Clock (GHz)"
};
static const std::vector<int> widths = {20, 14, 12, 20, 18};
printTableHeader(widths, headers);
// Call the BST's inorder traversal method to display the tree
// TODO: Use a proper display function
bst->inOrder([](const std::string &key) {
std::cout << key << std::endl;
bst->inOrder([this](const string &cpuId) {
CPU cpu;
cpu.setCpuId(cpuId);
this->hashTable->search(cpu, cpu, key_to_index);
rowDisplay(cpu, widths);
});
printTableFooter(widths);
}
#endif

View File

@ -24,7 +24,8 @@ template<typename T>
void SearchManager<T>::searchCPU() const {
std::string cpuID;
std::cout << "Enter the CPU ID to search: ";
std::cin >> cpuID;
std::cin.ignore();
getline(std::cin, cpuID);
// Search for the CPU in the HashTable
CPU foundCPU;
@ -33,7 +34,7 @@ void SearchManager<T>::searchCPU() const {
if (collisionCount != -1) {
std::cout << "CPU found:" << std::endl;
std::cout << foundCPU << std::endl;
display(foundCPU);
} else {
std::cout << "CPU not found." << std::endl;
}

View File

@ -1,8 +1,9 @@
#include <iostream>
#include "utils.h"
using std::cout;
void printRow(const vector<int> &widths, const vector<string>& row) {
void printRow(const vector<int> &widths, const vector<string> &row) {
cout << '|';
for (int i = 0; i < widths.size(); i++) {
cout << ' ';
@ -13,20 +14,10 @@ void printRow(const vector<int> &widths, const vector<string>& row) {
cout << '\n';
};
/*
* Print ascii/unicode table with given column widths and data. For example:
*
* | Col1 | Col2 | Col3 | Numeric Column |
* +========================================+==========+========================+================+
* | Value 1 | Value 2 | 123 | 10.0 |
* | Separate | cols | with a tab or 4 spaces | -2,027.1 |
* | This is a row with only one cell | | | |
*
*/
void printTable(const vector<int>& widths, const vector<vector<string>>& data) {
void printTableHeader(const vector<int> &widths, const vector<string> &headers) {
// Print top border
cout << "";
for (int width : widths) {
for (int width: widths) {
for (int i = 0; i < width; i++) {
cout << "";
}
@ -39,29 +30,26 @@ void printTable(const vector<int>& widths, const vector<vector<string>>& data) {
for (int i = 0; i < widths.size(); i++) {
cout << ' ';
cout.width(widths[i] - 1);
cout << std::left << data[0][i];
cout << std::left << headers[i];
cout << '|';
}
cout << '\n';
// Print middle border
cout << '+';
for (int width : widths) {
for (int width: widths) {
for (int i = 0; i < width; i++) {
cout << '=';
}
cout << '+';
}
cout << '\n';
}
// Print data
for (int i = 1; i < data.size(); i++) {
printRow(widths, data[i]);
}
void printTableFooter(const vector<int> &widths) {
// Print bottom border
cout << "";
for (int width : widths) {
for (int width: widths) {
for (int i = 0; i < width; i++) {
cout << "";
}
@ -70,6 +58,24 @@ void printTable(const vector<int>& widths, const vector<vector<string>>& data) {
cout << "\b\n";
}
/*
* Print ascii/unicode table with given column widths and data. For example:
*
* | Col1 | Col2 | Col3 | Numeric Column |
* +========================================+==========+========================+================+
* | Value 1 | Value 2 | 123 | 10.0 |
* | Separate | cols | with a tab or 4 spaces | -2,027.1 |
* | This is a row with only one cell | | | |
*
*/
void printTable(const vector<int> &widths, const vector<vector<string>> &data) {
printTableHeader(widths, data[0]);
for (int i = 1; i < data.size(); i++) {
printRow(widths, data[i]);
}
printTableFooter(widths);
}
void printHelp() {
static const vector<int> helpWidths = {5, 40};
static const vector<vector<string>> helpData = {
@ -142,3 +148,14 @@ int findNextPrime(int n) {
return n;
}
string boldStr(const string &str) {
return "\033[1m" + str + "\033[0m";
}
string centeredStr(const string &str, int width) {
int padding = width - str.length();
int leftPadding = padding / 2;
int rightPadding = padding - leftPadding;
return string(leftPadding, ' ') + str + string(rightPadding, ' ');
}

13
utils.h
View File

@ -3,11 +3,16 @@
#include <vector>
#include <string>
using std::vector, std::string;
void printRow(const vector<int> &widths, const vector<string>& row);
void printRow(const vector<int> &widths, const vector<string> &row);
void printTable(const vector<int>& widths, const vector<vector<string>>& data);
void printTableHeader(const vector<int> &widths, const vector<string> &headers);
void printTableFooter(const vector<int> &widths);
void printTable(const vector<int> &widths, const vector<vector<string>> &data);
void printHelp();
@ -15,4 +20,8 @@ void printTeam();
int findNextPrime(int n);
string boldStr(const string &str);
string centeredStr(const string &str, int width);
#endif //INC_08_TEAM_PROJECT_UTILS_H