bai5_matranvt_f
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#include"iomanip.h"
#include"math.h"
class matran;
class vecto;
class matran
{
private:
int a[50][50];
int n,m;
public:
friend void docmt(char *tentep,matran &mt);
friend void ghimt(char *tentep);
void nhapmt();
void xuatmt();
friend void tichvtmt(matran mt,vecto vt,vecto &t,int &kt);
friend void tichmtvt(matran mt,vecto vt,vecto &t,int &kt);
};
class vecto
{
private:
int a[50];
public:
friend void docvt(char *tentep,vecto &vt);
friend void ghivt(char *tentep);
void nhapvt();
void xuatvt();
friend void tichvtmt(matran mt,vecto vt,vecto &t,int &kt);
friend void tichmtvt(matran mt,vecto vt,vecto &t,int &kt);
};
void matran::nhapmt()
{
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 docmt(char *tentep,matran &mt)
{
FILE *f;
f=fopen(tentep,"rb");
fread(&mt,sizeof(matran),1,f);
fclose(f);
}
void taofile_mt(char *tentep)
{
FILE *f;
matran tg;
f=fopen(tentep,"wb");
cout<<"
Nhap ma tran:";
tg.nhapmt();
fwrite(&tg,sizeof(matran),1,f);
fclose(f);
}
void matran::xuatmt()
{
int i,j;
for(i=1;i<=n;i++)
{
cout<<"
";
for(j=1;j<=m;j++)
cout<<setw(5)<<a[i][j];
}
}
void xuatfile_mt(char *tentep)
{
FILE *f;
matran tg;
f=fopen(tentep,"rb");
fread(&tg,sizeof(matran),1,f);
cout<<"
Ma tran la:";
tg.xuatmt();
fclose(f);
}
void vecto::nhapvt()
{
cout<<"
Nhap so phan tu cho vecto:"; cin>>n;
for(i=1;i<=n;i++)
{
cout<<"
a["<<i<<"]=";
cin>>a[i];
}
}
void docvt(char *tentep,vecto &vt)
{
FILE *f;
f=fopen(tentep,"rb");
fread(&vt,sizeof(vecto),1,f);
fclose(f);
}
void taofile_vt(char *tentep)
{
FILE *f;
vecto tg;
f=fopen(tentep,"wb");
cout<<"
Nhap vecto:";
tg.nhapvt();
fwrite(&tg,sizeof(vecto),1,f);
fclose(f);
}
void vecto::xuatvt()
{
for(i=1;i<=n;i++)
cout<<setw(5)<<a[i];
}
void xuatfile_vt(char *tentep)
{
FILE *f;
vecto tg;
f=fopen(tentep,"rb");
fread(&tg,sizeof(vecto),1,f);
cout<<"
Vec to la:";
tg.xuatvt();
fclose(f);
}
void tichmtvt(matran mt,vecto vt,vecto &t,int &kt)
{
int i,j;
if(vt.n==mt.n)
{
kt=1;
for(i=1;i<=mt.n;i++)
{ t.a[i]=0;
for(j=1;j<=mt.m;j++)
t.a[i]=t.a[i]+mt.a[i][j]*vt.a[j];
}
t.n=mt.m;
}
else
kt=0;
}
void tichvtmt(matran mt,vecto vt,vecto &t,int &kt)
{
int i,j;
if(vt.n==mt.n)
{
kt=1;
for(j=1;j<=mt.m;j++)
{ t.a[j]=0;
for(i=1;i<=mt.n;i++)
t.a[j]=t.a[j]+vt.a[i]*mt.a[i][j];
}
t.n=mt.m;
}
else
kt=0;
}
void main()
{
char *tentep1;char *tentep2;
matran mt;
vecto vt,t;
int kt;
clrscr();
cout<<"
Nhap ten file:";gets(tentep1);
taofile_mt(tentep1);
cout<<"
Nhap ten file:"; gets(tentep2);
taofile_vt(tentep2);
cout<<"
Du lieu tu file:";
xuatfile_mt(tentep1);
xuatfile_vt(tentep2);
cout<<"
";
docmt(tentep1,mt);
docvt(tentep2,vt);
cout<<"
Du lieu tu lop:";
cout<<"
Ma tran la:";
mt.xuatmt();
cout<<"
Vecto la:";
vt.xuatvt();
tichmtvt(mt,vt,t,kt);
if (kt==1)
{
cout<<"
Tich cua ma tran va vecto la:";
t.xuatvt();
}
else
cout<<"
Khong tinh dc tich";
tichvtmt(mt,vt,t,kt);
if (kt==1)
{
cout<<"
Tich cua vecto va ma tran la:";
t.xuatvt();
}
else
cout<<"
Khong tinh dc tich";
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro