-
Notifications
You must be signed in to change notification settings - Fork 4
/
oFmt.h
66 lines (56 loc) · 1.38 KB
/
oFmt.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include <iostream>
#include <iomanip>
#include <locale>
using namespace std;
#include <cmath>
#include <iomanip>
#include <sstream>
#include <string>
#include "cmat.h"
class FMT
{
public:
explicit FMT(const char* fmt) : m_fmt(fmt) {}
private:
class fmter //actual worker class
{
public:
explicit fmter(std::ostream& strm, const FMT& fmt) : m_strm(strm), m_fmt(fmt.m_fmt) {}
//output next object (any type) to stream:
template<typename TYPE>
std::ostream& operator<<(const TYPE& value)
{
// return m_strm << "FMT(" << m_fmt << "," << value << ")";
char buf[40]; //enlarge as needed
snprintf(buf, sizeof(buf), m_fmt, value);
return m_strm << buf;
}
private:
std::ostream& m_strm;
const char* m_fmt;
};
const char* m_fmt; //save fmt string for inner class
//kludge: return derived stream to allow operator overloading:
friend FMT::fmter operator<<(std::ostream& strm, const FMT& fmt)
{
return FMT::fmter(strm, fmt);
}
};
void print_vector(vector<double>& m) {
for (int j = 0; j < m.size(); ++j) {
cout << FMT("%4.20f") << m[j] << endl;
}
cout << endl;
}
void print_2Dmatrix(Matrix<double>& matrix) {
int rows = matrix.num_rows();
int cols = matrix.num_cols();
for (int i = 0; i < rows; ++i) {
cout << "\t\t";
for (int j = 0; j < cols; ++j) {
cout << FMT("%4.20f") << matrix(i,j) << "\t";
}
cout << endl;
}
}