aaaaaaaaa
//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);
};
//-------------
phanso::phanso()
{ts=0;ms=1;}
//-------------
phanso::phanso(long m,long n)
{
// if(n==0){cout<<"
Ko ton tai phan";}
//else{ts=m;ms=n;}
ts=m;ms=n;
}
//---------------
void phanso::nhap()
{
phanso ps;
cout<<"
TS: ";cin>>ts;
cout<<"
MS: ";cin>>ms;
if(ms==0){cout<<"
Yeu cau nhap lai:";ps.nhap();phanso(ts,ms);}
}
//----------------
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;
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.ts=this->ts;
mps.ms=this->ms;
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.ts=this->ts;
mps.ms=this->ms;
return (mps*ps.nd()).tg();
}
//-----------
void bd(phanso ps)
{
long m,n;
m=ps.ts;
n=ps.ms;
long k,kk,l,cs[50],sd[50],x;
cout<<"
Bieu dien dang thap phan cua "<<m<<"/"<<n<<":";
cout<<m/n;
if(m%n==0)return;
l=kk=k=0;
x=m%n;
kk++;sd[kk]=x;
while(l==0)
{
x=x*10;
k++;cs[k]=x/n;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 PS:
";
cout<<"
PS1";ps1.nhap();
cout<<"
PS1 la:";ps1.in();
cout<<"
DPS1:";ps1.dd().in();
cout<<"
NPS1:";ps1.nd().in();
cout<<"
TPS1:";ps1.tg().in();
bd(ps1);
cout<<"
PS2:";ps2.nhap();
cout<<"
PS2 la:";ps2.in();
cout<<"
DPS2:";ps2.dd().in();
cout<<"
NPSps2:";ps2.nd().in();
cout<<"
TPS2:";ps2.tg().in();
bd(ps2);
ps=ps1+ps2;
cout<<"
Tong:";bd(ps);
ps=ps1-ps2;
cout<<"
Tru:";bd(ps);
ps=ps1*ps2;
cout<<"
Tich:";bd(ps);
ps=ps1/ps2;
cout<<"
Thuong:";bd(ps);
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro