问题 D: Point_Array

问题 D: Point_Array
时间限制: 1 Sec 内存限制: 128 MB

题目描述

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

然后在主函数中根据用户输入的数目建立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<string>
#include<math.h>
#include<iomanip>
using namespace std;

class Point {
	double x, y;
public:
	Point()
	{
		cout << "Constructor." << endl;
		x = y = 0;
	}
	~Point()
	{
		cout << "Distructor." << endl;
	}
	Point(double x, double y)
	{
		this->x = x;
		this->y = y;
	}
	Point(const Point& p)
	{
		x = p.x;
		y = p.y;
	}
	double getX()
	{
		return x;
	}
	double gexY()
	{
		return y;
	}
	void setxy(double x, double y)
	{
		this->x = x;
		this->y = y;
	}
	void setx(double x)
	{
		this->x = x;
	}
	void sety(double y)
	{
		this->y = y;
	}
	double getdis(const Point& p)
	{
		return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2));
	}
};

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int num;
		cin >> num;
		Point* p = new Point[num];
		for (int i = 0; i < num; ++i)
		{
			int x, y;
			cin >> x >> y;
			p[i].setxy(x, y);
		}
		double maxdis = -1.0;
		int flag1, flag2;
		for (int i = 0; i < num - 1; ++i)
		{
			for (int j = i + 1; j <= num; ++j)
			{
				
				if (p[i].getdis(p[j]) > maxdis)
				{
					maxdis = p[i].getdis(p[j]);
					flag1 = i, flag2 = j;
				}
			}
		}
		cout << "The longeset distance is " << fixed << setprecision(2) << maxdis << ",between p[" << flag1 << "] and p[" << flag2 << "]." << endl;
		delete[]p;
	}
	return 0;
}
代码解释:void CopleyAmplifier::SetNewPVTMotionStartTime(boost::posix_time::ptime time,CouchTrjType pvt_point) { //Record the time stamp and data. m_bool_pvt_started = true; m_start_motion_time_us = PosixTime2Integer<unsigned long long>(time); m_last_pvt_data.p = m_start_pos; //Send the last dummy data calculated by the motion start time. ptime current_time = microsec_clock::universal_time(); ptime couch_time = Integer2PosixTime<unsigned long long>(pvt_point.t, current_time); ptime couch_to_L1_time = Integer2PosixTime<unsigned long long>(pvt_point.timeReachToBuffer, current_time); unsigned char next_point_time = round((pvt_point.t-m_start_motion_time_us)/1000.0)-m_total_motion_time_ms; if(next_point_time<4) { GcLogInfo(m_log_id, __FUNCTION__, "<CopleyStartPVT>Motion start time:%s. First couch time:%s.First couch to L1 time:%s.", boost::posix_time::to_simple_string(time).c_str(), boost::posix_time::to_simple_string(couch_time).c_str(), boost::posix_time::to_simple_string(couch_to_L1_time).c_str()); GcLogInfo(m_log_id, __FUNCTION__, "next_point_time: %d.",next_point_time); BOOST_THROW_EXCEPTION(AxisException() <<Axis_Error_Msg("Start PVT time failed! No enough time for First PVT data!")); } AmpPVTData dummy_data = {next_point_time,0,0}; //Send the left dummy data. dummy_data.time = next_point_time; Gantry::Array seg_cmd = ComposePVTRawData(dummy_data,m_next_pvt_index,1); GcLogDebugExpect(m_need_trace, m_log_id, __FUNCTION__, "<CopleyStartPVT>The %dth PVT dummy data.", m_next_pvt_index); WriteSDO(Gantry::ODAddress(COPLEY_PVT_DATA, 0), (unsigned long long)seg_cmd.GetValue<unsigned long long>()); GcLogInfo(m_log_id, __FUNCTION__, "<CopleyStartPVT>Motion start time:%s. First couch time:%s.First couch to L1 time:%s.", boost::posix_time::to_simple_string(time).c_str(), boost::posix_time::to_simple_string(couch_time).c_str(), boost::posix_time::to_simple_string(couch_to_L1_time).c_str()); m_total_motion_time_ms += dummy_data.time; m_lasttrj_segments.push_back(seg_cmd.GetValue<unsigned long long>()); ++m_next_pvt_index; GcLogInfo(m_log_id, __FUNCTION__, "<CopleyStartPVT>Motion Started. Start position %f mm.", pvt_point.p); }
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值