64,654
社区成员
发帖
与我相关
我的任务
分享
//matrix.h
#pragma once
#include<iostream>
using namespace std;
template<class T>
class matrix
{
friend ostream& operator<<(ostream&, const matrix<T>&);
public:
matrix(int theRows = 0, int theColumes = 0);
matrix(const matrix<T>&);
~matrix(){ delete[] elements; }
int rows(){ return theRows; }
int columes(){ return theColumes; }
T& operator()(int i, int j) const;
matrix<T> operator+(const matrix<T>&) const;
matrix<T> operator+() const;
matrix<T>& operator=(const matrix<T>&);
matrix<T> operator-(const matrix<T>&) const;
matrix<T> operator-() const;
matrix<T> operator*(const matrix<T>&) const;
matrix<T> operator+=(const T&);
private:
int theRows, theColumes;
T *elements;
};
template<class T>
ostream& operator<<(ostream& out, const matrix<T>& m)
{// Put matrix m into the stream out.
// One row per line.
int k = 0; // index into element array
for (int i = 0; i < m.theRows; i++)
{// do row i
for (int j = 0; j < m.theColumes; j++)
out << m.elements[k++] << " ";
// row i finished
out << endl;
}
return out;
}
//matrix.cpp
#include "matrix.h"
#include "illegalParameterValue.h"
template<class T>
matrix<T>::matrix(int theRows,int theColumes)
{
if (theRows < 0 || theColumes < 0)
throw illegalParameterValue("Row and Columes must be >=0");
if ((theRows != 0 || theColumes != 0) && (theRows == 0 || theColumes == 0))
throw illegalParameterValue("Either both or neither Rows and Columes should be zero");
this->theRows = theRows;
this->theColumes = theColumes;
elements = new T[theRows*theColumes];
}
template<class T>
matrix<T>::matrix(const matrix<T>& m)
{
theRows = m.theRows;
theColumes = m.theColumes;
elements = new T[theColumes*theRows];
copy(m.elements, m.elements + theColumes*theRows, elements);
}
template<class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& m)
{
if (this != &m)
{
delete[] elements;
theRows = m.theRows;
theColumes = m.theColumes;
elements = new T[theColumes*theRows];
copy(m.elements, m.elements + theRows*theColumes, elements);
}
return *this;
}
template<class T>
T& matrix<T>::operator()(int i, int j) const
{
if (i<1 || i>theRows || j > 1 || j > theColumes)
throw matrixIndexOutOfBrounds();
return elements[(i - 1)*theColumes + j - 1];
}
template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m) const
{
if (theRows != m.theRows || theColumes != m.theColumes)
throw matrixSizeMismatch();
matrix<T> w(theRows, theColumes);
for (int i = 0; i < theRows*theColumes; i++)
w.elements[i] = m.elements[i] + elements[i];
return w;
}
template<class T>
matrix<T> matrix<T>::operator*(const matrix<T>& m) const
{
if (theColumes != m.theRows)
throw matrixSizeMismatch();
matrix<T> w(theRows, m.theColumes);
int ct = 0, cm = 0, cw = 0;
for (int i = 1; i <= theRows; i++)
{
for (int j = 0; j <= theColumes; j++)
{
T sum = elements[ct] * m.elements[cm];
for (int k = 0; k <= theColumes; k++)
{
ct++;
cm += m.theColumes;
sum += elements[ct] * m.elements[cm];
}
w.elements[cw++] = sum;
ct -= theColumes - 1;
cm = j;
}
ct += theColumes;
cm = 0;
}
return w;
}
//main.cpp
#include<iostream>
#include<string>
#include"matrix.h"
using namespace std;
int main()
{
matrix<int> mat(4,5);
return 0;
}