题目内容:
先定义一个能描述平面上一条线段的类Beeline,包含私有数据成员为线段两个端点的坐标(X1,Y1,X2,Y2),在类中定义形参默认值为0的构造函数,计算线段长度的公有成员函数Length(),显示线段两个端点坐标的公有成员函数show()。然后再定义一个能描述平面上三角形的类Triangle,其数据成员为用Beeline定义的对象line1,line2,line3。在类中定义的构造函数要能对对象成员进行初始化。再定义计算三角形面积的函数Area()及显示三条边端点坐标及面积的函数Print(),Print()函数中可调用show()函数显示三条边两端点坐标。
输入格式:
输入三角形三个顶点的坐标(x1,y1)、(x2,y2)、(x3,y3)。
其中 -100 <= x1,x2,x3,y1,y2,y3 <= 100,且为整数。
在主函数中创建类对象tri(x1,y1,x2,y2,x3,y3),对应line1(x1, y1, x2, y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1)。
输出格式:
调用Print()函数,将三角形三条边的端点坐标及面积。面积保留两位小数。
输入样例:
0 0
0 4
3 0
输出样例:
Three edges' points are listed as follows:
(0, 0),(0, 4)
(0, 4),(3, 0)
(3, 0),(0, 0)
The area of this triangle is: 6.00.
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Beeline {
private:
int x1, y1, x2, y2;
public:
Beeline()
{
x1 = 0; y1 = 0; x2 = 0; y2 = 0;
}
void set(int x, int y, int c, int v)
{
x1 = x; y1 = y; x2 = c; y2 = v;
}
double Length()
{
return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
}
void show()
{
cout << "(" << x1 << ", " << y1 << ")" << "," << "(" << x2 << ", " << y2 << ")";
cout << endl;
}
};
class Triangle {
private:
Beeline line1;//以其他类作为成员数据
Beeline line2;
Beeline line3;
double area;
public:
Triangle(int a, int b, int c, int d, int e, int f)
{
line1.set(a, b, c, d);
line2.set(c, d, e, f);
line3.set(e, f, a, b);
}
void Area()
{
double p, a, b, c;
a = line1.Length();
b = line2.Length();
c = line3.Length();
p = (a + b + c) / 2;
area = sqrt(p*fabs(p - a) *fabs(p - b)*fabs(p - c));//计算三角形的面积公式
}
void print()
{
cout << "Three edges' points are listed as follows:" << endl;
line1.show();
line2.show();
line3.show();
cout << "The area of this triangle is: " << setiosflags(ios::fixed) << setprecision(2) << area << ".";
}
};
int main()
{
int a, b, c, d, e, f;
cin >> a >> b >> c >> d>>e>>f;
Triangle tri(a, b, c, d, e, f);
tri.Area();
tri.print();
return 0;
}