add test coverage and fix input

This commit is contained in:
Ilya Grigorev
2024-10-08 00:57:25 +05:00
parent bf714eed92
commit 8007275709
9 changed files with 389 additions and 244 deletions
+37 -49
View File
@@ -1,47 +1,5 @@
#include "matrix.h"
void showMatrix(Matrix matrix) {
int maxNumberLength = 1;
for (size_t y = 0; y < matrix.getRows(); y++) {
for (size_t x = 0; x < matrix.getColumns(); x++) {
if (std::to_string(matrix[y][x]).length() > maxNumberLength) {
maxNumberLength = std::to_string(matrix[y][x]).length();
}
}
}
//std::cout << maxNumberLength << std::endl;
std::cout << std::endl;
for (size_t y = 0; y < matrix.getRows(); y++) {
std::string row = "";
for (size_t x = 0; x < matrix.getColumns(); x++) {
std::string strNumber = std::to_string(matrix[y][x]);
bool spaceRight = true;
while (strNumber.length() < maxNumberLength) {
if (spaceRight) {
strNumber += " ";
}
else {
strNumber = " " + strNumber;
}
spaceRight = !spaceRight;
strNumber = "" + strNumber;
}
if (matrix[y][x] == 0) {
row += "[\033[0m" + strNumber + "\033[0m] ";
}
else if(matrix[y][x] > 0) {
row += "[\033[32m" + strNumber + "\033[0m] ";
}else {
row += "[\033[31m" + strNumber + "\033[0m] ";
}
}
std::cout << row << std::endl;
}
std::cout << std::endl;
}
Vector::Vector(int n) {
rows = n;
columns = 1;
@@ -263,15 +221,45 @@ std::istream& operator>>(std::istream& cin, Matrix& matrixObj) {
}
std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj) {
for (int i = 0; i < matrixObj.rows; ++i) {
for (int j = 0; j < matrixObj.columns; ++j) {
if (j == matrixObj.columns - 1) {
cout << matrixObj[i][j] << std::endl;
}
else {
cout << matrixObj[i][j] << ' ';
size_t maxNumberLength = 1;
for (int y = 0; y < matrixObj.getRows(); y++) {
for (int x = 0; x < matrixObj.getColumns(); x++) {
if (std::to_string(matrixObj[y][x]).length() > maxNumberLength) {
maxNumberLength = std::to_string(matrixObj[y][x]).length();
}
}
}
//std::cout << maxNumberLength << std::endl;
std::cout << std::endl;
for (int y = 0; y < matrixObj.getRows(); y++) {
std::string row = "";
for (int x = 0; x < matrixObj.getColumns(); x++) {
std::string strNumber = std::to_string(matrixObj[y][x]);
bool spaceRight = true;
while (strNumber.length() < maxNumberLength) {
if (spaceRight) {
strNumber += " ";
}
else {
strNumber = " " + strNumber;
}
spaceRight = !spaceRight;
strNumber = "" + strNumber;
}
if (matrixObj[y][x] == 0) {
row += "[\033[0m" + strNumber + "\033[0m] ";
}
else if(matrixObj[y][x] > 0) {
row += "[\033[32m" + strNumber + "\033[0m] ";
}else {
row += "[\033[31m" + strNumber + "\033[0m] ";
}
}
std::cout << row << std::endl;
}
std::cout << std::endl;
return cout;
}