#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() 卻出現了“參數不能爲空的錯誤”不理解啊。望老師給予寶貴意見,
想在運行時隨意輸入三個點。 呵呵
。謝謝老師啦。呵呵
我要繼續加油!!!!!!!!!!