今天对小学期第三个题目进行解答,三个点的坐标判断能否形成三角形,形成什么三角形,难点在于取精度为double是,不能直接使用“==”来判断边长是否相等,而是需要用约等于,限制两个double的差来判断是否等于,除此之外就没有其他难点。
附代码:
#include <iostream>
#include <math.h>
using namespace std;
class Point
{
public:
double x;
double y;
Point (){};
~Point(){};
};
class Triangle
{
public:
Point m_a;
Point m_b;
Point m_c;
Triangle(){cout<<"输入三个点的坐标:"<<endl;
cin>>m_a.x >>m_a.y >>m_b.x >>m_b.y >>m_c.x >>m_c.y ;};
Triangle(double a,double b,double c,double d,double e,double f)
{
m_a.x =a;m_a.y =b;m_b.x =c;m_b.y =d;m_c.x=e;m_c.y =f;
}
void T()
{
double a;
double b;
double c;
double temp=0;
a=sqrt((m_a.x -m_b.x )*(m_a.x -m_b.x )+(m_a.y -m_b.y )*(m_a.y -m_b.y ));
b=sqrt((m_a.x -m_c.x )*(m_a.x -m_c.x )+(m_a.y -m_c.y )*(m_a.y -m_c.y ));
c=sqrt((m_c.x -m_b.x )*(m_c.x -m_b.x )+(m_c.y -m_b.y )*(m_c.y -m_b.y ));
if(a<b) {temp=b;b=a;a=temp;}
if(b<c) {temp=b;b=c;c=temp;}
if(b+c<a||a-b>c) {cout<<"不能构成三角形!"<<endl;}
else if(a-b<=0.01&&b-c<=0.01) {cout<<"该三角形是等边三角形!"<<endl;}
else if((a-b<=0.01&&b-c>0.01)||(a-b>0.01&&b-c<0.1)||(a-c<0.01&&a-b>0.01))
{
double d=a*a-(b*b+c*c);
if(d<0.01){cout<<"该三角形是直角等腰三角形!"<<endl;}else {cout<<"该三角形是等腰三角形"<<endl;}}
else if(a*a-(b*b+c*c)<0.01){cout<<"该三角形是直角三角形!"<<endl;}
}
};
int main()
{
Triangle a;
a.T ();
return 0;
}