64,683
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<string>
#include "math.h"
using namespace std;
//设计并定义一个Triange类来描述三角形,使用Point类的对象来表示端点,Point类的定义如下:
class Point
{
public:
Point(long double nx=0.0,long double ny=0.0):x(nx),y(ny){}
Point(Point &np):x(np.x),y(np.y){}
long double GetX(){return x;}
long double GetY(){return y;}
void SetX(long double nx){x=nx;}
void SetY(long double ny){y=ny;}
void SetPoint(long double nx,long double ny){x=nx;y=ny;}
void SetPoint(Point &np){x=np.x;y=np.y;}
private:
long double x,y ;
} ;
class Triangle
{
Point p1,p2,p3;
long double a,b,c;
public:
Triangle(long double x1,long double y1,long double x2,long double y2,long double x3,long double y3):p1(x1,y1),p2(x2,y2),p3(x3,y3)
{
a=GetLength(p1,p2);
b=GetLength(p1,p3);
c=GetLength(p2,p3);
}
long double GetLength(Point p,Point q)
{
return sqrt(pow(p.GetX()-q.GetX(),2)+pow(p.GetY()-q.GetY(),2));
}
long double GetPerimeter()
{
return a+b+c;
}
long double GetArea()
{
long double p=GetPerimeter()/2;
return sqrt( p*(p-a)*(p-b)*(p-c));
}
};
int main()
{
Triangle t1(0,0,0,3,4,0);
cout<<t1.GetPerimeter()<<endl;
cout<<t1.GetArea()<<endl;
system("pause");
return 0;
}