#include
閱讀以下說明和c++代碼,填充代碼中的空缺,將解答填入答題紙的對應(yīng)欄內(nèi)。深夜成人在线,chinese国产一区二区,欧美精品乱码,日韩欧美在线视频免费观看,国产午夜不卡,日韩av影院在线,五月天婷婷国产精品
【說明】
下面的程序用來計(jì)算并尋找平面坐標(biāo)系中給定點(diǎn)中最近的點(diǎn)對(若存在多對,則輸出其中的一對即可)。程序運(yùn)行時,先輸入點(diǎn)的個數(shù)和一組互異的點(diǎn)的坐標(biāo),通過計(jì)算每對點(diǎn)之間的距離,從而確定出距離最近的點(diǎn)對。例如,在圖 5-1 所示的8個點(diǎn)中,點(diǎn)(1,1) 與(2,0.5)是間距最近的點(diǎn)對。
【C++代碼】
#include <iostream>
#include <cmath>
using namespace std;
class GPoint {
private:
double x ,y;
public:
void setX(double x) { this->x = x; }
void setY(double y) { this->y = y; }
double getX() { return this->x; }
double getY() { return this->y; }
}
class ComputeDistance {
public:
double distance(GPoint a ,GPoint b) {
return sqrt((a.getX() - b.getX())*(a .getX() - b.getX())
+ (a.getY() - b.getY())*(a.getY() - b.getY()));
}
};
int main ()
{
int i ,j ,numberOfPoints = 0;
cout << "輸入點(diǎn)的個數(shù):";
cin >> numberOfpoints;
(1)points = new GPoint[numberOfPoints]; //創(chuàng)建保存點(diǎn)坐標(biāo)的數(shù)組
memset(points ,0 ,sizeof(points));
cout << "輸入" << numberOfPoints<< "個點(diǎn)的坐標(biāo) :";
for (i = 0; i < numberOfPoints; i++) {
double tmpx ,tmpy;
cin>>tmpx>>tmpy;
points[i] .setX(tmpx);
points[i] .setY(tmpy);
}
(2)computeDistance = new ComputeDistance();
int p1 = 0 ,p2=1; //p1 和 p2 用于表示距離最近的點(diǎn)對在數(shù)組中的下標(biāo)
double shortestDistance = computeDistance->distance(points[p1] ,points[p2]);
//計(jì)算每一對點(diǎn)之間的距離
for (i = 0; i < numberOfPoints; i++) {
for (j = i+1; j <(3); j++) {
double tmpDistance = computeDistance->(4);
if ( (5) ) {
p1 = i; p2 = j;
shortestDistance = tmpDistance;
}
}
}
cout << "距離最近的點(diǎn)對是: (" ;
cout << points[p1] .getX() << "," << points[pl] .gety()<<")和(" ;
cout << points [p2].getX () << "," << points [p2].gety () << ")" << endl;
delete computeDistance;
return 0;
}
相關(guān)試題