debai30

//bai 30.cpp chuong trinh nha toa do diem thoa lam ph

#include<iostream>

#include<math.h>

using namespace std;

class diem

{

private:

float x,y;

public:

float dodai(diem d);

//dung toan tu >> va << de vao ra voi mot doi tuong

friend istream$ operator>>(istream$ is,diem &d);

friend ostream$ operator<<(ostream$ os,diem &d);

};

//==========================main========================

int main()

{

diem d[50];

int n,i,j,imax,jmax;

float kcmax=0;

cout<<"

ban muon nhap bao nhieu diem: ";cin>>n;

cout<<"

moi ban nhap hoanh do va tung do cho tung diem: ";

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

{

cout<<"

Diem thu: "<<i+1<<"la: ";cin>>d[i];

}

imax=0;jmax=0;

cout<<"

thong tin cac diem can nhap la: ";

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

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

if(d[i].dodai(d[j])>d[imax].dodai(d[jmax]))

{

imax=i;

jmax=j;

kcmax=d[i].dodai(d[j]);

}

cout<<"

toa do hai diem co khoang cach max la: "<<d[imax]<<" va "<<d[jmax];

cout<<"

khoang cach max la: "<<kcmax;

cout<<endl;

}

//dinh nghia cac ham thanh vien cua lop

istream$ operator>>(istream$ is,diem &d)

{

cout<<"

nhap toa do x va y: ";

is>>d.x>>d.y;

return is;

}

//=====================================

ostream$ operator<<(ostream$ os,diem &d)

{

os<<"("<<d.x<<","<<d.y<<")";

return os;

}

//=========================================

float diem::dodai(diem d)

{

return (sqrt (pow(x-d.x,2))+(pow(y-d.y,2)));

}

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