bai1-week3

// bai tap phan so

#include<iostream.h>

#include<math.h>

#include<conio.h>

class PS

{

int ts,ms;

public:

void nhap();

void in();

void toigian();

int tru(PS);

};

void PS::nhap()

{

cout<<"nhap tu so: ";cin>>ts;

cout<<"nhap mau so: ";

do

{ cin>>ms;

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

while(ms==0);

toigian();

}

void PS::toigian()

{

int a=abs(ts);int b=abs(ms);

if(a!=0)

{

while(a!=b)

{if(a>b) a=a-b;

else b=b-a;

}

ts/=b;

ms/=b;

}

}

void PS::in()

{

if(ts==0)cout<< 0;

else if(ms==1)cout<< ts;

else if(ms==-1)cout<< -ts;

else if(ts*ms>0)cout<< abs(ts)<<"/"<<abs(ms);

else cout<< -abs(ts)<<"/"<<abs(ms);

}

int PS::tru(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++)

{a[i].nhap() ; cout<<'

';}

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

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

if(a[j].tru(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++)

{ a[i].in();cout<<' '; }

getch();

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: #business