#include "intMatris.h" #include //Public intMatris::intMatris() { mRows = 0; mCols = 0; try { matrix = new intVektor[0]; } catch(std::bad_alloc& e) { matrix = 0; throw outOfMemory(); } setMatrix(0,0); } intMatris::intMatris(int rows, int cols) { assert(rows > 0 && cols > 0); mRows = 0; mCols = 0; try { matrix = new intVektor[0]; } catch(std::bad_alloc& e) { matrix = 0; throw outOfMemory(); } setMatrix(rows,cols); } intMatris::intMatris(const intMatris& rh) { if(this != &rh) { mRows = 0; mCols = 0; try { matrix = new intVektor[0]; } catch(std::bad_alloc& e) { matrix = 0; throw outOfMemory(); } //Satt storlek try { setMatrix(rh.mCols,rh.mRows); } catch(outOfMemory &e) { throw; } //Kopiera varden for(int i = 0; i < rh.mCols; i++) matrix[i] = rh.matrix[i]; } } intMatris::~intMatris() { delete[] matrix; } intMatris& intMatris::operator=(const intMatris& rh) { if(this != &rh) { //Satt storlek try { setMatrix(rh.mCols,rh.mRows); } catch(outOfMemory &e) { throw; } //Kopiera varden for(int i = 0; i < mCols; i++) matrix[i] = rh.matrix[i]; } return *this; } intVektor& intMatris::operator[](const int x) const { assert(0 <= x && x < mRows); return matrix[x]; } const intMatris intMatris::operator+(intMatris& rh) { assert(mRows == rh.mRows && mCols == rh.mCols); intMatris temp(rh.mRows, rh.mCols); for(int i = 0; i < mRows; i++) temp[i] = matrix[i] + rh.matrix[i]; return temp; } int intMatris::getRows() { return mRows; } int intMatris::getCols() { return mCols; } //Private void intMatris::setMatrix(const int newRows, const int newCols) { if(matrix != 0) delete[] matrix; try { matrix = new intVektor[newCols]; } catch(std::bad_alloc& e) { matrix = 0; throw outOfMemory(); } for(int i = 0; i < newRows; i++) { const intVektor temp(newRows); matrix[i] = temp; } mRows = newRows; mCols = newCols; } //Other operators std::ostream& operator<<(std::ostream& os, intMatris rh) { os << "("; for(int j = 0; j < rh.getRows(); j++) { for(int i = 0; i < rh.getRows(); i++) os << "[" << rh[j][i] << "]"; if(j != rh.getRows() - 1) os << std::endl; } os << ")" << std::endl; return os; } std::istream& operator>>(std::istream& is, intMatris& rh) { for(int j = 0; j < rh.getRows(); j++) { try { is >> rh[j]; } catch(std::ios_base::failure &e) { throw streamError(); } } return is; }