C++ 之点类point 输入10个点,求出10个点的距离

假设有一个点类point,具有两个实数坐标。希望主程序使用这个类完成下述功能:
(l)主程序为类point定义10个点的对象数组(也可以动态申请空间)。
(2)要求调用一个函数Set()从键盘输入10个对象的属性。
(3)要求调用一个函数Display()显示10个对象的值。
(4)要求调用一个函数Lenth(),计算将这些点连成一条折线时,这条折线的长度。
参考的输入(每个点要求提示“Input x,y:”):
Input x,y:0 0
Input x,y:3 5
Input x,y:2 4
Input x,y:9 8
Input x,y:2 3
Input x,y:8 9
Input x,y:7 6
Input x,y:2 9
Input x,y:5 8
Input x,y:6 6
参考的输出:
(0,0)
(3,5)
(2,4)
(9,8)
(2,3)
(8,9)
(7,6)
(2,9)
(5,8)
(6,6)
Length:46.7866

本程序是由 QTCreator在linux操作系统下完成的,仅供参考

Point类的头文件

#ifndef CPOINT_H
#define CPOINT_H
class CPoint
{
private:
    int row;
    int col=0;
    int array[15][2];
    float DisAB;
    int sum = 0;
public:
    void CSetPoint();
    void Display();
    void Lenth();
    int PowerX_X(int i);
    int PowerY_Y(int i);
    int Twodist();
};
#endif // CPOINT_H

Point类的实现方法

#include "cpoint.h"
#include <math.h>
#include <iostream>
using namespace std;
void CPoint::CSetPoint()
{
    //cause this is a point,so the col is 2,and i made the default value is 0
    cout <<endl <<"Please input the POINT number : ";
    cin >> row;
    for(int i=0; i<row;i++)
    {
        cout<<endl<<"Please input " <<i+1<<" point(x1 y1) :( ";
        cin >> array[i][col]
            >>array[i][col+1];  //Store the two values of input
    }
}
void CPoint::Display()  //display the point that your input
{
    for(int i=0; i<row;i++)
    {
        cout <<endl
             <<" point "<< i+1 <<": ( "
             << array[i][col] <<" , "
             << array[i][col+1] <<" ) "
             <<endl;
    }
    cout<<endl ;
}
int CPoint::PowerX_X(int i) //compute (X1-X2)*(X1-X2)
{
   return (array[i][col] - array[i+1][col]) * (array[i][col] - array[i+1][col]);
}
int CPoint::PowerY_Y(int i)     //compute (Y1-Y2)*(Y1-Y2)
{
   return (array[i][col+1] - array[i+1][col+1]) * (array[i][col+1] - array[i+1][col+1]);
}
int CPoint::Twodist()   //compute the distance of two point
{
    sum=0;      //this sum is for clear last result when you input 'y'
    for(int point = 0; point < row-1; point++) //must be carful.three point ,just compute twice
    {
        int XX=PowerX_X(point);
        int YY=PowerY_Y(point);
        sum += fabs ( DisAB = sqrt ( XX + YY ) );   //compute sum of each point
    }
    return sum;
}
void CPoint::Lenth()    //obtain the return value and display
{
    int lenth ;
    lenth = Twodist();
    cout << "The "<<row<<" Point's distance is : "<< lenth <<endl;
}

Point实现的Main函数

#include <iostream>
#include "cpoint.h"
#include <iostream>
using namespace std;
int main()
{
    CPoint point;
    char flag ='y';
    while(flag == 'y')  // maybe you want once again,so set a flag
    {
        point.CSetPoint();
        point.Display();
        point.Lenth();
        cout<< endl <<"Do you once again ? (y or n): ";
        cin >>flag;
    }
    return 0;
}

运行结果:
为了看出效果,我先设置的是3个点,且点都是横坐标相同,其次设置3个纵坐标相同的点,最后实现10点,
在这里插入图片描述

在这里插入图片描述

2018年10月18日22:24:14

  • 11
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值