bai4-week5
// ma tran
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class MT
{ int n,m;
float a[20][20];
public:
friend istream& operator>>(istream& is,MT &b);
friend ostream& operator<<(ostream& os,MT b);
friend int ktra(MT); // co the dung ham ban o ham nay
};
istream& operator>>(istream& is,MT &b)
{
cout<<"nhap so dong: ";is>>b.m;
cout<<"nhap so cot: ";is>>b.n;
for(int i=1;i<=b.m;i++)
for(int j=1;j<=b.n;j++)
{ cout<<"nhap a["<<i<<"]["<<j<<"]= ";
is>>b.a[i][j]; }
return(is);
}
ostream& operator<<(ostream& os,MT b)
{ cout<<setiosflags(ios::showpoint)<<setprecision(2); /*(2) la sau dau cham co 2-1=1 so sau dau cham */
for(int i=1;i<=b.m;i++)
for(int j=1;j<=b.n;j++)
{if(j==1) os<<"
";
os<<setw(5)<<b.a[i][j]; }
return(os);
}
int ktra(MT b)
{ int kq; float x[20][20];
if(b.n!=b.m) kq=0;
else
{
for(int i=1;i<=b.n;i++) // thanh lap 1 ma tran don vi de so sanh
for(int j=1;j<=b.n;j++)
{ x[i][j]=0;
x[i][i]=1; }
int dem=0;
for(i=1;i<=b.n;i++)
for(int k=1;k<=b.n;k++) /* chu y bien int k,ko dc viet bien j thay cho k*/
{
if((b.a[i][k]==x[i][k]))
dem=dem+1;
}
if(dem==b.n*b.n) kq=1;
else kq=0;
}
return(kq);
}
main()
{ MT b;
cout<<"
nhap ma tran can xet
";cin>>b;cout<<b; //ko dc viet ktra(b)
if(ktra(b)) cout<<"
ma tran vua nhap <la> ma tran don vi";
else cout<<"
ma tran vua nhap <khong> la ma tran don vi";
getch();
}
Bạn đang đọc truyện trên: AzTruyen.Top