bai4_matran_f
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#include"iomanip.h"
#include"math.h"
class matran
{
private:
int a[50][50];
int n,m;
public:
friend void doc(char *tentep,matran &mt1,matran &mt2);
friend void ghi(char *tentep);
void nhap();
void xuat();
int tongdcchinh();
int tongdcphu();
void sxtang_cot();
void sxdcchinh();
friend void tongmt(matran mt1,matran mt2,matran &t,int &kt);
friend void tichmt(matran mt1,matran mt2,matran &t,int &kt);
};
void matran::nhap()
{
int i,j;
cout<<"
Nhap so hang n="; cin>>n;
cout<<"
Nhap so cot m="; cin>>m;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
}
void doc(char *tentep,matran &mt1,matran &mt2)
{
FILE *f;
f=fopen(tentep,"rb");
fread(&mt1,sizeof(matran),1,f);
fread(&mt2,sizeof(matran),1,f);
fclose(f);
}
void taofile(char *tentep)
{
FILE *f;
matran tg;
f=fopen(tentep,"wb");
cout<<"
Nhap ma tran A:";
tg.nhap();
fwrite(&tg,sizeof(matran),1,f);
cout<<"
Nhap ma tran B:";
tg.nhap();
fwrite(&tg,sizeof(matran),1,f);
fclose(f);
}
void matran::xuat()
{
int i,j;
for(i=1;i<=n;i++)
{
cout<<"
";
for(j=1;j<=m;j++)
cout<<setw(5)<<a[i][j];
}
}
void xuatfile(char *tentep)
{
FILE *f;
matran tg;
f=fopen(tentep,"rb");
fread(&tg,sizeof(matran),1,f);
cout<<"
Ma tran A la:";
tg.xuat();
fread(&tg,sizeof(matran),1,f);
cout<<"
Ma tran B la:";
tg.xuat();
fclose(f);
}
int matran::tongdcchinh()
{
int i,j,s;
s=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(a[i][j]==a[i][i])
s=s+a[i][i];
return s;
}
int matran::tongdcphu()
{
int i,j,s1;
s1=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(a[i][j]==a[i][n-i+1])
s1=s1+a[i][n-i+1];
return s1;
}
void matran::sxtang_cot()
{
int i,j,tg,k;
for(j=1;j<=m;j++)
for(i=1;i<n;i++)
for(k=i+1;k<=n;k++)
if(a[i][j]>a[k][j])
{
tg=a[i][j];
a[i][j]=a[k][j];
a[k][j]=tg;
}
}
void matran::sxdcchinh()
{
int i,j,tg;
for(i=1;i<n;i++)
for(j=i+1;j<=m;j++)
if(a[i][i]>a[j][j])
{
tg=a[i][i];
a[i][i]=a[j][j];
a[j][j]=tg;
}
}
void tongmt(matran mt1,matran mt2,matran &t,int &kt)
{
int i,j;
if(mt1.n==mt2.n && mt1.m==mt2.m)
{
kt=1;
for(i=1;i<=mt1.n;i++)
for(j=1;j<=mt1.m;j++)
t.a[i][j]=mt1.a[i][j]+mt2.a[i][j];
t.n=mt1.n;
t.m=mt1.m;
}
else
kt=0;
}
void tichmt(matran mt1,matran mt2,matran &t,int &kt)
{
int i,j,k;
if(mt1.m==mt2.n)
{
kt=1;
for(i=1;i<=mt1.n;i++)
for(j=1;j<=mt2.m;j++)
{
t.a[i][j]=0;
for(k=1;k<=mt2.n;k++)
t.a[i][j]=t.a[i][j]+mt1.a[i][k]*mt2.a[k][j];
}
t.n=mt1.n;
t.m=mt2.m;
}
else
kt=0;
}
void main()
{
char *tentep1;
matran mt1,mt2,mt3,tong,tich;
int kt;
clrscr();
cout<<"
Nhap ten file:";gets(tentep1);
taofile(tentep1);
cout<<"
Du lieu tu file la:";
xuatfile(tentep1);
cout<<"
";
doc(tentep1,mt1,mt2);
cout<<"
Du lieu tu lop:";
cout<<"
Ma tran A:";
mt1.xuat();
cout<<"
Ma tran B:";
mt2.xuat();
cout<<"
Tong cac phan tu tren duong cheo chinh ma tran A la:"<<mt1.tongdcchinh();
cout<<"
Tong cac phan tu tren duong cheo phu ma tran A la:"<<mt1.tongdcphu();
mt1.sxtang_cot();
cout<<"
Sap xep ma tran tang dan tren cot ma tran A:";
mt1.xuat();
mt2.sxdcchinh();
cout<<"
Sap xep duong cheo chinh cua ma tran B tang dan:";
mt2.xuat();
tongmt(mt1,mt2,tong,kt);
if (kt==1)
{
cout<<"
Ma tran tong la:";
tong.xuat();
}
else
cout<<"
Khong tinh dc tong";
tichmt(mt1,mt2,tich,kt);
if(kt==1)
{
cout<<"
Ma tran tich la:";
tich.xuat();
}
else
cout<<"
Khong tinh dc tich";
getch();
}
Bạn đang đọc truyện trên: AzTruyen.Top