要求:声明点类Point,三角形类Triangle。用三个点作为Triangle类的数据成员求三角形面积。
#include<iostream>
#include<cmath>
using namespace std;
class point
{
int x,y;
public:
point(int xx=0,int yy=0)
{
x=xx;
y=yy;
}
point(point &p)
{
x=p.x;
y=p.y;
}
int get_x()
{
return x;
}
int get_y()
{
return y;
}
};
class triangle
{
point a,b,c;
public:
triangle(point a1,point b1,point c1):a(a1),b(b1),c(c1)
{
;
}
double area()
{
double r,r1,r2,r3,s;
r1=sqrt(pow((a.get_x()-b.get_x()),2)+pow((a.get_y()-b.get_y()),2));
r2=sqrt(pow((a.get_x()-c.get_x()),2)+pow((a.get_y()-c.get_y()),2));
r3=sqrt(pow((c.get_x()-b.get_x()),2)+pow((c.get_y()-b.get_y()),2));
r=(r1+r2+r3)/2;
s=sqrt(r*(r-r1)*(r-r2)*(r-r3));
return s;
}
};
int main()
{
point a(1,5), b(2,3), c(3,4);
triangle t(a,b,c);
cout<<"该三角形的面积为:"<<t.area()<<endl;
return 0;
}