bai1_phanso_f
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#include"math.h"
#include"iomanip.h"
class phanso
{
private:
int ts,ms;
public:
friend void doc(char *tentep,phanso &a,phanso &b);
friend void ghi(char *tentep);
void nhap();
void xuat();
friend void rutgon(phanso &a);
friend phanso tongps(phanso a,phanso b);
friend phanso hieups(phanso a,phanso b);
friend phanso tichps(phanso a,phanso b);
};
void phanso::nhap()
{
cout<<"
Nhap tu so:"; cin>>ts;
cout<<"
Nhap mau so:"; cin>>ms;
}
void doc(char *tentep,phanso &a,phanso &b)
{
FILE *f;
f=fopen(tentep,"rb");
fread(&a,sizeof(phanso),1,f);
fread(&b,sizeof(phanso),1,f);
fclose(f);
}
void taofile(char *tentep)
{
FILE *f;
phanso tg;
f=fopen(tentep,"wb");
cout<<"
Nhap phan so thu nhat:";
tg.nhap();
fwrite(&tg,sizeof(phanso),1,f);
cout<<"
Nhap phan so thu hai:";
tg.nhap();
fwrite(&tg,sizeof(phanso),1,f);
fclose(f);
}
/*void ghi(char *tentep)
{
FILE *f;
f=fopen(tentep,"wb");
fflush(f);
putw(ts,f);
putw(ms,f);
fclose(f);
}*/
void phanso::xuat()
{
cout<<ts<<"/"<<ms;
}
void xuatfile(char *tentep)
{
FILE *f;
phanso tg;
f=fopen(tentep,"rb");
fread(&tg,sizeof(phanso),1,f);
cout<<"
Phan so thu nhat la:";
tg.xuat();
fread(&tg,sizeof(phanso),1,f);
cout<<"
Phan so thu hai la:";
tg.xuat();
fclose(f);
}
int ucln(int x,int y)
{
while (x!=y)
if(x>y)
x=x-y;
else
y=y-x;
return(x);
}
void rutgon(phanso &a)
{
int uc;
uc=ucln(abs(a.ts),abs(a.ms));
a.ts=a.ts/uc;
a.ms=a.ms/uc;
}
phanso tongps(phanso a,phanso b)
{
phanso tong;
tong.ts=a.ts*b.ms+b.ts*a.ms;
tong.ms=a.ms*b.ms;
rutgon(tong);
return(tong);
}
phanso hieups(phanso a,phanso b)
{
phanso hieu;
hieu.ts=a.ts*b.ms-b.ts*a.ms;
hieu.ms=a.ms*b.ms;
rutgon(hieu);
return(hieu);
}
phanso tichps(phanso a,phanso b)
{
phanso tich;
tich.ts=a.ts*b.ts;
tich.ms=a.ms*b.ms;
rutgon(tich);
return(tich);
}
void main()
{ char *tentep1;
phanso a,b,ps;
clrscr();
cout<<"
Nhap ten file:"; gets(tentep1);
taofile(tentep1);
cout<<"
Du lieu trong file la";
xuatfile(tentep1);
cout<<"
";
doc(tentep1,a,b);
cout<<"
a="; a.xuat();
cout<<"
b="; b.xuat();
cout<<"
Tinh toan cho 2 phan so:
";
cout<<"
a + b =";
ps=tongps(a,b);
ps.xuat();
cout<<"
a - b =";
ps=hieups(a,b);
ps.xuat();
cout<<"
a x b =";
ps=tichps(a,b);
ps.xuat();
/*cout<<"
Xuat phan so tu File ra man hinh:
";
cout<<"
Phan so a la:";
a.ghi(tentep1);
xuatfile(tentep1,'a');
cout<<"
Phan so b la:";
b.ghi(tentep2);
xuatfile(tentep2,'b');*/
getch();
}
Bạn đang đọc truyện trên: AzTruyen.Top