Left justify output
This commit is contained in:
parent
e125739a63
commit
eeee3590c7
3 changed files with 91 additions and 63 deletions
17
matrix.hpp
17
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++) {
|
||||
|
|
15
vector.hpp
15
vector.hpp
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include "cnumber.hpp"
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class matrix;
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue