bai1_ps
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#include"math.h"
#include"iomanip.h"
class phanso
{
private:
int ts,ms;
public:
void nhap(phanso &a);
void xuat(phanso &a);
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(phanso &a)
{
cout<<"
Nhap tu so:"; cin>>a.ts;
cout<<"
Nhap mau so:"; cin>>a.ms;
}
void phanso::xuat(phanso &a)
{
cout<<a.ts<<"/"<<a.ms;
}
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()
{
phanso ps,a,b;
clrscr();
cout<<"
Nhap phan so a:";ps.nhap(a);
cout<<"
Nhap phan so b:";ps.nhap(b);
cout<<"
a=";ps.xuat(a);
cout<<"
b=";ps.xuat(b);
cout<<"
Tinh toan cho 2 phan so:
";
cout<<"
a + b ="; ps.xuat(tongps(a,b));
cout<<"
a - b ="; ps.xuat(hieups(a,b));
cout<<"
a x b ="; ps.xuat(tichps(a,b));
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro