From eeee3590c78468c55bb215213dd593fc0b0a1461 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Mon, 18 Jul 2022 23:01:56 +0200 Subject: [PATCH] Left justify output --- README.md | 96 +++++++++++++++++++++++++++--------------------------- matrix.hpp | 33 ++++++++++++++----- vector.hpp | 25 ++++++++++---- 3 files changed, 91 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 7fc3826..e9a4ecc 100644 --- a/README.md +++ b/README.md @@ -42,27 +42,27 @@ a * b = 18-i a / b = (6/25)+(17/25)i (a / b)* = (6/25)-(17/25)i v: -| 1 | -| 2 | -| i | +|1| +|2| +|i| w: -| 3 | -| 4 | -| 5 | +|3| +|4| +|5| v + w: -| 4 | -| 6 | -| 5+i | +|4 | +|6 | +|5+i| v - w: -| -2 | -| -2 | -| -5+i | +|-2 | +|-2 | +|-5+i| v * w: 11+5i w * i: -| 3i | -| 4i | -| 5i | +|3i| +|4i| +|5i| Element 0 of v: 1 Element 1 of v: @@ -70,57 +70,57 @@ Element 1 of v: Element 2 of v: i A temp vector of dimension 3 -| 0 | -| 0 | -| 0 | +|0| +|0| +|0| Assignment of w to k -| 3 | -| 4 | -| 5 | +|3| +|4| +|5| Manually set elements of k to elements of v -| 1 | -| 2 | -| i | +|1| +|2| +|i| The matrix m -| 1 | 1 | -| 1 | 1 | +|1|1| +|1|1| The determinant of matrix m: 0 The matrix n: -| 2 | 1+i | -| 1-i | 5 | +|2 |1+i| +|1-i|5 | The matrix n roted by one pi radian: -| 1+i | 5 | -| 2 | 1-i | +|1+i|5 | +|2 |1-i| The determinant of matrix n: 8 The matrix n's transpose: -| 2 | 1-i | -| 1+i | 5 | +|2 |1-i| +|1+i|5 | The matrix n's diagonal: -| 2 | -| 5 | +|2| +|5| The matrix n's off diagonal: -| 1+i | -| 1-i | +|1+i| +|1-i| The vector d: -| 1-i | -| 5 | +|1-i| +|5 | The matrix n * the vector d -| 2-2i | 2 | -| 5-5i | 25 | +|2-2i|2 | +|5-5i|25 | The vector d * the matrix n -| 7-7i | -| 27 | +|7-7i| +|27 | The matrix m * n -| 3-i | 6+i | -| 3-i | 6+i | +|3-i|6+i| +|3-i|6+i| The matrix m + n -| 3 | 2+i | -| 2-i | 6 | +|3 |2+i| +|2-i|6 | Is 2 an eigenvalue of m? Yes! The matrix n is hermitian, here is the hermitian conjugate: -| 2 | 1+i | -| 1-i | 5 | +|2 |1+i| +|1-i|5 | ``` diff --git a/matrix.hpp b/matrix.hpp index 56c9914..e743c7b 100644 --- a/matrix.hpp +++ b/matrix.hpp @@ -160,12 +160,27 @@ public: } friend ostream &operator<<(ostream &os, const matrix &m) { char last = '\0'; + int longest = 0; for (long long i = 0; i < m.num_entries; i++) { for (long long j = 0; j < m.entry_dimension; j++) { + ostringstream oss; + oss << m.entries[i][j]; + string s = oss.str(); + if (longest < s.length()) + longest = s.length(); + } + } + for (long long i = 0; i < m.num_entries; i++) { + for (long long j = 0; j < m.entry_dimension; j++) { + ostringstream iss; + iss << m.entries[i][j]; + string s = iss.str(); + int padding = longest - s.length() +1; string symbols[3]; symbols[0] = "|"; ostringstream oss; - oss << " " << m.entries[i][j] << " "; + oss << m.entries[i][j] ; + oss << std::setw(padding) << "|"; symbols[1] = oss.str(); symbols[2] = "|"; for (int i = 0; i < 3; i++) { @@ -206,16 +221,16 @@ public: return n; } -const matrix operator*(vector &v) const { - matrix res(this->get_num_entries(), this->get_entry_dimension()); - for (long long i = 0; i < this->get_num_entries(); i++) { - for (long long j = 0; j < this->get_entry_dimension(); j++) { - res[i][j] = (this->get_entry(i)[j] * v.get_entry(i)); + const matrix operator*(vector &v) const { + matrix res(this->get_num_entries(), this->get_entry_dimension()); + for (long long i = 0; i < this->get_num_entries(); i++) { + for (long long j = 0; j < this->get_entry_dimension(); j++) { + res[i][j] = (this->get_entry(i)[j] * v.get_entry(i)); + } } - } - return res; -} + return res; + } const matrix operator*(const matrix m) const { matrix n(this->num_entries, m.entry_dimension); if (this->num_entries != m.entry_dimension && diff --git a/vector.hpp b/vector.hpp index 4f18535..46db71c 100644 --- a/vector.hpp +++ b/vector.hpp @@ -1,5 +1,6 @@ #pragma once #include "cnumber.hpp" +#include #include using namespace std; class matrix; @@ -11,12 +12,12 @@ private: public: bool is_error() { return this->err; } - vector() {}; + vector(){}; vector(const long long dimention) { this->err = false; this->dimention = dimention; free(this->entries); - this->entries = (cnumber *)malloc(sizeof(cnumber)* dimention); + this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention); for (long long i = 0; i < dimention; i++) { this->set_entry(i, cnumber(0, 0)); } @@ -25,7 +26,7 @@ public: this->err = false; this->dimention = v.get_dimention(); free(this->entries); - this->entries = (cnumber *)malloc(sizeof(cnumber)* v.get_dimention()); + this->entries = (cnumber *)malloc(sizeof(cnumber) * v.get_dimention()); for (long long i = 0; i < v.get_dimention(); i++) { this->set_entry(i, v.get_entry(i)); } @@ -33,7 +34,7 @@ public: ~vector() { this->dimention = 0; free(this->entries); - //this->entries = NULL; + // this->entries = NULL; this->err = true; } long long get_dimention() const { return this->dimention; } @@ -59,8 +60,20 @@ public: cnumber &operator[](const long long index) { return this->entries[index]; } friend ostream &operator<<(ostream &os, const vector &v) { + int longest = 0; for (long long i = 0; i < v.get_dimention(); i++) { - os << "| " << v[i] << " |"; + ostringstream oss; + oss << v[i]; + string s = oss.str(); + if (longest < s.length()) + longest = s.length(); + } + for (long long i = 0; i < v.get_dimention(); i++) { + ostringstream oss; + oss << v[i]; + string s = oss.str(); + int padding = longest - s.length() +1; + os << "|" << v[i] << std::setw(padding) << "|"; if (i != v.get_dimention() - 1) os << endl; } @@ -69,7 +82,7 @@ public: void operator=(const vector &v) { this->dimention = v.get_dimention(); free(this->entries); - this->entries = (cnumber *)malloc(sizeof(cnumber)* dimention); + this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention); for (long long i = 0; i < v.get_dimention(); i++) { this->set_entry(i, v.get_entry(i)); }