ps111111
//Tao lop phan so
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<math.h>
//---------------------------------
class phanso
{
private:
long ts,ms;
public:
phanso();
phanso(long m, long n);
void nhap();
void in();
phanso dd();
phanso nd();
phanso tg();
phanso operator+(phanso ps);
phanso operator-(phanso ps);
phanso operator*(phanso ps);
phanso operator/(phanso ps);
friend long ucln(long m,long n);
friend void bd(phanso ps);
};
//--------------------------------------
phanso::phanso()
{ts=0;ms=1;}
//-------------------------------------
phanso::phanso(long m,long n)
{
ts=m;ms=n;
if(n==0){cout<<"
Ko ton tai phan so";n=1;}
}
//--------------------------------------
void phanso::nhap()
{
while(1)
{
cout<<"
TS: ";cin>>ts;
cout<<"
MS: ";cin>>ms;
if(ms==0)
{
cout<<"
Yeu cau nhap lai:";
continue;
}
else break;
}
}
//--------------------------------------
void phanso::in()
{
if(ms==1){cout<<"TS: "<<ts;}
else{
cout<<"
TS: "<<ts<<" "<<"MS:"<<ms;
}
}
//----------------------------------------
phanso phanso::dd()
{
long m=-ts;
long n=ms;
return phanso(m,n);
}
//-----------------------------------------
phanso phanso::nd()
{
long m=ms;
long n=ts;
if(n==0){cout<<"
Ko ton tai phan so";n=1;}
return phanso(m,n);
}
//------------------------------------------
long ucln(long m,long n)
{
if(n==0) return m;
else{
return ucln(n,m%n);
}
}
//-----------------------------------------
phanso phanso::tg()
{
long m=this->ts;
long n=this->ms;
long u=ucln(m,n);
m=m/u;
n=n/u;
return phanso(m,n);
}
//-------------------------------------------
phanso phanso:: operator +(phanso ps)
{
phanso mps;
mps.ts=this->ts;
mps.ms=this->ms;
long u=ucln(mps.ms,ps.ms);
long msc=(mps.ms/u)*ps.ms;
long tsc=((msc/mps.ms)*mps.ts)+((msc/ps.ms)*ps.ts);
mps=phanso(tsc,msc);
return mps.tg();
}
//------------------------------------------------
phanso phanso:: operator -(phanso ps)
{
phanso mps;
mps.ts=this->ts;
mps.ms=this->ms;
long u=ucln(mps.ms,ps.ms);
long msc=(mps.ms/u)*ps.ms;
long tsc=((msc/mps.ms)*mps.ts)-((msc/ps.ms)*ps.ts);
mps=phanso(tsc,msc);
return mps.tg();
}
//---------------------------------------------------
phanso phanso::operator *(phanso ps)
{
phanso mps;
mps.ms=this->ms;
mps.ts=this->ts;
long msc=mps.ms*ps.ms;
long tsc=mps.ts*ps.ts;
mps=phanso(tsc,msc);
return mps.tg();
}
//-----------------------------------------------------
phanso phanso::operator/(phanso ps)
{
phanso mps;
mps.ms=this->ms;mps.ts=this->ts;
return(mps*ps.nd());//sd ham nhan ben tren
}
//-----------------------------------------------------
void bd(phanso ps)
{
long cs[50],sd[50];
long l,k,kk,x;
long m=ps.ts;
long n=ps.ms;
cout<<"
Phan so bieu dien: "<<m<<"/"<<n<<":";
cout<<m/n;
x=(m%n);l=kk=k=0;
if(x==0)return;
kk++;sd[kk]=x;
while(l==0)
{
x=x*10;
long i=x/n;
k++;cs[k]=i;x=x%n;
if(x==0)break;
for(long i=1;i<=kk;i++)
{
if(x==sd[i]){l=i;break;}
}
}
kk++;sd[kk]=x;
cout<<",";
if(l==0){for(long i=1;i<=k;i++){cout<<cs[i];}}
else
{
for(long i=1;i<l-1;i++)cout<<cs[i];cout<<"(";
for(long i=l;i<=k;i++)cout<<cs[i];cout<<")";
}
}
//--------------------------------------------------------
int main()
{
phanso ps1,ps2,ps;
cout<<"
Nhap 2 phan so: ";
cout<<"
Phan so thu nhat: ";ps1.nhap();
cout<<"
Phan so1 la: ";ps1.in();
cout<<"
Phan so doi cua ps1:";ps1.dd().in();
cout<<"
Phan so nghich dao cua ps1:";ps1.nd().in();
cout<<"
Phan so toi gian cua ps1:";ps1.tg().in();
bd(ps1);
cout<<"
Phan so thu hai: ";ps2.nhap();
cout<<"
Phan so2 la: ";ps2.in();
cout<<"
Phan so doi cua ps2:";ps2.dd().in();
cout<<"
Phan so nghich dao cua ps2:";ps2.nd().in();
cout<<"
Phan so toi gian cua ps2:";ps2.tg().in();
bd(ps2);
ps=ps1+ps2;
cout<<"
Tong cua 2 phan so la:";ps.in();bd(ps);
ps=ps1-ps2;
cout<<"
Tru cua 2 phan so la:";ps.in();bd(ps);
ps=ps1*ps2;
cout<<"
Tich cua 2 phan so la:";ps.in();bd(ps);
ps=ps1/ps2;
cout<<"
Thuong cua 2 phan so la:";ps.in();bd(ps);
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro