题目描述
上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改:1、增加自写的析构函数;2、将getDisTo方法的参数修改为getDisTo(const Point &p);3、根据下面输出的内容修改相应的构造函数。
然后在主函数中根据用户输入的数目建立Point数组,求出数组内距离最大的两个点之间的距离值。
输入
测试数据的组数 t
第一组点的个数
第一个点的 x 坐标 y坐标
第二个点的 x坐标 y坐标
............
输出
输出第一组距离最大的两个点以及其距离(存在多个距离都是最大值的情况下,输出下标排序最前的点组合。比如如果p[0]和p[9]、p[4]和p[5]之间的距离都是最大值,那么前一个是答案,因为p[0]排序最前)
...........
在C++中,输出指定精度的参考代码如下:
#include <iostream>
#include <iomanip> //必须包含这个头文件
using namespace std;
void main( )
{ double a =3.141596;
cout<<fixed<<setprecision(3)<<a<<endl; //输出小数点后3位
样例查看模式
正常显示查看格式
输入样例1 <-复制
输出样例1
语言: 编译选项
主题:
#include<iostream>
#include <iomanip> //必须包含这个头文件
using namespace std;
class point
{
double x, y;
public:
point();
point(double x_value, double y_value);
~point();
double getX();
double getY();
void setXY(double x1, double y1) { x = x1; y = y1; }
void setX(double x_value);
void setY(double y_value);
double getDisTo(point& p);
};
point::point()
{
x = 0;
y = 0;
cout << "Constructor." << endl;
}
point::point(double x_value, double y_value)
{
x = x_value;
y = y_value;
cout << "Constructor." << endl;
}
point::~point()
{
cout << "Distructor." << endl;//析构函数
}
double point::getX()
{
return x;
}
double point::getY()
{
return y;
}
void point::setX(double x_value)
{
x = x_value;
}
void point::setY(double y_value)
{
y = y_value;
}
double point::getDisTo(point& p)
{
return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
int main()
{
int t;
cin >> t;
int index1 = 0;
int index2 = 0;
//数目建立Point数组,求出数组内距离最大的两个点之间的距离值。
while (t--)
{
int times = 0;
cin >> times;
point* p = new point[times];//创建动态数组
double x, y;
for (int i = 0; i < times; i++)
{
cin >> x;
cin >> y;
p[i].setXY(x, y);//数据录入
}
double max = 0;
for (int i = 0; i < times; i++)
{
for (int j = i + 1; j < times; j++)
{
if (p[i].getDisTo(p[j])>max)//距离最大的
{
max = p[i].getDisTo(p[j]);
index1 = i;
index2 = j;
}
}
}
cout << "The longeset distance is " << fixed << setprecision(2) << max << ",between p[" << index1 << "] and p[" << index2 << "]." << endl;
delete[] p;
}
return 0;
}