西北农林科技大学OJ (C++)三角形类的设计与实现(组合类与组合对象)

Description

分别设计用于表示点和三角形的类:CPoint类和CTriangle类,要求如下:

(1)CPoint类中包含两个用于表示点的X和Y坐标分量的double型私有数据成员,能够根据指定的X和Y坐标构建点对象,且能够根据指定的另一点计算两点间的距离(要求参数为常引用)。

(2)CTriangle类中包含三个用于表示三角形顶点的私有对象成员,能够根据指定的三点构造三角形对象(要求构造函数的参数为常引用),且有计算三角形周长和面积的公有成员函数。

Input

输入的三个点的坐标构建三角形对象。

Output

输出精度为10(采用setprecision(10)设置输出精度)的三角形的周长和面积。

Sample Input 1 

0 0 3 0 0 4

Sample Output 1

12 6

**************************************************************************************************************

代码实现

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;
class CPoint 
{
    public:
        CPoint(double x1,double y1): x(x1),y(y1){}

        double Distance(CPoint& other)
        {
            return sqrt((x-other.x)*(x-other.x) + (y-other.y)*(y-other.y)); 
        }
    private:
        double x,y;
};

class CTriangle
{
    private:
        CPoint p1,p2,p3;
    public:
        CTriangle(CPoint P1,CPoint P2,CPoint P3):p1(P1),p2(P2),p3(P3){}
        double Circumference()
        {
            return p1.Distance(p2) + p2.Distance(p3) + p3.Distance(p1);
        }

        double Area()
        {
            double p;
            p = Circumference() / 2;
            return sqrt(p * (p-p1.Distance(p2))*(p-p2.Distance(p3))*(p-p3.Distance(p1)));
        }
};

int main()
{
    double a,b,c,d,e,f;

    cin >> a >> b >> c >> d >> e >> f;
    CPoint p1(a,b),p2(c,d),p3(e,f);
    CTriangle(p1,p2,p3);

    cout << setprecision(10);
    cout << CTriangle(p1,p2,p3).Circumference() << " " <<CTriangle(p1,p2,p3).Area()<<endl;

    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值