【id:56】【20分】A. 距离计算(友元函数)

文章描述了一个C++编程问题,要求实现一个Point类,包含x和y坐标,并添加一个友元函数`doubleDistance(Point&a,Point&b)`来计算两个点之间的欧几里得距离。在主函数中,程序会读取用户输入的点坐标,计算并输出多组点对间的距离,结果不进行四舍五入。

时间限制
1s
内存限制
128MB

题目描述
Point类的基本形式如下:

class Point
{
private:
double x, y;
public:
Point(double xx, double yy); // 构造函数
};
请完成如下要求:

1.实现Point类;
2.为Point类增加一个友元函数double Distance(Point &a, Point &b),用于计算两点之间的距离。直接访问Point对象的私有数据进行计算。
3.编写main函数,输入两点坐标值,计算两点之间的距离。

输入
第1行:输入需计算距离的点对的数目

第2行开始,每行依次输入两个点的x和y坐标

输出
每行依次输出一组点对之间的距离(结果直接取整数部分,不四舍五入 )

样例查看模式
正常显示
查看格式
输入样例1 <-复制

输出样例1

#include<bits/stdc++.h>
using namespace std;
class Point
{
private:
    double x, y;
public:
    friend double Distance(Point& a, Point& b);
    Point(double xx, double yy) {
        x = xx;
        y = yy;
    } // 构造函数
};
double Distance(Point& a, Point& b) {
    double dis;
    dis = sqrt((a.x-b.x)* (a.x - b.x)+(a.y-b.y)* (a.y - b.y));
    return dis;
}
int main() {
    int t;
    double x1, x2, y1, y2;
    cin >> t;
    while (t--) {
        cin >> x1 >> y1 >> x2 >> y2;
        Point p1(x1, y1);
        Point p2(x2, y2);
        cout <<fixed<<setprecision(0)<< Distance(p1, p2) << endl;
    }

    return 0;
}
单元测试中 void BusinessStateMachine::NotifyStart(BusinessState state) 347 : { 348 : static std::map<BusinessState, std::string> msgMap{ 349 0 : {RT_RM_CHECK_RETICLE, "Scan RM start"}, 350 0 : {RT_RM_TAKE_RETICLE, "Robot start take reticle from Reticle memory"}, 351 0 : {RT_RM_PUT_RETICLE, "Robot start put reticle to Reticle memory"}, 352 : // PRE 353 0 : {RT_PRE_TAKE_RETICLE, "Robot start take reticle from PreAlign"}, 354 0 : {RT_PRE_PUT_RETICLE, "Robot start put reticle to PreAlign"}, 355 0 : {RT_PRE_ALIGN_RETICLE, "Align Retcile start"}, 356 0 : {RT_PRE_CHECK_RETICLE, "Scan Reticle code start"}, 357 0 : {RT_PRE_CHECK_WAIT, "Wait user check Reticle id"}, 358 : // CS 359 0 : {RT_CS_TAKE_RETICLE, "Robot start take reticle from CS"}, 360 0 : {RT_CS_PUT_RETICLE, "Robot start put reticle to CS"}, 361 : // RS 362 0 : {RT_RS_COARSE_ALIGN, "Start search align"}, 363 0 : {RT_RS_COARSE_ALIGN_WAIT, "Search align waiting"}, 364 0 : {RT_RS_TAKE_RETICLE, "Robot start take reticle from RS"}, 365 0 : {RT_MOVE_RS_AND_ROBOT_TO_UNLOADPOS, "Robot start move RS and robot to unload position"}, 366 0 : {RT_MOVE_RS_AND_ROBOT_TO_LOADPOS, "Robot start move RS and robot to load position"}, 367 0 : {RT_MOVE_ROBOT_TO_LOADPOS, "Robot start move robot to load position"}, 368 0 : {RT_MOVE_ROBOT_TO_UNLOADPOS, "Robot start move robot to unload position"}, 369 0 : {RT_LOAD_RETICLE_TO_RS, "Robot start load reticle to RS"}, 370 0 : {RT_UNLOAD_RETICLE_FROM_RS, "Robot start unload reticle from RS"}, 371 : 372 0 : {RT_RS_PUT_RETICLE, "Robot start put reticle to RS"}, 373 0 : {RT_ROBOT_ROTATION_CONTROL, "Robot start move"}, 374 : // user process 375 0 : {RT_PAUSE, "RH start pause"}, 376 0 : {RT_STOP, "RH start stop"}, 377 0 : {RT_RESET, "RH start reset"}, 378 0 : {RT_CALIBRATE_SLOTS, "RH start calibrate Reticle Memory slot pos"}, 379 0 : {RT_EMO, "RH start EMO"}, 380 0 : {RT_TERMINAL, "RH start terminate"}, 381 112 : }; 382 58 : if (msgMap.find(state) == msgMap.end()) { 383 2 : return; 384 : } 385 56 : auto notify = NotifyMessage::NewProcessNotify(msgMap[state]); 386 56 : service_.lock()->Notify(std::move(notify)); 387 56 : } 388 : 389 78 : BNEStatus BusinessStateMachine::ExecPause() 390 : { 391 78 : if (bm_.lock()->HasUnexecutedTask()) { 392 39 : return entry_->Pause(); // 通知Entry暂停 393 : } 394 39 : NotifyService(NotifyMessageType::RTC_PAUSE_RESULT, BNE_RTC_PAUSE_NO_TASK); 395 39 : BNELogWarn("RTC BusinessManager Queue is empty, no need to exec Pause.\n"); 396 39 : setState_(RT_IDLE); 397 39 : return BNE_SUCCESS; 398 : }里的map如何覆盖
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谁的BUG最难改

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值