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

该代码展示了如何在C++中定义一个Point类,用于存储二维坐标,并实现构造函数、析构函数以及计算两点之间的距离。程序通过main函数输入点集,找出最长距离及其对应的点索引。

aa

#include<iostream>
#include <iomanip>
#include<math.h>
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, i1, i2, i, j;
	double max = 0, x, y;
	cin >> t;

	while (t--)
	{
		int ts = 0;
		cin >> ts;
		point* p = new point[ts];
		for (i = 0; i < ts; i++)
		{
			cin >> x;
			cin >> y;
			p[i].setXY(x, y);
		}
		for (i = 0; i < ts; i++)
		{
			for (j = i + 1; j < ts; j++)
			{
				if (max < p[i].getDisTo(p[j]))
				{
					max = p[i].getDisTo(p[j]);
					i1 = i;
					i2 = j;
				}
			}
		}
		cout << "The longeset distance is " << fixed << setprecision(2) << max 
			<< ",between p[" << i1 << "] and p[" << i2 << "]." << endl;
		delete[] p;
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值