帮我看看下面的题目用C++怎么编吧!!! 题目:基于运算符重载的方阵类设计与实现
要求:设计一个能够实现n×n矩阵操作的类,这里的n可变,主要功能包括:(1)使用构造函数完成方阵的初始化赋值;(2)使用析构函数完成矩阵动态内存的释放;(3)重载加法运算...
要求:设计一个能够实现n×n矩阵操作的类,这里的n可变,主要功能包括:
(1) 使用构造函数完成方阵的初始化赋值;
(2) 使用析构函数完成矩阵动态内存的释放;
(3) 重载加法运算符+,实现两个矩阵的和;
(4) 重载加法运算符-,实现两个矩阵的差;
(5) 重载加法运算符*,实现两个矩阵的积;
(6) 重载加法运算符=,实现两个矩阵之间的赋值;
(7) 使用拷贝构造函数完成方阵的拷贝构造;
(8) 重载加法运算符<<,实现矩阵按照行列的格式输出;
(9) 编写一个主函数测试上述功能。 展开
(1) 使用构造函数完成方阵的初始化赋值;
(2) 使用析构函数完成矩阵动态内存的释放;
(3) 重载加法运算符+,实现两个矩阵的和;
(4) 重载加法运算符-,实现两个矩阵的差;
(5) 重载加法运算符*,实现两个矩阵的积;
(6) 重载加法运算符=,实现两个矩阵之间的赋值;
(7) 使用拷贝构造函数完成方阵的拷贝构造;
(8) 重载加法运算符<<,实现矩阵按照行列的格式输出;
(9) 编写一个主函数测试上述功能。 展开
2个回答
展开全部
class Matrix
{
friend Matrix operator+(const Matrix & m1, const Matrix & m2);
friend Matrix operator-(const Matrix & m1, const Matrix & m2);
friend Matrix operator*(const Matrix & m1, const Matrix & m2);
friend ostream &operator<<(ostream & os1,const Matrix & m1);
private:
int** matrix;
int Dim;
public:
Matrix();
Matrix(int n);
Matrix(const Matrix & m);
~Matrix();
void Print();
Matrix operator=(const Matrix & m1);
};
Matrix::Matrix()
{
matrix = NULL;
Dim = 0;
}
Matrix::Matrix(int n)
{
Dim = n;
matrix = new int* [Dim];
for(int i=0; i<Dim; i++)
{
cout << "输入矩阵第" << i+1 << "行元素,共" << Dim << "个元素:";
matrix[i] = new int [Dim];
for(int j=0; j<Dim; j++)
{
cin >> matrix[i][j];
}
cout << endl;
}
}
Matrix::Matrix(const Matrix & m)
{
Dim = m.Dim;
matrix = new int* [Dim];
for(int i=0; i<Dim; i++)
{
matrix[i] = new int [Dim];
for(int j=0; j<Dim; j++)
{
matrix[i][j] = m.matrix[i][j];
}
}
}
Matrix::~Matrix()
{
if(Dim != 0)
{
for(int i=0; i<Dim; i++)
{
delete [] matrix[i];
}
delete [] matrix;
}
}
void Matrix::Print()
{
if(Dim == 0)
{
cout << "Matrix is NULL!" << endl;
}
else
{
for(int i=0; i<Dim; i++)
{
for(int j=0; j<Dim; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
}
Matrix operator+(const Matrix & m1, const Matrix & m2)
{
Matrix matrix(m1);
for(int i=0; i<m2.Dim; i++)
{
for(int j=0; j<m2.Dim; j++)
{
matrix.matrix[i][j] += m2.matrix[i][j];
}
}
return matrix;
}
Matrix operator-(const Matrix & m1, const Matrix & m2)
{
Matrix matrix(m1);
for(int i=0; i<m2.Dim; i++)
{
for(int j=0; j<m2.Dim; j++)
{
matrix.matrix[i][j] -= m2.matrix[i][j];
}
}
return matrix;
}
Matrix operator*(const Matrix & m1, const Matrix & m2)
{
Matrix matrix;
matrix.Dim = m2.Dim;
matrix.matrix = new int* [m2.Dim];
for(int i=0; i<m2.Dim; i++)
{
matrix.matrix[i] = new int [m2.Dim];
for(int j=0; j<m2.Dim; j++)
{
matrix.matrix[i][j] = 0;
for(int k=0; k<m2.Dim; k++)
{
matrix.matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
}
}
}
matrix.Print();
return matrix;
}
Matrix Matrix:: operator=(const Matrix & m)
{
Dim = m.Dim;
matrix = new int* [Dim];
for(int i=0; i<Dim; i++)
{
matrix[i] = new int [Dim];
for(int j=0; j<Dim; j++)
{
matrix[i][j] = m.matrix[i][j];
}
}
return *this;
}
ostream &operator<<(ostream & os,const Matrix & m1)
{
for(int i=0; i<m1.Dim; i++)
{
for(int j=0; j<m1.Dim; j++)
{
os << m1.matrix[i][j] << " ";
}
os << endl;
}
return os;
}
void main()
{
Matrix m(2);
Matrix m1(2);
Matrix m2 = m + m1; //加法
cout << m << endl; //重载<<运算符
Matrix m3 = m - m1; //减法
cout << m3 << endl;
Matrix m4 = m * m1; //乘法
cout << m4 << endl;
Matrix m5 = m; //等号赋值
cout << m5 << endl;
Matrix m6(m1); //重载拷贝构造函数
cout << m6 << endl;
}
{
friend Matrix operator+(const Matrix & m1, const Matrix & m2);
friend Matrix operator-(const Matrix & m1, const Matrix & m2);
friend Matrix operator*(const Matrix & m1, const Matrix & m2);
friend ostream &operator<<(ostream & os1,const Matrix & m1);
private:
int** matrix;
int Dim;
public:
Matrix();
Matrix(int n);
Matrix(const Matrix & m);
~Matrix();
void Print();
Matrix operator=(const Matrix & m1);
};
Matrix::Matrix()
{
matrix = NULL;
Dim = 0;
}
Matrix::Matrix(int n)
{
Dim = n;
matrix = new int* [Dim];
for(int i=0; i<Dim; i++)
{
cout << "输入矩阵第" << i+1 << "行元素,共" << Dim << "个元素:";
matrix[i] = new int [Dim];
for(int j=0; j<Dim; j++)
{
cin >> matrix[i][j];
}
cout << endl;
}
}
Matrix::Matrix(const Matrix & m)
{
Dim = m.Dim;
matrix = new int* [Dim];
for(int i=0; i<Dim; i++)
{
matrix[i] = new int [Dim];
for(int j=0; j<Dim; j++)
{
matrix[i][j] = m.matrix[i][j];
}
}
}
Matrix::~Matrix()
{
if(Dim != 0)
{
for(int i=0; i<Dim; i++)
{
delete [] matrix[i];
}
delete [] matrix;
}
}
void Matrix::Print()
{
if(Dim == 0)
{
cout << "Matrix is NULL!" << endl;
}
else
{
for(int i=0; i<Dim; i++)
{
for(int j=0; j<Dim; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
}
Matrix operator+(const Matrix & m1, const Matrix & m2)
{
Matrix matrix(m1);
for(int i=0; i<m2.Dim; i++)
{
for(int j=0; j<m2.Dim; j++)
{
matrix.matrix[i][j] += m2.matrix[i][j];
}
}
return matrix;
}
Matrix operator-(const Matrix & m1, const Matrix & m2)
{
Matrix matrix(m1);
for(int i=0; i<m2.Dim; i++)
{
for(int j=0; j<m2.Dim; j++)
{
matrix.matrix[i][j] -= m2.matrix[i][j];
}
}
return matrix;
}
Matrix operator*(const Matrix & m1, const Matrix & m2)
{
Matrix matrix;
matrix.Dim = m2.Dim;
matrix.matrix = new int* [m2.Dim];
for(int i=0; i<m2.Dim; i++)
{
matrix.matrix[i] = new int [m2.Dim];
for(int j=0; j<m2.Dim; j++)
{
matrix.matrix[i][j] = 0;
for(int k=0; k<m2.Dim; k++)
{
matrix.matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
}
}
}
matrix.Print();
return matrix;
}
Matrix Matrix:: operator=(const Matrix & m)
{
Dim = m.Dim;
matrix = new int* [Dim];
for(int i=0; i<Dim; i++)
{
matrix[i] = new int [Dim];
for(int j=0; j<Dim; j++)
{
matrix[i][j] = m.matrix[i][j];
}
}
return *this;
}
ostream &operator<<(ostream & os,const Matrix & m1)
{
for(int i=0; i<m1.Dim; i++)
{
for(int j=0; j<m1.Dim; j++)
{
os << m1.matrix[i][j] << " ";
}
os << endl;
}
return os;
}
void main()
{
Matrix m(2);
Matrix m1(2);
Matrix m2 = m + m1; //加法
cout << m << endl; //重载<<运算符
Matrix m3 = m - m1; //减法
cout << m3 << endl;
Matrix m4 = m * m1; //乘法
cout << m4 << endl;
Matrix m5 = m; //等号赋值
cout << m5 << endl;
Matrix m6(m1); //重载拷贝构造函数
cout << m6 << endl;
}
展开全部
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
void exchange(int *a,int *b,int n)
{
int i;
for (i = 0; i<n; i++) {
swap(a+i,b+i);
}
}
#include <stdio.h>
#include <stdlib.h>
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
void exchange(int *a,int *b,int n)
{
int i;
for (i = 0; i<n; i++) {
swap(a+i,b+i);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询