A. Point(类与构造)
题目描述
下面是一个平面上的点的类定义,请在类外实现它的所有方法,并生成点测试它。
输入
测试数据的组数 t
第一组测试数据点p1的x坐标 第一组测试数据点p1的y坐标 第一组测试数据点p2的x坐标 第一组测试数据点p2的y坐标
..........
输出
输出p1到p2的距离
输入样例1
2 1 2 3 4 -1 0.5 -2 5
输出样例1
Distance of Point(1.00,2.00) to Point(3.00,4.00) is 2.83 Distance of Point(-1.00,0.50) to Point(-2.00,5.00) is 4.61
该题比较简单,主要考察类与构造函数的相关知识,主要注意格式输出
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
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_value);
double distanceToAnotherPoint(Point p);
};
Point::Point()
{
x = 0;
y = 0;
}
void Point::setX(double x_value)
{
x = x_value;
}
void Point::setY(double y_value)
{
y = y_value;
}
double Point::getX()
{
return x;
}
double Point::getY()
{
return y;
}
Point::Point(double x_value, double y_value)
{
x = x_value;
y = y_value;
}
double Point::distanceToAnotherPoint(Point p)
{
return sqrt((p.x - this->x) * (p.x - this->x) + (p.y - this->y) * (p.y - this->y));
}
int main()
{
int t;
double x_v1, y_v1, x_v2, y_v2;
cin >> t;
Point p1,p2;
while (t--)
{
cin >>x_v1 >> y_v1 >> x_v2 >> y_v2;
Point p1(x_v1, y_v1);
Point p2(x_v2, y_v2);
cout << "Distance of Point(";
cout << fixed << setprecision(2) << x_v1 << "," << y_v1 << ") to Point(" << x_v2 <<"," << y_v2 << ") is " << p1.distanceToAnotherPoint(p2) << endl;
}
return 0;
}