//任务五 main.cpp
#include <iostream>
#include <cmath>
#include "class.h"
using namespace std;
int main()
{
CTriangle ctr1(CPoint(6,6),CPoint(9,7),CPoint(10,5)) ;
//ctr1.setTriangle() ;//
cout<<"该三角形的周长为:"<<ctr1.perimeter()<<",面积为:"<<ctr1.area()<<endl<<endl;
cout<<"该三角形"<<(ctr1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;
cout<<"该三角形"<<(ctr1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;
system ("pause") ;
return 0 ;
}
// class.h //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)输出点的值 }; 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 ;// };
// Triangle.cpp #include "class.h" #include <cmath> 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 ; }
// CPoint.cpp #include <iostream> #include "class.h" #include <cmath> using namespace std ; 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 ; }
运行结果:
加油 继续吧!!!!