C++ 编程,三角形类
答案:1 悬赏:40 手机版
解决时间 2021-03-27 19:22
- 提问者网友:欲劫无渡
- 2021-03-27 02:12
C++ 编程,三角形类
最佳答案
- 五星知识达人网友:迟山
- 2021-03-27 03:41
#include "iostream.h"
#include "math.h"
class CPoint//定义点
{
private:
double x,y;
public:
CPoint(double a,double b){x=a;y=b;}//构造函数
CPoint(){x=0;y=0;}
void Set(double a,double b){x=a,y=b;}//设置点坐标
double GetX(){return x;}
double GetY(){return y;}
};
class CTreiangle//三角形类
{
private:
CPoint x,y,z;
double s1,s2,s3;
public:
void SetTriangle(CPoint P1,CPoint p2,CPoint P3);
bool Test();
double Area();
};
void CTreiangle::SetTriangle(CPoint P1,CPoint p2,CPoint p3)//设置三角形的三个点并计算三条边的边长
{
x=P1;
y=p2;
z=p3;
s1=sqrt((x.GetX()-y.GetX())*(x.GetX()-y.GetX())+(x.GetY()-y.GetY())*(x.GetY()-y.GetY()));
s2=sqrt((x.GetX()-z.GetX())*(x.GetX()-z.GetX())+(x.GetY()-z.GetY())*(x.GetY()-z.GetY()));
s3=sqrt((z.GetX()-y.GetX())*(z.GetX()-y.GetX())+(z.GetY()-y.GetY())*(z.GetY()-y.GetY()));
}
bool CTreiangle::Test()//检测是不是三角形,原理三点不共线
{
if((y.GetX()-x.GetX())*z.GetY()==(y.GetY()-x.GetY())*z.GetX()+x.GetY()*y.GetY()-x.GetY()*x.GetY()-x.GetX()*y.GetY()+y.GetX()*x.GetY())
return false;
else
return true;
}
double CTreiangle::Area()//应用海伦公式计算面积并返回
{
double p,area;
p=(s1+s2+s3)/2.0;
area=sqrt(p*(p-s1)*(p-s2)*(p-s3));
return area;
}
int main()
{
CPoint temp[3];//定义三个点
double tempx,tempy;
CTreiangle treiangle;
while(1)
{
for(int i=0;i<3;i++)
{
cout<<"请输入第"< cin>>tempx;
cout<<"请输入第"< cin>>tempy;
temp[i].Set(tempx,tempy);
}
treiangle.SetTriangle(temp[0],temp[1],temp[2]);
if(!treiangle.Test())
{
cout<<"输入数据不合法不能够形成三角形"< cout<<"重新输入数据"< }
else
{
cout<<"三角形的面积为"< cout<<"重新输入数据"< }
}
return 0;
}
VC6.0编译运行通过
测试图,以3,4,5为例
#include "math.h"
class CPoint//定义点
{
private:
double x,y;
public:
CPoint(double a,double b){x=a;y=b;}//构造函数
CPoint(){x=0;y=0;}
void Set(double a,double b){x=a,y=b;}//设置点坐标
double GetX(){return x;}
double GetY(){return y;}
};
class CTreiangle//三角形类
{
private:
CPoint x,y,z;
double s1,s2,s3;
public:
void SetTriangle(CPoint P1,CPoint p2,CPoint P3);
bool Test();
double Area();
};
void CTreiangle::SetTriangle(CPoint P1,CPoint p2,CPoint p3)//设置三角形的三个点并计算三条边的边长
{
x=P1;
y=p2;
z=p3;
s1=sqrt((x.GetX()-y.GetX())*(x.GetX()-y.GetX())+(x.GetY()-y.GetY())*(x.GetY()-y.GetY()));
s2=sqrt((x.GetX()-z.GetX())*(x.GetX()-z.GetX())+(x.GetY()-z.GetY())*(x.GetY()-z.GetY()));
s3=sqrt((z.GetX()-y.GetX())*(z.GetX()-y.GetX())+(z.GetY()-y.GetY())*(z.GetY()-y.GetY()));
}
bool CTreiangle::Test()//检测是不是三角形,原理三点不共线
{
if((y.GetX()-x.GetX())*z.GetY()==(y.GetY()-x.GetY())*z.GetX()+x.GetY()*y.GetY()-x.GetY()*x.GetY()-x.GetX()*y.GetY()+y.GetX()*x.GetY())
return false;
else
return true;
}
double CTreiangle::Area()//应用海伦公式计算面积并返回
{
double p,area;
p=(s1+s2+s3)/2.0;
area=sqrt(p*(p-s1)*(p-s2)*(p-s3));
return area;
}
int main()
{
CPoint temp[3];//定义三个点
double tempx,tempy;
CTreiangle treiangle;
while(1)
{
for(int i=0;i<3;i++)
{
cout<<"请输入第"< cin>>tempx;
cout<<"请输入第"< cin>>tempy;
temp[i].Set(tempx,tempy);
}
treiangle.SetTriangle(temp[0],temp[1],temp[2]);
if(!treiangle.Test())
{
cout<<"输入数据不合法不能够形成三角形"<
else
{
cout<<"三角形的面积为"<
}
return 0;
}
VC6.0编译运行通过
测试图,以3,4,5为例
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯