三角形类有关实验问题c++

三角形类

【问题描述】先定义一个能描述平面上一条线段的类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()函数,将三角形三条边的端点坐标及面积。面积保留两位小数。具体格式见样例。

海伦公式 (p为周长一半)

{\color{Green} s=\sqrt{p*(p-a)*(p-b)*(p-c)}}

【样例输入】

  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

【提示】计算面积建议用海伦公式。严格按照【样例输出】的格式输出结果。如果没有使用类,得分为0。

代码

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Beeline
{
	private:
		int x1,y1,x2,y2;
	public:
		Beeline(int xx1=0,int yy1=0,int xx2=0,int yy2=0);
		float Length();
		void show();
}; 
Beeline::Beeline(int xx1,int yy1,int xx2,int yy2)
{
    x1=xx1;
	y1=yy1;
	x2=xx2;
	y2=yy2;	
} 
float Beeline::Length() 
{
	return sqrt(pow((x1-x2),2)+pow((y1-y2),2));
}
void Beeline::show()
{
	cout<<'('<<x1<<','<<y1<<"),"<<'('<<x2<<','<<y2<<')'<<endl;
}
class Triangle
{
	private:
	   Beeline l1,l2,l3;
	public:
		Triangle(int x1,int y1,int x2,int y2,int x3,int y3):l1(x1,y1,x2,y2),l2(x2,y2,x3,y3),l3(x3,y3,x1,y1) {}
        float Area();
        void Print();
}; 
float Triangle::Area() 
{
	float p=(l1.Length()+l2.Length()+l3.Length())/2;
	float s;
	s=sqrt(p*(p-l1.Length())*(p-l2.Length())*(p-l3.Length()));
	return s;
}
void Triangle::Print() 
{
	cout<<"Three edges' points are listed as follows:"<<endl;
	l1.show();  l2.show();  l3.show();
	cout<<"The area of this triangle is:"<<fixed<<setprecision(2)<<Area();   
}
int main()
{
	int  x1,x2,x3,y1,y2,y3;
	cin>>x1>>y1;
	cin>>x2>>y2;
	cin>>x3>>y3;
	Triangle tri(x1,y1,x2,y2,x3,y3);
	tri.Print(); 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

将爱却晚秋-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值