【id:31】【20分】A. Point(类与构造)

题目描述

下面是一个平面上的点的类定义,请在类外实现它的所有方法,并生成点测试它。

输入

测试数据的组数 t

第一组测试数据点p1的x坐标 第一组测试数据点p1的y坐标 第一组测试数据点p2的x坐标 第一组测试数据点p2的y坐标

..........

输出

输出p1到p2的距离

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

#include <iostream>

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

using namespace std;

void main( )

{ double a =3.14;

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

}

样例查看模式

正常显示查看格式

输入样例1 <-复制

输出样例1

#include<iostream>
#include <iomanip> //必须包含这个头文件
using namespace std; 
class point
{
private:
    double x, y;
public:
    point();
    point(double x_value, double y_value);
    double getX();
    double getY();
    void setX(double x_value);
    void setY(double y_vlaue);
    double distanceTOAnotherPoint(point p);
};
point::point()
{
    x = 0;
    y = 0;
}//初始化
point::point(double x_value, double y_value)
{
    x = x_value;
    y = y_value;
}
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::distanceTOAnotherPoint(point p)
{
    double distance = 0;
    distance = sqrt(pow(x - p.getX(), 2) + pow(y - p.getY(), 2));
    return distance;
}
int main()
{
    int t;
    cin >> t;
    double x1, y1;
    double x2, y2;
    while (t--)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        point p1(x1, y1);
        point p2(x2, y2);
        cout << fixed << setprecision(2) << "Distance of Point(" << x1 << "," << y1 << ") to Point(" << x2 << "," << y2 << ") is " << p1.distanceTOAnotherPoint(p2) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值