#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
private :
double x ;
double y ;
public :
CPoint( double xx = 0 , double yy = 0 );//构造函数声明
double Distance ( CPoint p ) const ;//求两点距离函数声明
void input() ;//按x , y的形式输入点
void output() ;//按(x,y)输出点的值
};
CPoint::CPoint( double xx , double yy )
{
x = xx ;
y = yy ;
}
void CPoint::input()
{
cout << "按 x , y 的形式输入坐标点" << endl ;
cin >> x >> y ;
}
void CPoint::output()
{
cout<< "点的坐标为:" <<"("<< x <<","<< y <<")"<< endl ;
}
double CPoint::Distance ( CPoint p ) const
{
double h ;
h = sqrt ((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y)) ;
return h ;
}
class CTriangle
{
public:
CTriangle(CPoint &x , CPoint &y , CPoint &z ):A(x),B(y),C(z){}//
void setTriangle (CPoint &x , CPoint &y , CPoint &z );//
double perimeter(void);//
double area(void);//
bool isRightTriangle();//
bool isIsoscelesTriangle();//
private:
CPoint A , B , C ;//
};
/*CTriangle:: CTriangle()
{
A=x ;
B=y ;
C=z ;
}*/
double CTriangle::perimeter(void)
{
return ( A.Distance(B) + A.Distance(C) + B.Distance(C) ) ;//
}
void CTriangle::setTriangle (CPoint &x , CPoint &y , CPoint &z )
{
A.input();
B.input();
C.input();
}
double CTriangle::area(void)
{
double s , m ;
m = ( A.Distance(B) + A.Distance(C) + B.Distance(C) )/2 ;
s = sqrt ( m * ( m - A.Distance(B) ) * ( m - A.Distance(C) ) * ( m - B.Distance(C) ) ) ;//
return s ;
}
bool CTriangle::isRightTriangle()
{
if ( abs(A.Distance(B)*A.Distance(B)+A.Distance(C)*A.Distance(C)-B.Distance(C)*B.Distance(C))<1e-6||abs( A.Distance(B)*A.Distance(B)+B.Distance(C)*B.Distance(C)-A.Distance(C)*A.Distance(C))<1e-6||abs(A.Distance(C)*A.Distance(C)+B.Distance(C)*B.Distance(C)-A.Distance(B)*A.Distance(B))<1e-6)
return true ;
else
return false ;
}
bool CTriangle::isIsoscelesTriangle()
{
if((abs(A.Distance(C)*A.Distance(C)-B.Distance(C)*B.Distance(C))<1e-6)||((abs(A.Distance(B)*A.Distance(B)-B.Distance(C)*B.Distance(C))<1e-6)||(abs(A.Distance(B)*A.Distance(B)-A.Distance(C)*A.Distance(C))<1e-6) ))
return true ;
else
return false ;
}
int main()
{
CTriangle ctr1(CPoint(3,6),CPoint(5,7),CPoint(8,6)) ;
//ctr1.setTriangle() ;//
cout<<"该三角形的周长为:"<<ctr1.perimeter()<<",面积为:"<<ctr1.area()<<endl<<endl;
cout<<"该三角形"<<(ctr1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;
cout<<"该三角形"<<(ctr1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;
system ("pause") ;
return 0 ;
}
运行结果:
感受:最后主函数中的后两个输出还有待掌握,原本想运行时输入三个点,ctr1.setTriangle() 却出现了“参数不能为空的错误”不理解啊。望老师给予宝贵意见,
想在运行时随意输入三个点。 呵呵
。谢谢老师啦。呵呵
我要继续加油!!!!!!!!!!