5-3 两点间距离计算

给出下面的一个基类框架:

class Point_1D

{ protected:

float x;//1D 点的x坐标

public:

Point_1D(float p = 0.0);

float distance(const Point_1D & p2);

}

以Point_1D为基类建立一个派生类Point_2D,增加一个保护数据成员:

float y;//2D平面上点的y坐标

以Point_2D为直接基类再建立一个派生类Point_3D,增加一个保护数据成员:

float z;//3D立体空间中点的z坐标

生成上述类并编写主函数,根据输入的点的基本信息,建立点对象,并能计算该点到原点的距离。

输入格式: 测试输入包含若干测试用例,每个测试用例占一行(点的类型(1表示1D点,2表示2D点,3表示3D点) 第一个点坐标信息(与点的类型相关) 第二个点坐标信息(与点的类型相关))。当读入0时输入结束,相应的结果不要输出。

输入样例:

1 -1 0

2 3 4 0 0

3 1 2 2 0 0 0

0

输出样例:

Distance from Point -1 to Point 0 is 1

Distance from Point(3,4) to Point(0,0) is 5

Distance from Point(3,3,3) to Point(0,0,0) is 3

参考代码

#include<iostream>
#include<math.h>
using namespace std;
//本题考察多层继承,将数据设为保护类,并使用公有继承
//建立一维类
class Point_1D
{
protected:
    float x;//1D 点的x坐标
public:
    void set_1D(){cin>>x;}
    float distance(const Point_1D & p2);
};
//建立二维类
class Point_2D:public Point_1D
{
protected:
    float y;//2D平面上点的y坐标
public:
    void set_2D(){set_1D();cin>>y;}//调用基类set方法
    float distance(const Point_2D & p2);
};
//建立三维类
class Point_3D:public Point_2D
{
protected:
    float z;//3D立体空间中点的z坐标
public:
    void set_3D(){set_2D();cin>>z;}
    float distance(const Point_3D & p2);
};
int main()
{
    int type;
    Point_1D a1,a2;
    Point_2D b1,b2;
    Point_3D c1,c2;
    cin>>type;
    while(type)
    {
      switch(type)
      {
        case 1:a1.set_1D();a2.set_1D();a1.distance(a2);break;
        case 2:b1.set_2D();b2.set_2D();b1.distance(b2);break;
        case 3:c1.set_3D();c2.set_3D();c1.distance(c2);break;
      }
      cin>>type;
    }
    return 0;
}
//实现distance方法 &重载
float Point_1D::distance(const Point_1D & p2)
{
      float a;
      a=fabs(x-p2.x);
      cout<<"Distance from Point "<<x<<" to Point "<<p2.x<<" is "<<a<<endl;
      return a;
}
float Point_2D::distance(const Point_2D & p2)
{
      float a;
      a=sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));
      cout<<"Distance from Point("<<x<<","<<y<<") to Point("<<p2.x<<","<<p2.y<<") is "<<a<<endl;
      return a;
}
float Point_3D::distance(const Point_3D & p2)
{
      float a;
      a=sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y)+(z-p2.z)*(z-p2.z));
      cout<<"Distance from Point("<<x<<","<<y<<","<<z<<") to Point("<<p2.x<<","<<p2.y<<","<<p2.z<<") is "<<a<<endl;
      return a;
}

欢迎指教,一起学习!

未经本人允许,请勿转载!

谢谢!

时间: 2024-08-27 10:17:00

5-3 两点间距离计算的相关文章

计算GPS两点间的距离[单位为:米]

/**     * 计算GPS两点间的距离[单位为:米]     * @param center GPS当前数据(LonLat对象表示,LonLat.lon表示经度,LonLat.lat表示纬度)     * @param turnPoint 转向点经纬度对象     * @return     */    private double gpsDistance( LonLat center, LonLat turnPoint )    {        double distance = 0; 

第十六周oj刷题——Problem D: B 友元类-计算两点间距离

Description 类Distance定义为类Point的友元类来实现计算两点之间距离的功能. Point类中有两个私有数据成员X和Y来表示点的两个坐标(横坐标和纵坐标), 成员函数需要自己定义. 主程序输入两个Point点的坐标,计算两个点之间的距离. Input 两个点的坐标(横坐标和纵坐标) Output 两个点的距离(保留了两位小数) Sample Input 1.0 1.0 2.0 2.0 Sample Output 1.41 /* All rights reserved. * 文

python,基于tkinter模块编写的根据经纬度计算两点间距离的应用程序

python的tkinter模块是用于编写GUI窗口程序的模块,使用起来非常方便,功能强大.基于tkinter模块,开发了一个输入两点经纬度计算输出距离(包括公里数和孤度数)的小程序,主要应用于地震台站地震报告编写.下面这段代码可以在python3.8上直接运行. import tkinter as tk import tkinter.messagebox from math import radians, cos, sin, asin, sqrt #定义由输入文本框获得台站及震源经纬度,计算距

HDU 2001 计算两点间距离

由四个实数组成的坐标,全部变量用double,刚开始用整数没过 #include<stdio.h> #include<math.h> int main() { double a,b,c,d; double r; while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF) { r=sqrt((a-c)*(a-c)+(b-d)*(b-d)); printf("%.2f\n",r);

iOS 地图 通过经纬度计算两点间距离

- (double)calculateStart:(CLLocationCoordinate2D)start end:(CLLocationCoordinate2D)end { double meter = 0; double startLongitude = start.longitude; double startLatitude = start.latitude; double endLongitude = end.longitude; double endLatitude = end.l

计算球面两点间距离实现Vincenty+Haversine

vincenty公式  精度很高能达到0.5毫米,但是很慢. Haversine公式半正矢公式,比vincenty快,精度没有vincenty高,也长使用. -------------------------------------------openlayers中实现的Vincenty---------------------------------------------------------- 角度转弧度 /*** Function: rad  * * Parameters:* x -

[MSSQL2008]Spatial Data in SQL Server 2008 - 根据经纬度计算两点间距离

DECLARE @BJ GEOGRAPHY DECLARE @XT GEOGRAPHY /*     GET Latitude/Longitude FROM here:http://www.travelmath.com/cities/Beijing,+China     the distance unit in SRID 4326 is the meter */ SELECT @BJ = geography::Point('39.92889', '116.38833', 4326) SELECT

mysql函数计算地表两点间距离

DELIMITER $$ CREATE FUNCTION `test`.`getDistance`(LatBegin FLOAT(10,4), LngBegin FLOAT(10,4), LatEnd FLOAT(10,4), LngEnd FLOAT(10,4))RETURNS FLOAT(10,4) BEGIN DECLARE Distance FLOAT(10,4) DEFAULT 0.0000; DECLARE EARTH_RADIUS FLOAT(10,4) DEFAULT 0.000

根据经纬度和半径计算经纬度范围,根据两点经纬度计算距离

这些经纬线是怎样定出来的呢?地球是在不停地绕地轴旋转(地轴是一根通过地球南北两极和地球中心的假想线),在地球中腰画一个与地轴垂直的大圆圈,使圈上的每一点都和南北两极的距离相等,这个圆圈就叫作“赤道”.在赤道的南北两边,画出许多和赤道平行的圆圈,就是“纬圈”:构成这些圆圈的线段,叫做纬线.我们把赤道定为纬度零度,向南向北各为90度,在赤道以南的叫南纬,在赤道以北的叫北纬.北极就是北纬90度,南极就是南纬90度.纬度的高低也标志着气候的冷热,如赤道和低纬度地地区无冬,两极和高纬度地区无夏,中纬度地区