D. Point_Array(类+构造+对象数组)
D. Point_Array(类+构造+对象数组)题目描述上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改:1、增加自写的析构函数;2、将getDisTo方法的参数修改为getDisTo(const Point &p);3、根据下面输出的内容修改相应的构造函数。然后在主函数中根据用户输入的数目建立Point数组,求出数组内距离最大的两个点之间的距离值。输入测试数据的组数 t第
·
D. Point_Array(类+构造+对象数组)
题目描述
![]()
上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改:
1、增加自写的析构函数;
2、将getDisTo方法的参数修改为getDisTo(const Point &p);
3、根据下面输出的内容修改相应的构造函数。
然后在主函数中根据用户输入的数目建立Point数组,求出数组内距离最大的两个点之间的距离值。
输入
测试数据的组数 t
第一组点的个数
第一个点的 x 坐标 y坐标
第二个点的 x坐标 y坐标
............
输出
输出第一组距离最大的两个点以及其距离(存在多个距离都是最大值的情况下,输出下标排序最前的点组合。比如如果p[0]和p[9]、p[4]和p[5]之间的距离都是最大值,那么前一个是答案,因为p[0]排序最前)
输入样例1 2 4 0 0 5 0 5 5 2 10 3 -1 -8 0 9 5 0 输出样例1 Constructor. Constructor. Constructor. Constructor. The longeset distance is 10.44,between p[1] and p[3]. Distructor. Distructor. Distructor. Distructor. Constructor. Constructor. Constructor. The longeset distance is 17.03,between p[0] and p[1]. Distructor. Distructor. Distructor.
该题主要考察类与对象数组与构造函数的综合应用,主要注意一些细节的处理及格式化输出
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;
class Point
{
double x, y;
public:
Point();
Point(double x_v, double y_v);
double getx();
double gety();
void setxy(double x1, double y1)
{
x = x1;
y = y1;
}
double getdisto(Point& p);
};
Point::Point()
{
x = 0;
y = 0;
}
double Point::getx()
{
return x;
}
double Point::gety()
{
return y;
}
Point::Point(double x_v, double y_v)
{
cout << "Constructor." << endl;
x = x_v;
y = y_v;
}
double Point::getdisto(Point& p)
{
double a = this->getx();
double b = this->gety();
double c = p.getx();
double d = p.gety();
return sqrt((a - c) * (a - c) + (b - d) * (b - d));
}
int main()
{
int t,index1,index2,n;
double MAXN = -1, x_v, y_v;
cin >> t;
while (t--)
{
cin >> n;
Point* p = new Point[n];
for (int i = 0; i < n; i++)//输入数据
{
cin >> x_v >> y_v;
Point t(x_v, y_v);
p[i] = t;
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)//寻找最大距离的两个点
{
if (p[i].getdisto(p[j]) > MAXN)
{
MAXN = p[i].getdisto(p[j]);
index1 = i;
index2 = j;
}
}
}
cout << fixed << setprecision(2) << "The longeset distance is " << MAXN << ",between p[" << index1 << "] and p[" << index2 << "]." << endl;
for (int i = 0; i < n; i++)
{
cout << "Distructor." << endl;
}
delete[]p;//记得delete
}
}
更多推荐




所有评论(0)