pretty output for list and search
This commit is contained in:
parent
62016d94a3
commit
a2746fe6de
@ -1,6 +1,7 @@
|
|||||||
#ifndef INC_08_TEAM_PROJECT_BINARYTREE_H
|
#ifndef INC_08_TEAM_PROJECT_BINARYTREE_H
|
||||||
#define INC_08_TEAM_PROJECT_BINARYTREE_H
|
#define INC_08_TEAM_PROJECT_BINARYTREE_H
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include "BinaryTreeNode.h"
|
#include "BinaryTreeNode.h"
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -23,7 +24,7 @@ public:
|
|||||||
|
|
||||||
void preOrder(void visit(const T&)) const { _preorder(visit, root); }
|
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); }
|
void postOrder(void visit(const T&)) const { _postorder(visit, root); }
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ private:
|
|||||||
// internal traverse
|
// internal traverse
|
||||||
void _preorder(void visit(const T&), BinaryTreeNode<T>* nodePtr) const;
|
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;
|
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>
|
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
|
if (nodePtr) // != NULL
|
||||||
{
|
{
|
||||||
|
81
CPU.cpp
81
CPU.cpp
@ -1,7 +1,86 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "CPU.h"
|
#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) {
|
ostream &operator<<(ostream &os, const CPU &cpu) {
|
||||||
os << "CPU ID: " << cpu.cpuId << std::endl;
|
os << "CPU ID: " << cpu.cpuId << std::endl;
|
||||||
|
17
CPU.h
17
CPU.h
@ -2,6 +2,7 @@
|
|||||||
#define INC_08_TEAM_PROJECT_CPU_H
|
#define INC_08_TEAM_PROJECT_CPU_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class CPU {
|
class CPU {
|
||||||
private:
|
private:
|
||||||
@ -55,13 +56,13 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Friend function declarations
|
// Friend function declarations
|
||||||
|
|
||||||
friend void display(const CPU &cpu);
|
friend void display(const CPU &cpu);
|
||||||
|
|
||||||
friend void iDisplay(const CPU &cpu, int level) {
|
friend void iDisplay(const CPU &cpu, int level);
|
||||||
for (int i = 1; i < level; i++)
|
|
||||||
std::cout << "..";
|
// Display the CPU object as a table row
|
||||||
std::cout << level << "). " << cpu.cpuId << std::endl;
|
friend void rowDisplay(const CPU &cpu, const std::vector<int> &widths);
|
||||||
};
|
|
||||||
|
|
||||||
friend std::ostream &operator<<(std::ostream &os, const CPU &cpu);
|
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);
|
std::ostream &operator<<(std::ostream &os, const CPU &cpu);
|
||||||
|
|
||||||
/*~*~*~*
|
/*~*~*~*
|
||||||
|
@ -46,13 +46,27 @@ void DisplayManager<T>::displayAll() const {
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void DisplayManager<T>::displayTree() const {
|
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
|
// Call the BST's inorder traversal method to display the tree
|
||||||
// TODO: Use a proper display function
|
bst->inOrder([this](const string &cpuId) {
|
||||||
bst->inOrder([](const std::string &key) {
|
CPU cpu;
|
||||||
std::cout << key << std::endl;
|
cpu.setCpuId(cpuId);
|
||||||
|
this->hashTable->search(cpu, cpu, key_to_index);
|
||||||
|
rowDisplay(cpu, widths);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
printTableFooter(widths);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -24,7 +24,8 @@ template<typename T>
|
|||||||
void SearchManager<T>::searchCPU() const {
|
void SearchManager<T>::searchCPU() const {
|
||||||
std::string cpuID;
|
std::string cpuID;
|
||||||
std::cout << "Enter the CPU ID to search: ";
|
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
|
// Search for the CPU in the HashTable
|
||||||
CPU foundCPU;
|
CPU foundCPU;
|
||||||
@ -33,7 +34,7 @@ void SearchManager<T>::searchCPU() const {
|
|||||||
|
|
||||||
if (collisionCount != -1) {
|
if (collisionCount != -1) {
|
||||||
std::cout << "CPU found:" << std::endl;
|
std::cout << "CPU found:" << std::endl;
|
||||||
std::cout << foundCPU << std::endl;
|
display(foundCPU);
|
||||||
} else {
|
} else {
|
||||||
std::cout << "CPU not found." << std::endl;
|
std::cout << "CPU not found." << std::endl;
|
||||||
}
|
}
|
||||||
|
59
utils.cpp
59
utils.cpp
@ -1,8 +1,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
|
||||||
void printRow(const vector<int> &widths, const vector<string>& row) {
|
void printRow(const vector<int> &widths, const vector<string> &row) {
|
||||||
cout << '|';
|
cout << '|';
|
||||||
for (int i = 0; i < widths.size(); i++) {
|
for (int i = 0; i < widths.size(); i++) {
|
||||||
cout << ' ';
|
cout << ' ';
|
||||||
@ -13,20 +14,10 @@ void printRow(const vector<int> &widths, const vector<string>& row) {
|
|||||||
cout << '\n';
|
cout << '\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
void printTableHeader(const vector<int> &widths, const vector<string> &headers) {
|
||||||
* 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) {
|
|
||||||
// Print top border
|
// Print top border
|
||||||
cout << "┌";
|
cout << "┌";
|
||||||
for (int width : widths) {
|
for (int width: widths) {
|
||||||
for (int i = 0; i < width; i++) {
|
for (int i = 0; i < width; i++) {
|
||||||
cout << "─";
|
cout << "─";
|
||||||
}
|
}
|
||||||
@ -39,29 +30,26 @@ void printTable(const vector<int>& widths, const vector<vector<string>>& data) {
|
|||||||
for (int i = 0; i < widths.size(); i++) {
|
for (int i = 0; i < widths.size(); i++) {
|
||||||
cout << ' ';
|
cout << ' ';
|
||||||
cout.width(widths[i] - 1);
|
cout.width(widths[i] - 1);
|
||||||
cout << std::left << data[0][i];
|
cout << std::left << headers[i];
|
||||||
cout << '|';
|
cout << '|';
|
||||||
}
|
}
|
||||||
cout << '\n';
|
cout << '\n';
|
||||||
|
|
||||||
// Print middle border
|
// Print middle border
|
||||||
cout << '+';
|
cout << '+';
|
||||||
for (int width : widths) {
|
for (int width: widths) {
|
||||||
for (int i = 0; i < width; i++) {
|
for (int i = 0; i < width; i++) {
|
||||||
cout << '=';
|
cout << '=';
|
||||||
}
|
}
|
||||||
cout << '+';
|
cout << '+';
|
||||||
}
|
}
|
||||||
cout << '\n';
|
cout << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
// Print data
|
void printTableFooter(const vector<int> &widths) {
|
||||||
for (int i = 1; i < data.size(); i++) {
|
|
||||||
printRow(widths, data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print bottom border
|
// Print bottom border
|
||||||
cout << "└";
|
cout << "└";
|
||||||
for (int width : widths) {
|
for (int width: widths) {
|
||||||
for (int i = 0; i < width; i++) {
|
for (int i = 0; i < width; i++) {
|
||||||
cout << "─";
|
cout << "─";
|
||||||
}
|
}
|
||||||
@ -70,6 +58,24 @@ void printTable(const vector<int>& widths, const vector<vector<string>>& data) {
|
|||||||
cout << "\b┘\n";
|
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() {
|
void printHelp() {
|
||||||
static const vector<int> helpWidths = {5, 40};
|
static const vector<int> helpWidths = {5, 40};
|
||||||
static const vector<vector<string>> helpData = {
|
static const vector<vector<string>> helpData = {
|
||||||
@ -142,3 +148,14 @@ int findNextPrime(int n) {
|
|||||||
|
|
||||||
return 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
13
utils.h
@ -3,11 +3,16 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using std::vector, std::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();
|
void printHelp();
|
||||||
|
|
||||||
@ -15,4 +20,8 @@ void printTeam();
|
|||||||
|
|
||||||
int findNextPrime(int n);
|
int findNextPrime(int n);
|
||||||
|
|
||||||
|
string boldStr(const string &str);
|
||||||
|
|
||||||
|
string centeredStr(const string &str, int width);
|
||||||
|
|
||||||
#endif //INC_08_TEAM_PROJECT_UTILS_H
|
#endif //INC_08_TEAM_PROJECT_UTILS_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user