Point_Array(类+构造+对象数组)

Point_Array(类+构造+对象数组)

题目描述

在这里插入图片描述
上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改:1、增加自写的析构函数;2、将getDisTo方法的参数修改为getDisTo(const Point &p);3、根据下面输出的内容修改相应的构造函数。

然后在主函数中根据用户输入的数目建立Point数组,求出数组内距离最大的两个点之间的距离值。

输入

测试数据的组数 t

第一组点的个数

第一个点的 x 坐标 y坐标

第二个点的 x坐标 y坐标

输出

输出第一组距离最大的两个点以及其距离

在C++中,输出指定精度的参考代码如下:

#include

#include //必须包含这个头文件

using namespace std;

void main( )

{ double a =3.141596;

cout<<fixed<<setprecision(3)<<a<<endl; //输出小数点后3位

示例输入

2
4
0 0
5 0
5 5
2 10
3
-1 -8
0 9
5 0

示例输出

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.

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class Point 
{
	double x,y;
	public:
		Point();
		~Point()
		{
				cout<<"Distructor."<<endl;
		}
		Point(double xvalue,double yvalue);
		double getX();
		double getY();
		void setXY(double x1,double y1)
		{
			x=x1;
			y=y1;
		}
		void setX(double xvalue);
		void setY(double yvalue);
		double getDisTo(Point &p);	
};	

Point::Point()
{
	x=0;
	y=0;
	cout<<"Constructor."<<endl;
}

Point::Point(double xvalue,double yvalue)
{
	x=xvalue;
	y=yvalue;
}

double Point::getX(){
	return x;
}

double Point::getY(){
	return y;
}

void Point::setX(double xvalue)
{
	x=xvalue;
}

void Point::setY(double yvalue)
{
	y=yvalue;
}

double Point::getDisTo(Point &p)
{
	double a,b,c,s;
	a=(x-p.getX())*(x-p.getX());
	b=(y-p.getY())*(y-p.getY());
	c=a+b;
	s=sqrt(c);
	return s;
}

class ArrayofPoint
{
	public:
		ArrayofPoint(int n)
		{
			num=n;
			Points=new Point[n];
		}
		~ArrayofPoint()
		{
		
			num=0;
			delete []Points;
		}
		Point &Element(int n)
		{
			return Points[n];
		}
	private:
		Point *Points;
		int num;	
};

int main()
{
	int t,i,x1,y1,j,a,b,m,n;
	double max;
	cin>>m;
	for(n=0;n<m;n++)
	{	
		cin>>t;
		double *p=new double[t];
		ArrayofPoint Points(t);
		for(i=0;i<t;i++)
		{
			cin>>x1>>y1;
			Points.Element(i).setXY(x1,y1);
		}
		max=Points.Element(0).getDisTo(Points.Element(1));
		a=0;b=1;
		for(i=0;i<t;i++)
		{
			for(j=i+1;j<t;j++)
			{
				if(Points.Element(i).getDisTo(Points.Element(j))>max)
				{
					max=Points.Element(i).getDisTo(Points.Element(j));
					a=i;
					b=j;
				}	
			}
		}
		cout<<"The longeset distance is "<<fixed<<setprecision(2)<<max<<","<<"between p["<<a<<"] and p["<<b<<"]."<<endl;
		delete []p;
	}
	return 0;
}
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值