bai1-week5

Màu nền
Font chữ
Font size
Chiều cao dòng

// phan so

#include<iostream.h>

#include<math.h>

#include<conio.h>

class PS

{

int ts,ms;

public:

friend istream& operator>>(istream& is,PS &a);

friend ostream& operator<<(ostream& os,PS a);

void toigian(PS);

int operator>(PS);

};

istream& operator>>(istream& is,PS &a)

{

cout<<"

nhap tu so: ";is>>a.ts;

cout<<"nhap mau so: ";

do

{ is>>a.ms;

if(a.ms==0) cout<<"nhap mau so khac 0"; }

while(a.ms==0);

int x=abs(a.ts);int y=abs(a.ms);

if(x!=0)

{

while(x!=y)

{ if(x>y) x=x-y;

else y=y-x; }

a.ts/=x;

a.ms/=x;

}

return(is);

}

ostream& operator<<(ostream& os,PS a)

{

if(a.ts==0)os<< 0;

else if(a.ms==1)os<< a.ts;

else if(a.ms==-1)os<< -a.ts;

else if(a.ts*a.ms>0)os<< abs(a.ts)<<"/"<<abs(a.ms);

else os<< -abs(a.ts)<<"/"<<abs(a.ms);

return(os);

}

int PS::operator>(PS a)

{ PS kq;

kq.ts=ts*a.ms-ms*a.ts;

kq.ms=ms*a.ms;

if((kq.ts*kq.ms)>0)return(1);

else return(0);

}

main()

{PS a[9];int i,j,n;

cout<<"nhap n: ";cin>>n;

for(i=0;i<n;i++)

{ cin>>a[i] ; cout<<'

';}

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

if(a[j]>a[i])

{ PS t=a[i];

a[i]=a[j];

a[j]=t;

}

cout<<" mang sau khi sap xep giam dan:

";

for(i=0;i<n;i++)

{ cout<<a[i]<<' '; }

getch();

}

Bạn đang đọc truyện trên: Truyen2U.Pro

#business