天天看点

point_triangle_类的组合

描述

设计一个Point类,表示平面中的一个点

设计一个Triangle类(三角形类),内含三点。

要求:

设计类的相应的构造函数、复制构造函数,完成初始化和对象复制

设计Triangle类的成员函数,分别完成三点边能否构成三角形的检查,三角形周长的计算

输入

三个点

输出

三角形周长(保留小数点后三位数。如果不是三角形,输出 no)

样例输入

0 0

0 1

1 0

样例输出

3.414

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Point
{
private:
    double x,y;
public:
    Point(double xx=0,int yy=0)
    {
        x=xx;
        y=yy;
    }
    friend class Triangle;
};
class Triangle
{
private:
    Point A,B;
    double len;
public:
    void length(Point AA,Point BB)
    {
        len=sqrt(  (AA.x-BB.x)*(AA.x-BB.x)+(AA.y-BB.y)*(AA.y-BB.y)  );
    }
    double judge(Triangle l2,Triangle l3)
    {

        if(len+l2.len>l3.len&&l2.len+l3.len>len&&len+l3.len>l2.len)
            cout<<fixed<<setprecision(3)<<(len+l2.len+l3.len)<<endl;
        else cout<<"no"<<endl;
        }
};
int main()
{
    int x1,y1,x2,y2,x3,y3;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    Point p1(x1,y1);
    Point p2(x2,y2);
    Point p3(x3,y3);
    Triangle l1,l2,l3;
    l1.length(p1,p2);
    l2.length(p2,p3);
    l3.length(p1,p3);
    l1.judge(l2,l3);
}