Table-and-Graph-Libs/tables.cpp

573 lines
15 KiB
C++
Raw Normal View History

2019-01-04 17:29:55 +08:00
// Teal Dulcet, CS546
2023-03-10 18:07:00 +08:00
// Compile: g++ -Wall -g -O3 -std=c++14 tables.cpp -o tables
2019-01-04 17:29:55 +08:00
// Run: ./tables
#include <cctype>
#include <cmath>
#include <algorithm>
#include <array>
#include <vector>
2019-01-04 17:29:55 +08:00
#include "tables.hpp"
using namespace std;
constexpr long double afunction(long double x)
2019-01-04 17:29:55 +08:00
{
return x + 1;
}
constexpr long double function1(long double x)
2019-01-04 17:29:55 +08:00
{
return 2 * x;
}
constexpr long double function2(long double x)
2019-01-04 17:29:55 +08:00
{
return pow(x, 2);
}
int dimensions = 0;
int sortdimension = 0;
/* template <typename T>
int compare(const void *pa, const void *pb)
{
const T a = *(const T *)pa;
const T b = *(const T *)pb;
2019-01-04 17:29:55 +08:00
if (a[sortdimension] == b[sortdimension])
{
for (int i = 0; i < dimensions; ++i)
{
if (sortdimension != i and a[i] != b[i])
{
if (a[i] > b[i])
return 1;
return -1;
}
}
}
if (a[sortdimension] > b[sortdimension])
return 1;
else if (a[sortdimension] == b[sortdimension])
return 0;
return -1;
} */
template <typename T>
bool compare(const T &a, const T &b)
2019-01-04 17:29:55 +08:00
{
if (a[sortdimension] == b[sortdimension])
for (int i = 0; i < dimensions; ++i)
if (sortdimension != i and a[i] != b[i])
return a[i] < b[i];
return a[sortdimension] < b[sortdimension];
}
int main()
{
const size_t rows = 5;
const size_t columns = 5;
const long double xmin = -10;
const long double xmax = 10;
const long double xscl = 2; // 80 / (xmax - xmin);
string *headerrow = nullptr;
string *headercolumn = nullptr;
2019-01-04 17:29:55 +08:00
// Output array as table
cout << "\nOutput array as table\n\n";
{
long long **array;
array = new long long *[rows];
for (unsigned int i = 0; i < rows; ++i)
array[i] = new long long[columns];
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand();
tables::options aoptions;
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::array(rows, columns, array, nullptr, nullptr, aoptions);
2019-01-04 17:29:55 +08:00
}
if (array != nullptr)
2019-01-04 17:29:55 +08:00
{
for (unsigned int i = 0; i < rows; ++i)
delete[] array[i];
delete[] array;
}
}
{
array<array<long long, columns>, rows> aarray;
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
aarray[i][j] = rand();
tables::options aoptions;
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::array(aarray, headerrow, headercolumn, aoptions);
}
}
{
vector<vector<long long>> array(rows, vector<long long>(columns));
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand();
tables::options aoptions;
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::array(array, headerrow, headercolumn, aoptions);
}
}
2019-01-04 17:29:55 +08:00
{
long double **array;
array = new long double *[rows];
for (unsigned int i = 0; i < rows; ++i)
array[i] = new long double[columns];
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
array[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX);
tables::options aoptions;
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::array(rows, columns, array, nullptr, nullptr, aoptions);
2019-01-04 17:29:55 +08:00
}
if (array != nullptr)
2019-01-04 17:29:55 +08:00
{
for (unsigned int i = 0; i < rows; ++i)
delete[] array[i];
delete[] array;
}
}
{
array<array<long double, columns>, rows> aarray;
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
aarray[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX);
tables::options aoptions;
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::array(aarray, headerrow, headercolumn, aoptions);
}
}
{
vector<vector<long double>> array(rows, vector<long double>(columns));
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
array[i][j] = static_cast<long double>(rand()) / static_cast<long double>(RAND_MAX);
tables::options aoptions;
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::array(array, headerrow, headercolumn, aoptions);
}
}
2019-01-04 17:29:55 +08:00
// Output char array as table
cout << "\nOutput char array as table\n\n";
{
2022-02-01 23:20:23 +08:00
const char *const array[rows][columns] = {
2019-01-04 17:29:55 +08:00
{"Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"},
{"Header column 2", "Data 1", "Data 2", "Data 3", "Data 4"},
{"Header column 3", "Data 5", "Data 6", "Data 7", "Data 8"},
{"Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"},
{"Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"}};
tables::options aoptions;
2019-01-04 17:29:55 +08:00
aoptions.headerrow = true;
aoptions.headercolumn = true;
// tables::options aoptions{.headerrow = true, .headercolumn = true};
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::array(array, headerrow, headercolumn, aoptions);
}
}
{
const array<array<string, columns>, rows> aarray = {{{"Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"},
{"Header column 2", "Data 1", "Data 2", "Data 3", "Data 4"},
{"Header column 3", "Data 5", "Data 6", "Data 7", "Data 8"},
{"Header column 4", "Data 9", "Data 10", "Data 11", "Data 12"},
{"Header column 5", "Data 13", "Data 14", "Data 15", "Data 16"}}};
tables::options aoptions;
aoptions.headerrow = true;
aoptions.headercolumn = true;
// tables::options aoptions{.headerrow = true, .headercolumn = true};
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::array(aarray, headerrow, headercolumn, aoptions);
2019-01-04 17:29:55 +08:00
}
}
// Output array as table with separate header row and column
cout << "\nOutput array as table with separate header row and column\n\n";
{
const size_t rows = 4;
const size_t columns = 4;
2022-02-01 23:20:23 +08:00
const char *const array[rows][columns] = {
2019-01-04 17:29:55 +08:00
{"Data 1", "Data 2", "Data 3", "Data 4"},
{"Data 5", "Data 6", "Data 7", "Data 8"},
{"Data 9", "Data 10", "Data 11", "Data 12"},
{"Data 13", "Data 14", "Data 15", "Data 16"}};
const char *const headerrow[] = {"Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"};
const char *const headercolumn[] = {"Header column 2", "Header column 3", "Header column 4", "Header column 5"};
tables::options aoptions;
2019-01-04 17:29:55 +08:00
aoptions.headerrow = true;
aoptions.headercolumn = true;
// tables::options aoptions{.headerrow = true, .headercolumn = true};
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::array(array, headerrow, headercolumn, aoptions);
}
}
{
const size_t rows = 4;
const size_t columns = 4;
const array<array<string, columns>, rows> aarray = {{{"Data 1", "Data 2", "Data 3", "Data 4"},
{"Data 5", "Data 6", "Data 7", "Data 8"},
{"Data 9", "Data 10", "Data 11", "Data 12"},
{"Data 13", "Data 14", "Data 15", "Data 16"}}};
const string headerrow[] = {"Header row/column 1", "Header row 2", "Header row 3", "Header row 4", "Header row 5"};
const string headercolumn[] = {"Header column 2", "Header column 3", "Header column 4", "Header column 5"};
vector<string> aheaderrow(headerrow, headerrow + (rows + 1) - 1);
vector<string> aheadercolumn(headerrow, headerrow + 1);
aheadercolumn.insert(aheadercolumn.end(), headercolumn, headercolumn + columns - 1);
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
{
tables::options aoptions;
aoptions.headerrow = true;
aoptions.headercolumn = true;
aoptions.cellborder = true;
aoptions.style = k;
// tables::options aoptions{.headerrow = true, .headercolumn = true, .cellborder = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
tables::options aoptions;
aoptions.headerrow = true;
aoptions.headercolumn = true;
aoptions.style = k;
// tables::options aoptions{.headerrow = true, .headercolumn = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
string *headerrow = aheaderrow.data();
string *headercolumn = nullptr;
tables::options aoptions;
aoptions.headerrow = true;
aoptions.style = k;
// tables::options aoptions{.headerrow = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
string *headerrow = nullptr;
string *headercolumn = aheadercolumn.data();
tables::options aoptions;
aoptions.headercolumn = true;
aoptions.style = k;
// tables::options aoptions{.headercolumn = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
string *headerrow = nullptr;
string *headercolumn = nullptr;
tables::options aoptions;
aoptions.cellborder = true;
aoptions.style = k;
// tables::options aoptions{.cellborder = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
string *headerrow = nullptr;
string *headercolumn = nullptr;
tables::options aoptions;
aoptions.tableborder = false;
aoptions.style = k;
// tables::options aoptions{.tableborder = false, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
tables::options aoptions;
aoptions.tableborder = false;
aoptions.headerrow = true;
aoptions.headercolumn = true;
aoptions.style = k;
// tables::options aoptions{.tableborder = false, .headerrow = true, .headercolumn = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
string *headerrow = aheaderrow.data();
string *headercolumn = nullptr;
tables::options aoptions;
aoptions.tableborder = false;
aoptions.headerrow = true;
aoptions.style = k;
// tables::options aoptions{.tableborder = false, .headerrow = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
string *headerrow = nullptr;
string *headercolumn = aheadercolumn.data();
tables::options aoptions;
aoptions.tableborder = false;
aoptions.headercolumn = true;
aoptions.style = k;
// tables::options aoptions{.tableborder = false, .headercolumn = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
{
string *headerrow = nullptr;
string *headercolumn = nullptr;
tables::options aoptions;
aoptions.tableborder = false;
aoptions.cellborder = true;
aoptions.style = k;
// tables::options aoptions{.tableborder = false, .cellborder = true, .style = k};
tables::array(aarray, headerrow, headercolumn, aoptions);
}
2019-01-04 17:29:55 +08:00
}
}
{
bool **array;
array = new bool *[rows];
for (unsigned int i = 0; i < rows; ++i)
array[i] = new bool[columns];
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand() % 2;
tables::options aoptions;
2019-01-04 17:29:55 +08:00
aoptions.boolalpha = true;
// tables::options aoptions{.boolalpha = true};
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::array(rows, columns, array, nullptr, nullptr, aoptions);
2019-01-04 17:29:55 +08:00
}
if (array != nullptr)
2019-01-04 17:29:55 +08:00
{
for (unsigned int i = 0; i < rows; ++i)
delete[] array[i];
delete[] array;
}
}
// Output sorted array as table
cout << "\nOutput sorted array as table\n\n";
{
int **array;
array = new int *[rows];
for (unsigned int i = 0; i < rows; ++i)
array[i] = new int[columns];
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand();
dimensions = columns;
sortdimension = 0;
// qsort(array, rows, sizeof(array[0]), compare<int *>);
sort(array, array + rows, compare<int *>);
2019-01-04 17:29:55 +08:00
tables::options aoptions;
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::array(rows, columns, array, nullptr, nullptr, aoptions);
2019-01-04 17:29:55 +08:00
}
if (array != nullptr)
2019-01-04 17:29:55 +08:00
{
for (unsigned int i = 0; i < rows; ++i)
delete[] array[i];
delete[] array;
}
}
{
array<array<int, columns>, rows> aarray;
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
aarray[i][j] = rand();
dimensions = columns;
sortdimension = 0;
sort(aarray.begin(), aarray.end(), compare<array<int, columns>>);
tables::options aoptions;
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::array(aarray, headerrow, headercolumn, aoptions);
}
}
{
vector<vector<int>> array(rows, vector<int>(columns));
for (unsigned int i = 0; i < rows; ++i)
for (unsigned int j = 0; j < columns; ++j)
array[i][j] = rand();
dimensions = columns;
sortdimension = 0;
sort(array.begin(), array.end(), compare<vector<int>>);
tables::options aoptions;
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::array(array, headerrow, headercolumn, aoptions);
}
}
2019-01-04 17:29:55 +08:00
// Output single function as table
cout << "\nOutput single function as table\n\n";
{
tables::options aoptions;
2019-01-04 17:29:55 +08:00
aoptions.headerrow = true;
// tables::options aoptions{.headerrow = true};
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::function(xmin, xmax, xscl, afunction, aoptions);
}
}
{
function<long double(long double)> afunction = [](auto x)
{ return x + 1; };
tables::options aoptions;
aoptions.headerrow = true;
// tables::options aoptions{.headerrow = true};
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::function(xmin, xmax, xscl, afunction, aoptions);
2019-01-04 17:29:55 +08:00
}
}
// Output multiple functions as table
cout << "\nOutput multiple functions as table\n\n";
{
function<long double(long double)> functions[] = {function1, function2};
tables::options aoptions;
aoptions.headerrow = true;
// tables::options aoptions{.headerrow = true};
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
{
aoptions.style = k;
tables::functions(xmin, xmax, xscl, 2, functions, aoptions);
}
}
{
function<long double(long double)> functions[] = {[](auto x)
{ return 2 * x; },
[](auto x)
{ return pow(x, 2); }};
2019-01-04 17:29:55 +08:00
tables::options aoptions;
2019-01-04 17:29:55 +08:00
aoptions.headerrow = true;
// tables::options aoptions{.headerrow = true};
2019-01-04 17:29:55 +08:00
2023-03-10 18:07:00 +08:00
for (unsigned int k = 0; k < tables::size(tables::styles); ++k)
2019-01-04 17:29:55 +08:00
{
aoptions.style = k;
tables::functions(xmin, xmax, xscl, 2, functions, aoptions);
2019-01-04 17:29:55 +08:00
}
}
return 0;
}