C++程序问题
先定义一个能描述平面上一条直线的类Beeline,其私有数据成员为直线两个端点的坐标(X1,Y1,X2,Y2)。在类中定义形参缺省值为0的构造函数,及计算直线长度的公有成...
先定义一个能描述平面上一条直线的类Beeline,其私有数据成员为直线两个端点的坐标(X1,Y1,X2,Y2)。在类中定义形参缺省值为0的构造函数,及计算直线长度的公有成员函数Length(),显示直线两个端点坐标的公有成员函数Show()。然后再定义一个能描述平面上三角形的类Triangle,其数据成员为用Beeline定义的对象line1、line2、line3与三角形三条边长l1、l2、l3。在类中定义的构造函数要能对对象成员与边长进行初始化。再定义计算三角形面积的函数Area(),及显示三条边端点坐标及面积的函数Print(),Print()函数中可调用Show()函数显示三条边两端点坐标。谁来帮忙编一下
展开
2个回答
2013-07-03
展开全部
都按你要求写好了,以下是测试图:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
/*---------------Beeline类-------------*/
class Beeline
{
private:
double x1,x2,y1,y2;
public:
Beeline();
Beeline(double,double,double,double);
double Length();
void Show();
};
Beeline::Beeline()
{
x1 = 0; y1 = 0;
x2 = 0; y2 = 0;
}
Beeline::Beeline(double _x1,double _y1,double _x2,double _y2)
{
x1 = _x1; y1 = _y1;
x2 = _x2; y2 = _y2;
}
double Beeline::Length()
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void Beeline::Show()
{
cout << "(" << x1 << "," << y1 << ") "
<< "(" << x2 << "," << y2 << ")"
<< endl;
}
/*---------------Triangle类-------------*/
class Triangle
{
private:
Beeline *line1, *line2, *line3;
double l1, l2, l3;
public:
Triangle();
Triangle(Beeline*,Beeline*,Beeline*);
double Area();
void Print();
};
Triangle::Triangle()
{
line1 = new Beeline(.0,.0,.0,.0);
line2 = new Beeline(.0,.0,.0,.0);
line3 = new Beeline(.0,.0,.0,.0);
l1 = 0.0; l2 = 0.0; l3 = 0.0;
}
Triangle::Triangle(Beeline* _line1,Beeline* _line2,Beeline* _line3)
{
line1 = _line1;
line2 = _line2;
line3 = _line3;
l1 = _line1->Length();
l2 = _line2->Length();
l3 = _line3->Length();
}
double Triangle::Area()
{
double p;
// 海伦公式
p = (l1+l2+l3)/2.0;
return sqrt(p*(p-l1)*(p-l2)*(p-l3));
}
void Triangle::Print()
{
line1->Show();
line2->Show();
line3->Show();
cout << "该三角形面积为: " ;
cout << fixed << setprecision(2) << Area() << endl;
}
/*------------测试-------------*/
int main()
{
Beeline *line_a = new Beeline(1.0,1.0,2.0,3.0);
Beeline *line_b = new Beeline(1.0,1.0,3.0,1.0);
Beeline *line_c = new Beeline(2.0,3.0,3.0,1.0);
cout << "line_a: ";
line_a->Show();
Triangle triangle(line_a,line_b,line_c);
triangle.Print();
system("pause");
return 0;
}
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
/*---------------Beeline类-------------*/
class Beeline
{
private:
double x1,x2,y1,y2;
public:
Beeline();
Beeline(double,double,double,double);
double Length();
void Show();
};
Beeline::Beeline()
{
x1 = 0; y1 = 0;
x2 = 0; y2 = 0;
}
Beeline::Beeline(double _x1,double _y1,double _x2,double _y2)
{
x1 = _x1; y1 = _y1;
x2 = _x2; y2 = _y2;
}
double Beeline::Length()
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void Beeline::Show()
{
cout << "(" << x1 << "," << y1 << ") "
<< "(" << x2 << "," << y2 << ")"
<< endl;
}
/*---------------Triangle类-------------*/
class Triangle
{
private:
Beeline *line1, *line2, *line3;
double l1, l2, l3;
public:
Triangle();
Triangle(Beeline*,Beeline*,Beeline*);
double Area();
void Print();
};
Triangle::Triangle()
{
line1 = new Beeline(.0,.0,.0,.0);
line2 = new Beeline(.0,.0,.0,.0);
line3 = new Beeline(.0,.0,.0,.0);
l1 = 0.0; l2 = 0.0; l3 = 0.0;
}
Triangle::Triangle(Beeline* _line1,Beeline* _line2,Beeline* _line3)
{
line1 = _line1;
line2 = _line2;
line3 = _line3;
l1 = _line1->Length();
l2 = _line2->Length();
l3 = _line3->Length();
}
double Triangle::Area()
{
double p;
// 海伦公式
p = (l1+l2+l3)/2.0;
return sqrt(p*(p-l1)*(p-l2)*(p-l3));
}
void Triangle::Print()
{
line1->Show();
line2->Show();
line3->Show();
cout << "该三角形面积为: " ;
cout << fixed << setprecision(2) << Area() << endl;
}
/*------------测试-------------*/
int main()
{
Beeline *line_a = new Beeline(1.0,1.0,2.0,3.0);
Beeline *line_b = new Beeline(1.0,1.0,3.0,1.0);
Beeline *line_c = new Beeline(2.0,3.0,3.0,1.0);
cout << "line_a: ";
line_a->Show();
Triangle triangle(line_a,line_b,line_c);
triangle.Print();
system("pause");
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-03
展开全部
百度下的蛮
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询