PHP Code:
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#include<stdlib.h>
class MT
{
private:
int hang,cot;
double a[20][20];
public:
friend ostream &operator<<(ostream &os,const MT &x);
friend istream &operator>>(istream &is,MT &x);
MT operator+(const MT& x2);
MT operator-(const MT& x2);
MT operator*(const MT& x2);
MT operator!();
void tim();
void dt();
};
MT MT::operator+(const MT &x2)
{
if(this->hang!=x2.hang||this->cot!=x2.cot)
{
cout<<"\n Khong thuc hien duoc phep cong ma tran!";
getch();
return *this;
}
else
{
MT x;
hang=x.hang=this->hang;
cot=x.cot=this->cot;
for(int i=0;i<hang;i++)
for(int j=0;j<cot;j++)
x.a[i][j]=this->a[i][j]+x2.a[i][j];
return x;
}
}
MT MT::operator-(const MT &x2)
{
if(this->hang!=x2.hang||this->cot!=x2.cot)
{
cout<<"\n Khong thuc hien duoc phep tru ma tran!";
getch();
return *this;
}
else
{
MT x;
hang=x.hang=this->hang;
cot=x.cot=this->cot;
for(int i=0;i<hang;i++)
for(int j=0;j<cot;j++)
x.a[i][j]=this->a[i][j]-x2.a[i][j];
return x;
}
}
MT MT::operator*(const MT &x2)
{
if(this->cot!=x2.hang)
{
cout<<"\n Khong thuc hien duoc phep nhan ma tran!";
getch();
return *this;
}
else
{
MT x;
hang=x.hang=this->hang;
cot=x.cot=x2.cot;
for(int i=0;i<hang;i++)
for(int j=0;j<cot;j++)
{
x.a[i][j]=0;
for(int k=0;k<x2.hang;k++)
x.a[i][j]+=this->a[i][k]*x2.a[k][j];
}
return x;
}
}
MT MT::operator!()
{
if(this->hang==this->cot)
{
MT x;
int i,j,k,r,n;
double tg;
n=x.hang=this->hang;
n=x.cot=this->cot;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==j) x.a[i][j]=1;
else x.a[i][j]=0;
for(k=0;k<n;k++)
{
r=k;
for(i=k+1;i<n;i++)
if(abs(this->a[i][k])>abs(this->a[r][k])) r=i;
/* if(abs(this->a[r][k])<1.0E-8)
{
cout<<"\n Ma tran suy bien,khong co nghich dao";
getch();
return *this;
} */
for(j=0;j<n;j++)
{
tg=this->a[k][j];
this->a[k][j]=this->a[r][j];
this->a[r][j]=tg;
tg=x.a[k][j];
x.a[k][j]=x.a[r][j];
x.a[r][j]=tg;
}
tg=this->a[k][k];
for(j=0;j<n;j++)
{
this->a[k][j]/=tg;
x.a[k][j]/=tg;
}
for(int i=0;i<n;i++)
if(i!=k)
{
tg=this->a[i][k];
for(j=0;j<n;j++)
{
this->a[i][j]-=tg*this->a[k][j];
x.a[i][j]-=tg*x.a[k][j];
}
}
}
return x;
}
else
{
cout<<"Khong thuc hien duoc";
return *this;
}
}
void MT::tim()
{
if(this->hang==this->cot)
cout<<*this;
}
void MT::dt()
{
int m,i,j,k,heso1=1,heso2=1,l,tg,c,u,v,heso,s,n;
double dt;
if(this->hang==this->cot)
{
m=this->hang;n=this->cot;
if(n==1) cout<<a[0][0];
if(n==2) cout<<a[0][0]*a[1][1]-a[1][0]*a[0][1];
if(n>2){ m=n+1;
for(j=0;j<n-1;j++)
{ m--;
for(i=0;i<m;)
{ if(a[i][j]==0) i++;
else{ k=i;i++;
if(a[i][j]!=0&&i<m){ if(a[k][j]%a[i][j]==0){ l=-a[k][j]/a[i][j];
for(c=0;c<n;c++)
{ a[i][c]=l*a[i][c]+a[k][c];
tg=a[k][c];a[k][c]=a[i][c];a[i][c]=tg;
a[k][c]=-a[k][c];
}
heso1=l*heso1;
}
else{ u=a[k][j];
v=a[i][j];
for(c=0;c<n;c++)
{ a[i][c]=-u*a[i][c]+v*a[k][c];
tg=a[k][c];a[k][c]=a[i][c];a[i][c]=tg;
a[k][c]=-a[k][c];
}
heso2=heso2*(-u);
}
}
else{ if(i==m-1){ for(c=0;c<n;c++)
{ tg=a[k][c];a[k][c]=a[i][c];a[i][c]=tg;
a[k][c]=-a[k][c];
}
}
}
}
}
}
cout<<"\n Ma tran tam giac tren:";
for(i=0;i<n;i++)
{ cout<<"\n";
for(j=0;j<n;j++)
cout<<setw(6)<<a[i][j];
}
cout<<"\n";
heso=1;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ heso=heso1*heso2;}
s=1;
cout<<"\n Dinh thuc:";
for(j=0,i=n-1;i>=0;i--,j++)
{ s=s*a[i][j];}
dt=s/heso;
cout<<dt;
}
}
}
istream &operator>>(istream &is,MT &x)
{
cout<<"\n Nhap so hang va so cot cua ma tran:";
is>>x.hang>>x.cot;
randomize();
for(int i=0;i<x.hang;i++)
for(int j=0;j<x.cot;j++)
{x.a[i][j]=random(5);
cout<<"\n";
cout<<"a["<<i+1<<"]"<<"["<<j+1<<"]:"<<x.a[i][j];
}
return is;
}
ostream &operator<<(ostream &os,const MT &x)
{
os<<setprecision(2)<<setiosflags(ios::showpoint);
for(int i=0;i<x.hang;i++)
{
os<<"\n";
for(int j=0;j<x.cot;j++)
os<<setw(6)<<x.a[i][j];
}
os<<"\n";
return os;
}
void Menu()
{
clrscr();
cout<<"\n Lua chon cong viec theo so \n";
cout<<"\n 1.Nhap ma tran";
cout<<"\n 2.Hien ma tran";
cout<<"\n 3.Cong 2 ma tran";
cout<<"\n 4.Hieu 2 ma tran";
cout<<"\n 5.Tich 2 ma tran";
cout<<"\n 6.Tim ma tran vuong";
cout<<"\n 7.Tim dinh thuc";
cout<<"\n 8.Tim ma tran nghich dao";
cout<<"\n 0.Thoat \n";
}
void main()
{
MT d,e,s;
while(1)
{
Menu();
int k=getch();
clrscr();
switch(k)
{
case '1':
cout<<"\nMa tran 1\n";
cin>>d;
cout<<"\nMa tran 2\n";
cin>>e;
break;
case '2':
cout<<d<<e;
break;
case '3':
cout<<"Tong 2 ma tran ban vua nhap la:\n";
cout<<d+e;
break;
case '4':
cout<<"Hieu 2 ma tran ban vua nhap la:\n";
cout<<d-e;
break;
case '5':
cout<<"Tich 2 ma tran ban vua nhap la:\n";
cout<<d*e;
break;
case '6':
cout<<"Ma tran vuong:\n";
d.tim();e.tim();
break;
case '7':
cout<<"\nDinh thuc ma tran 1\n";
d.dt();
cout<<"\nDinh thuc ma tran 2\n";
e.dt();
break;
case '8':
cout<<"\n Ma tran nghich dao\n";
cout<<!(d);
cout<<"\n";
cout<<!(e);
break;
}
if(k=='0') break;
getch();
}
}
Lúc em chạy nó báo lỗi:
Illegal use of floating point
Lỗi này là lỗi gì hả các anh. Khi em sửa khai báo double a[10][10] thành int a[10][10] thì nó lại hết.