西北农林科技大学2024学年C++面向对象程序设计OJ——T9 三角形类的设计与实现

一.题目描述

三角形类的设计与实现(组合类与组合对象)

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<bits/stdc++.h>
using namespace std;

class CPoint{
private:
    double x;
    double y;
public:
    CPoint(double xx, double yy) : x(xx), y(yy) {}
    double distance(const CPoint& other) const {
        return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
    }

};
class CTriangle{
private:
    CPoint a;
    CPoint b;
    CPoint c;
public:
    CTriangle(const CPoint& a1, const CPoint& b1, const CPoint& c1) : a(a1), b(b1), c(c1) {}
    double perimeter()const{
        double a_a = a.distance(b);
        double b_b = b.distance(c);
        double c_c = c.distance(a);
        return a_a + b_b + c_c;    
    }
    double area()const{
        double a_a = a.distance(b);
        double b_b = b.distance(c);
        double c_c = c.distance(a);
        double p = (a_a + b_b + c_c)/2;
        return sqrt(p * (p - a_a) * (p - b_b) * (p - c_c));
    }
};
int main ()
{
    double x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

    CPoint point1(x1, y1);
    CPoint point2(x2, y2);
    CPoint point3(x3, y3);

    CTriangle triangle(point1, point2, point3);

    cout << setprecision(10) << triangle.perimeter() << " " << triangle.area() << endl;

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失忆已成习惯.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值