对于点.线.三角形类的操作(2020.12.26)

题目内容:

先定义一个能描述平面上一条线段的类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;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值