PROBLEM:
matrix.h + matrix.cpp
Define a class Matrix to implement a concept – matrix of integers.
•The numbers of rows and columns are provided by the user of the class. In addition, define an exception type Matrix_exception , which is the inner class of Matrix.
NOTE: Implementation Constraint:
•When choosing data representation, you cannot use any STL containers. Use low-level array, linked list, and others.
matrix.cpp
#include<string.h>
#include <bits/stdc++.h>
#include <algorithm>
#include "matrix.h"
using namespace std;
Matrix::Matrix(int m=0,int n=0)
{
a=new int [m*n];
r=m;c=n;
}Matrix::Matrix(const Matrix& b)
{
cout<<"COPY"<<endl;
r=b.r;
c=b.c;
_init_a(b.a);
}Matrix::Matrix(Matrix&& b)
{
cout<<"MOVE COPY"<<endl;
r=b.r;c=b.c;
a=b.a;
b.r=0;b.c=0;
b.a=nullptr;
}Matrix::Matrix()
{
r=0;c=0;
a=new int[0];
}Matrix::~Matrix()
{
delete [] a;
}
int& Matrix::operator ()(int x,int y)
{
return a[x*c+y];
}int Matrix::col()
{
return c;
}int Matrix::row()
{
return r;
}
Matrix Matrix::operator+(Matrix& b)
{
if(r!=b.r||c!=b.c) throw Matrix_exception{};
Matrix result{r,c};
for(int i=0;i<r*c;i++)
{
result.a[i]=a[i]+b.a[i];
}
return move(result);
}Matrix Matrix::operator*(Matrix& b)
{
if (c!=b.r) throw Matrix_exception{};
Matrix result{r,b.c};
for (int i=0;i<r;i++)
{
for(int j=0;j<b.c;j++)
{
int p=0;
for (int k=0;k<c;k++)
{
p+=a[i*c+k]*b.a[k*b.c+j];
}
result.a[i*result.c+j]=p;
}
}
return move(result);
}Matrix& Matrix::operator=(const Matrix& b)
{
cout<<"COPY ASSIGN"<<endl;
r=b.r;c=b.c;
delete [] a;
a=new int[r*c];
for (int i=0;i<r*c;i++) a[i]= b.a[i];
return *this;
}
Matrix& Matrix::operator=(Matrix&& b)
{
cout<<"MOVE ASSGIN"<<endl;
if(this!=&b)
{
// if(a) delete [] a;
r=b.r;
c=b.c;
a=b.a;
b.r=0;b.c=0;
b.a=nullptr;
}
return *this;
}
ostream& operator<<(ostream& out,Matrix& b){
for(int i=0;i<b.r;i++)
{
for(int j =0;j<b.c;j++)
{
out<<b.a[i*b.c+j]<<(j==b.c-1?'\n':' ');
}
}
return out;
}istream& operator>>(istream& in,const Matrix& b)
{
for(int i=0;i<b.r;i++)
{
for(int j=0;j<b.c;j++)
in>>b.a[i*b.c+j];
}
return in;
}
matrix.h
#ifndef RINGBUFFERQUEUE_H_INCLUDED
#define RINGBUFFERQUEUE_H_INCLUDED
#define MAX_N 100001
#include <string>
#include <bits/stdc++.h>
using namespace std;
class Matrix
{
private:
int * a,r,c; //r:行 c:列
friend ostream& operator<<(ostream&,Matrix&);
friend istream& operator>>(istream&,const Matrix&);
void _init_a(const int* b)
{
a = new int[r*c];
for(int i=0;i<r*c;i++)
a[i]=b[i];
}
public:
Matrix(int,int);
~Matrix();
Matrix (Matrix&&);
Matrix (const Matrix&);
Matrix();
int col();
int row();
int& operator()(int,int);
Matrix operator*(Matrix&);
Matrix operator+(Matrix&);
Matrix& operator=(const Matrix&);
Matrix& operator=(Matrix&&);
struct Matrix_exception{};
};
#endif // RINGBUFFERQUEUE_H_INCLUDED
testMatrix.cpp
// test program for Matrix: testMatrix.cpp
#include <iostream>
using namespace std;#include "matrix.h"
int main(int argc,char**argv)
try
{
int m,n;
cout<<"Input the numbers of rows and columns:"<<endl;
cin>>m>>n;Matrix a{m,n},b{a},c;
cout<<"Input the elements of Matrix:"<<endl;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
cin>>a(i,j);
cout<<"Input the elements of another Matrix:"<<endl;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
cin>>b(i,j);
cout<<endl;cout<<"Matrix A:";
cout << "rows: " << a.row() << " columns: "
<< a.col() << endl << "data: " << endl << a << endl;
cout<<"Matrix B:";
cout << "rows: " << b.row() << " columns: "
<< b.col() << endl << "data: " << endl << b << endl;c=a+b;
cout<<"Matrix A+B:";
cout << "rows: " << c.row() << " columns: "
<< c.col() << endl << "data: " << endl << c << endl;c(0,0)*=100;
c(1,1)=10000;
cout << endl;
cout<<"Matrix C:";
cout << "rows: " << c.row() << " columns: "
<< c.col() << endl << "data: " << endl << c << endl;c=a*b;
cout<<"Matrix A * B:";
cout << "rows: " << c.row() << " columns: "
<< c.col() << endl << "data: " << endl << c << endl;Matrix d1;
d1=c;Matrix d2{3,4};
c=d1+d2;
return 0;
}
catch(Matrix::Matrix_exception)
{
cerr << "An exception happened in class Matrix"<<endl;
return 0;
}