代码:
//直角三角形,考察类的继承,类的方法,
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
//点类
class CPoint{
public:
int x,y;
CPoint(){
}
CPoint(int xx,int yy)
{
x=xx;
y=yy;
}
double operator-(CPoint p)
{
return pow(pow(x-p.x,2)+pow(y-p.y,2),0.5);
}
};
//直角三角形类
class CTriangle:public CPoint{
private:
double b1,b2,b3;//成员变量,代表三角形的三边长
public:
//构造函数
CTriangle (){
}
CTriangle (double a,double b,double c)
{
b1=a;b2=b;b3=c;
}
bool judgeA(){//判断是否为三角形
if(b1+b2>b3&&b1+b3>b2&&b2+b3>b1)return true;
else return false;
}
bool judgeB()//判断三角形是否为直角三角形
{
if((pow(b1,2)+pow(b2,2)==pow(b3,2))
||(pow(b2,2)+pow(b3,2)==pow(b1,2))
||pow(b1,2)+pow(b3,2)==pow(b2,2))
return true;
else return false;
}
double s()//计算周长
{
return b1+b2+b3;
}
};
int main()
{
int m;
cin>>m;
int x1,y1,x2,y2,x3,y3;
while(m--)
{
cin>>x1>>y1>>x2>>y2>>x3>>y3;
CPoint p1(x1,y1),p2(x2,y2),p3(x3,y3);
CTriangle ct(p1-p2,p1-p3,p2-p3);
if(ct.judgeB())
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
if(ct.judgeA())
cout<<fixed<<setprecision(2)<<ct.s()<<endl;
}
return 0;
}
代码中,根据点类中的减法(-)运算符,来计算求三角形的三边长。