【id:34】【20分】D. Point_Array(类+构造+对象数组)

博客要求在原C++代码基础上进行修改,包括增加自写析构函数、修改getDisTo方法参数、修改构造函数。还需在主函数根据用户输入建立Point数组,求出数组内距离最大的两个点的距离值,并给出了C++输出指定精度的参考代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值