-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_printer.cpp
53 lines (35 loc) · 1.27 KB
/
matrix_printer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>
#include <memory>
#include "matrix.h"
#include "matrix_printer.h"
#define MAX_PRINT_WIDTH 5
#define MAX_PRINT_LENGTH 5
void Matrix::Printer::operator()(const Matrix::Representation& m) noexcept {
bool valid_column_print = true;
bool reached_end_row = true;
bool before_max_width = true;
bool last_column_val = true;
u_int64_t n_cols = m.num_cols();
u_int64_t n_rows = m.num_rows();
uint64_t total_iter = n_rows * n_cols;
std::cout << "Matrix[R=" << n_rows << "][C=" << n_cols << "]:";
std::cout << std::setprecision(2);
for (u_int64_t i = 0; i < total_iter; i++) {
reached_end_row = i % n_cols == 0;
before_max_width = i % n_cols < MAX_PRINT_WIDTH;
last_column_val = i % n_cols == n_cols - 1;
if (reached_end_row) std::cout << '\n';
std::cout << std::setw(6) << std::right;
if (before_max_width || last_column_val) {
std::cout << m.get(i / n_cols, i % n_cols) << " ";
valid_column_print = true;
}
else if (!valid_column_print) {}
else {
std::cout << "... ";
valid_column_print = false;
}
}
std::cout << std::endl << std::endl;
}