zoukankan      html  css  js  c++  java
  • 用c++定义两个坐标点,计算两点间距离;进而计算线段的面积


    //本程序有三个层次

    //第一层(define_class.h):构造坐标点类,颜色和宽度的结构体,线段类

    //第二层(function.h):对上一层声明的函数进行定义

    //第三层(distance.cpp):用类定义对象并初始化对象,对结果进行测试

    define_class.h

    #if!defined(define_class_H)
    #define define_class_h
    #include <iostream>
    #include <cmath>
    using namespace std;
    //定义坐标点类
    class Point
    {
    private:
      double X,Y; //横坐标、纵坐标作为私有变量
    public:
      Point(double=0,double=0); //构造函数

      Point(Point &); //复制的构造函数

      void Display() //显示坐标点
      {
        cout<<X<<","<<Y<<endl;
      }

      double Distance(Point &); //两点间距离的函数,参数是点类的引用,也可以用友元函数

      int getX()
      {
        return X; //得到横坐标的值
      }

      int getY()
      {
        return Y; //得到纵坐标的值
      }
    };

    struct Cow //color和width,结构体,结构体内的变量是public的
    {
      int Color;
      int Width;
    };

    class Line //定义线段类
    {
      private:
      Point a,b; //线段类的私有数据成员是点类的对象
      Cow cw; //线段有颜色和宽度
      public:
      Line(Point &,Point &,Cow &); //线段的构造函数,由两个点、颜色和宽度构成
      void Display();
      Line(Line &); //复制的构造函数
      double Distance(); //两点间的距离
      double Area(); //线段的面积
    };

    #endif

    function.h

    #if!defined(function_H)
    #define function_H
    #include "define_class.h" //包含头函数
    Point::Point(double a,double b) //定义构造函数,前面的头函数中仅仅声明了函数
    {
      X=a;
      Y=b;
    }
    Point::Point(Point &a) //定义复制的构造函数
    {
      X=a.X;
      Y=a.Y;
    }
    double Point::Distance(Point &a) //求两点间的距离
    {
      double dis;
      dis=sqrt((X-a.X)*(X-a.X)+(Y-a.Y)*(Y-a.Y));
      return dis;
    }
    Line::Line(Point &a1,Point &a2,Cow &a3):a(a1),b(a2),cw(a3) //给Line的私有变量初始化
    { //对象间的初始化,因此需要复制的构造函数
    }
    Line::Line(Line &s) //定义复制的构造函数
    {
      a=s.a;
      b=s.b;
      cw=s.cw;
    }
    void Line::Display() //显示线段
    {
      a.Display();
      b.Display();
      cout<<"Color="<<cw.Color<<","<<"width="<<cw.Width<<endl;
    }

    Line::Line(Line &m)    //定义Line的复制构造函数

    {

          a=m.a;

          b=m.b;

    }


    double Line::Distance()
    {
      double x,y;
      x=a.getX()-b.getX();
      y=a.getY()-b.getY();
      return sqrt(x*x+y*y);
    }
    double Line::Area()
    {
      return cw.Width * Distance();
    }
    #endif

    distance.cpp

    #include <iostream>
    #include <cmath>
    #include "function.h"
    using namespace std;

    void main()
    {
      Point a;
      Point b(8.9,9.8),c(34.5,67.8);
      a=c;
      a.Display();
      b.Display();

      cout<<"两点之间的距离:"<<a.Distance(b)<<endl;

      Cow cw={3,5};

      Line s(a,b,cw);

      Line s1(s);

      s1.Display();

      cout<<"线段的长度:"<<s1.Distance()<<endl;
      cout<<"线段的面积:"<<s1.Area()<<endl;
    }

  • 相关阅读:
    浅谈Python常用英文单词
    python web框架 Django的APP以及目录介绍 2
    Python中的enumerate函数
    python web框架 django wsgi 理论
    python web框架 django 工程 创建 目录介绍
    python web框架 django工程的创建
    mysql c 终止 mysql输入语句模式
    前端 html input标签 placeholder属性 标签上显示内容
    img 标签注意 默认img标签,有一个1px的边框 img{ border: 0; }
    前端 html input标签 disable 属性
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11096455.html
Copyright © 2011-2022 走看看