Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

KTLT-Luu Hong Viet

#include

#include

#include

class matrix{

int row; //so hang cua ma tran

int column; //so cot cua ma tran

float **p; //con tro cua con tro tro den cac phan tu cua ma tran

public:

matrix():row(0),column(0),p(0){} //ham tao khong tham so

matrix(int a,int b,float d=0.0); //khai bao ham tao co tham so voi 1 tham so mac dinh

matrix(const matrix &); //khai bao ham tao ban sao

~matrix(); //khai bao ham huy

int getrow(){ //ham thanh vien lay so hang cua ma tran

return row;

}

int getcolumn(){ //ham thanh vien lay so cot cua ma tran

return column;

}

matrix &operator=(const matrix &) ; //khai bao ham nap chong toan tu gan

matrix operator+(const matrix &) const; //khai bao ham cong hai ma tran

matrix operator-(const matrix &) const; //khai bao ham tru hai ma tran

matrix operator*(const matrix &) const; //khai bao ham nhan hai ma tran

matrix operator!() const; //khai bao ham tinh nghich dao ma tran

void putmatrix(); //khai bao ham nhap ma tran

void putsquarematrix(); //khai bao ham nhap ma tran vuong

void display(); //khai bao ham hien thi ma tran

private:

void creatematrix(int a,int b){

row=a;

column=b;

p=new float*[a];

for(int i=0;i

p[i]=new float[b];

}

void destroymatrix(){

if(p!=0){

for(int i=0;i

delete [] p[i];

delete [] p;

}

}

};

matrix::matrix(int a,int b,float d){

creatematrix(a,b);

for(int i=0;i

for(int j=0;j

p[i][j]=d;

}

matrix::matrix(const matrix & A){

row=A.row;

column=A.column;

creatematrix(row,column);

for(int i=0;i

for(int j=0;j

p[i][j]=A.p[i][j];

}

matrix::~matrix(){ destroymatrix();}

matrix & matrix::operator=(const matrix & A){

if((row!=A.row)||(column!=A.column)){

destroymatrix();

creatematrix(A.row,A.column);

}

for(int i=0;i

for(int j=0;j

p[i][j]=A.p[i][j];

return *this;

}

matrix matrix::operator+(const matrix & A) const{

int i,j;

matrix B(A.row,A.column);

for(i=0;i

for(j=0;j

B.p[i][j]=p[i][j]+A.p[i][j];

return B;

}

matrix matrix::operator-(const matrix &A) const{

int i,j;

matrix B(A.row,A.column);

for(i=0;i

for(j=0;j

B.p[i][j]=p[i][j]-A.p[i][j];

return B;

}

matrix matrix::operator*(const matrix & A) const{

int i,j,k;

matrix B(row,A.column);

for(i=0;i

for(j=0;j

for(k=0;k

B.p[i][j]+=p[i][k]*A.p[k][j];

return B;

}

matrix matrix::operator!() const{

int n=row; //ma tran nay la ma tran vuong n=row=column

matrix A(n,n);

int i,j,k,r;

float tg;

for(i=0;i

for(j=0;j

if(i==j)

A.p[i][j]=1;

else

A.p[i][j]=0;

for(k=0;k

r=k;

for(i=k+1;i

if(fabs(p[i][k])>fabs(p[r][k]))

r=i;

if(fabs(p[r][k])==0){

cout

Ma tran suy bien,khong co nghich dao !";

getch();

}

for(j=0;j

tg=p[k][j];

p[k][j]=p[r][j];

p[r][j]=tg;

tg=A.p[k][j];

A.p[k][j]=A.p[r][j];

A.p[r][j]=tg;

}

tg=p[k][k];

for(j=0;j

p[k][j]/=tg;

A.p[k][j]/=tg;

}

for(i=0;i

if(i!=k){

tg=p[i][k];

for(j=0;j

p[i][j]-=tg*p[k][j];

A.p[i][j]-=tg*A.p[k][j];

}

}

}

return A;

}

void matrix::display(){

int i,j;

cout

";

for(i=0;i

for(j=0;j

cout

cout

";

}

}

void matrix::putmatrix(){

int a,b,i,j;

cout

So hang : ";

cin>>a;

cout

cin>>b;

creatematrix(a,b);

for(i=0;i

for(j=0;j

cout

Nhap phan tu a["

cin>>p[i][j];

}

}

void matrix::putsquarematrix(){

int n,i,j;

cout

Cap ma tran vuong n= ";

cin>>n;

creatematrix(n,n);

for(i=0;i

for(j=0;j

cout

Nhap phan tu a["

cin>>p[i][j];

}

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: #ktlt