📄 matrix.cpp
字号:
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <fstream.h>
#include "matrix.h"
/////////////////////////////////////////////////////////
matrix::matrix()
{
column=1;
row=1;
pdate=NULL;
init(row,column);
}
///////////////////////////////////////////////////////
bool matrix::init(int n,int m)
{
if(pdate)
{delete [] pdate;
pdate=NULL;
}
int size=n*m;
if(size<0)
{return false;
}
pdate=new double[size];
memset(pdate,0,sizeof(double)*size);
return true;
}
///////////////////////////////////////////////
matrix::matrix(int n,int m)
{column=m;
row=n;
pdate=NULL;
init(row,column);
}
///////////////////////////////////////////////
matrix::matrix(int n,int m,double value[])
{column=m;
row=n;
pdate=NULL;
init(n,m);
setdate(value);
}
//////////////////////////////////////////////
void matrix::setdate(double value[])
{memset(pdate,0,sizeof(double)*column*row);
memcpy(pdate,value,sizeof(double)*column*row);
for(int i=0;i<row;i++)
{cout<<endl;
for(int j=0;j<column;j++)
cout<<*(pdate+j+i*column)<<" ";
}
}
//////////////////////////////////////////
matrix::matrix(int nn,double value[])
{column=nn;
row=nn;
pdate=NULL;
init(nn,nn);
setdate(value);
}
//////////////////////////////////////////
matrix::matrix(matrix &other)
{row=other.row;
column=other.column;
pdate=NULL;
init(row,column);
memcpy(pdate,other.pdate,sizeof(double)*column*row);
}
/////////////////////////////////////////////
matrix::~matrix()
{delete [] pdate;
pdate=NULL;
}
///////////////////////////////////////////
bool matrix::setElement(int n,int m,double value)
{ if(n<0 || m<0 || n>row||m>column) return false;
if(pdate==NULL) return false;
pdate[(n-1)*column+(m-1)]=value;
for(int i=0;i<row;i++)
{ cout<<endl;
for(int j=0;j<column;j++)
cout<<*(pdate+j+i*column)<<" ";
}
cout<<endl;
return true;
}
///////////////////////////////////////////////
double matrix::getElement(int n,int m) const
{assert(n>=0 && m>=0 && n<=row && m<=column) ;
assert(pdate!=NULL);
return pdate[(n-1)*column+(m-1)];
}
//////////////////////////////////////////////
int matrix::getRow()
{return row;
}
////////////////////////////////////////////////
int matrix::getColumn()
{return column;
}
//////////////////////////////////////////
int matrix::getRowVector(int n,double *pvector)
{
for(int i=1;i<=column;i++)
pvector[i-1]=getElement(n,i);
return column;
}
//////////////////////////////////////////////
int matrix::getColumnVector(int n,double *pvector)
{for(int i=1;i<=row;i++)
pvector[i-1]=getElement(i,n);
return row;
}
/////////////////////////////////////////////
double *matrix::getdate() const
{return pdate;
}
//////////////////////////////////////////////
matrix &matrix::operator=(matrix &other)
{
memcpy(pdate,other.pdate,sizeof(double)*row*column);
return *this;
}
///////////////////////////////////////////////
bool matrix::operator==(matrix &other) const
{if(row!=other.row || column!=other.column)
{cout<<"这两个矩阵不相等"<<endl;
return false;
}
else
for(int i=1;i<=column*row;i++)
if(pdate[i-1]!=other.pdate[i-1])
{cout<<"这两个矩阵不相等"<<endl;
return false;
}
cout<<"这两个矩阵相等"<<endl;
return true;
}
////////////////////////////////////////////
matrix matrix::operator+(matrix &other)
{ if(row!=other.row || column!=other.column)
{cout<<"这两个矩阵行列值不相等,不能相加"<<endl;
return *this ;
}
matrix result(row,column);
for(int i=1;i<=row*column;i++)
result.pdate[i-1]=pdate[i-1]+other.pdate[i-1];
return result;
}
///////////////////////////////////////////////
matrix matrix::operator-(matrix &other)
{if(row!=other.row || column!=other.column)
{cout<<"这两个矩阵行列值不相等,不能相减"<<endl;
return *this ;
}
matrix result(row,column);
for(int i=1;i<=row*column;i++)
result.pdate[i-1]=pdate[i-1]-other.pdate[i-1];
return result;
}
////////////////////////////////////////////////
matrix matrix::operator*(matrix &other)
{if(column!=other.row)
{cout<<"第一个矩阵的列值和第二个矩阵的行值相等,不能相乘"<<endl;
return *this ;
}
matrix result(row,other.column);
double sum;
for(int i=1;i<=row;i++)
{for(int j=1;j<=other.column;j++)
{sum=0;
for(int k=1;k<=column;k++)
sum+=getElement(i,k)*other.getElement(k,j);
result.setElement(i,j,sum);
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void matrix::print()
{for(int i=0;i<row*column;i++)
{ cout<<pdate[i]<<" ";
if((i+1)%column==0) cout<<'\n';
}
}
//////////////////////////////////
matrix matrix::operator *(double k)
{matrix result(row,column);
for(int i=1;i<=row;i++)
for(int j=1;j<=column;j++)
result.setElement(i,j,this->getElement(i,j)*k);
return result;
}
////////////////////////////////////
matrix matrix::operator /(double k)
{matrix result(row,column);
for(int i=1;i<=row;i++)
for(int j=1;j<=column;j++)
result.setElement(i,j,this->getElement(i,j)/k);
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -