phan so
#include <conio.h>
#include <iostream.h>
#include <math.h>
#include <stdio.h>
class phanso
{
int ts;
int ms;
public:
// void nhap();
// void in();
friend istream & operator>>(istream & is,phanso &);
friend ostream & operator<<(ostream & os,phanso &);
void toigian();
int operator <=(phanso);
};
// void phanso::nhap()
istream & operator>>(istream & is,phanso & a)
{
cout<<"
nhap tu so: "; is>>a.ts;
cout<<"
nhap mau so: "; is>>a.ms;
return is;
}
// void phanso::in()
ostream & operator<<(ostream & os,phanso & a)
{
os<<a.ts<<"/"<<a.ms;
return os;
}
void phanso::toigian()
{
int a=abs(ts);
int b=abs(ms);
if(a!=0)
{
while(a!=b)
{ if(a>b) a=a-b;
b=b-a;
}
ts/=a;
ms/=a;
cout<<ts<<"/"<<ms;
}
}
int phanso::operator <= (phanso x)
{
int k1=ts*x.ms-ms*x.ts;
// int k2=ms*x.ms;
if(k1<=0) return 1;
else return 0;
}
void main()
{
phanso a[20];
int n;
clrscr();
cout<<"
nhap so phan so: "; cin>>n;
cout<<"
nhap cac phan so :";
for(int i=0;i<n;i++)
{ cout<<"
nhap phan so thu "<<i+1;cin>>a[i];
cout<<"
phan so toi gian la:"; a[i].toigian();
}
for(i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if ((a[i]<=a[j])==1)
{
phanso tg=a[i];
a[i]=a[j];
a[j]=tg;
}
cout<<"
sap xep cac phan so theo thu tu giam dan: ";
for(i=0;i<n;i++) cout<<a[i]<<" ";
getch();
}
Bạn đang đọc truyện trên: AzTruyen.Top