距离计算(友元函数)

距离计算(友元函数)

题目描述

Point类的基本形式如下:

请完成如下要求:

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

输入

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

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

输出

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

样例输入

2
1 0 2 1
2 3 2 4

样例输出

1
1

我的代码

#include <iostream>
#include <math.h>
using namespace std;
 
class Point{
private:
    double x, y;
public:
    Point(double xx, double yy);
    friend double Distance(Point &a, Point &b);
};
 
Point::Point(double xx, double yy) {
    x = xx;
    y = yy;
}
 
double Distance(Point &a, Point &b) {
    double s;
    s = sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
    return s;
}
 
int main(int argc, char** argv) {
    int t;
    cin >> t;
    while(t--) {
        double x, y;
        cin >> x >> y;
        Point a(x, y);
        cin >> x >> y;
        Point b(x, y);
         
        double s;
        s = Distance(a, b);
        cout << (int)s << endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/xindolia_ring/article/details/80280630