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
|
#pragma once
#include <string>
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
template <class T, unsigned int ROWS, unsigned int COLUMNS>
class Matrix
{
public:
Matrix(); // default constructor
~Matrix(); // destructor
template<class T,unsigned int ROWS,unsigned int COLUMNS>
friend ostream& operator << (ostream&,const Matrix<T,ROWS,COLUMNS>&);
protected:
T* M[ROWS];
};
template<class T,unsigned int H, unsigned int W>
Matrix<T,H,W>::Matrix() // identity matrix
{
for(int i=0;i<H;i++)
M[i]=new T[W];
for(int i=0;i<H;i++)
for(int j=0;j<W;j++)
M[i][j]=(i==j)?i+j:0;
}
template<class T,unsigned int H,unsigned int W>
Matrix<T,H,W>::~Matrix()
{
for(int i=0;i<H;i++)
delete[] M[i];
}
template<class T,unsigned int H,unsigned int W>
ostream& operator<< (ostream& out,const Matrix<T,H,W>& m)
{
for(int i=0;i<H;i++)
for(int j=0;j<W;j++)
{
char* temp;
sprintf(temp,"%03d",m.M[i][j]);
out<<temp;
}
return out;
}
|