描述
設計一個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);
}