#include<iostream>
#include<stdio.h >
#include<cstdlib >
using namespace std;
class matrix
{
int const line; //矩阵的行
int const column; //矩阵的列
long double **matrip;
public:
matrix(int,int);
~matrix(){for(int i=0;i<column;i++)delete []matrip[i]; delete []matrip;}
matrix E(int,int);
void inputm();
void outputm();
matrix operator+(matrix) const;
matrix& operator=(matrix&);
};
matrix::matrix(int l,int c):line(l),column(c)
{
matrip=new long double *[line];
for(int i=0;i<line;i++)
{
matrip[i]=new long double[column];
for(int j=0;j<column;j++)
matrip[i][j]=l;
}
}
void matrix::inputm()
{
cout<<"请按行输入"<<line<<"*"<<column<<"矩阵数据"<<endl;
for(int i=0;i<line;i++)
for(int j=0;j<column;j++)
cin>>matrip[i][j];
}
void matrix::outputm()
{
cout<<"按行输出的"<<line<<"*"<<column<<"矩阵数据"<<endl;
for(int i=0;i<line;i++)
{
for(int j=0;j<column;j++)
cout<<matrip[i][j]<<" ";
cout<<endl;
}
}
matrix matrix::operator +(matrix m) const
{
if(line!=m.line||column!=m.column)
{
cout<<"error,their sizes differ";
exit(1);
}
matrix tem(line,column);
for(int i=0;i<line;i++)
for(int j=0;j<column;j++)
tem.matrip[i][j]=matrip[i][j]+m.matrip[i][j];
return tem;
}
matrix& matrix::operator =(matrix& m)
{
if(line!=m.line||column!=m.column)
{
cout<<"error,their sizes differ";
exit(1);
}
for(int i=0;i<line;i++)
for(int j=0;j<column;j++)
matrip[i][j]=m.matrip[i][j];
return *this;
}
void main()
{
matrix a(2,2),b(2,2),c(2,2);
a.inputm();
a.outputm();
b.outputm();
c=a+b;
c.outputm();
}
[此贴子已经被作者于2007-10-23 14:21:35编辑过]