2。注意程序的健壮性
#include<iostream> #include<math.h> using namespace std; class CPoint { private: int x,y; public: CPoint(int xx,int yy){x=xx;y=yy;} float operator- (CPoint c); }; float CPoint::operator- (CPoint c) { return sqrt((float)(x-c.x)*(x-c.x)+(float)(y-c.y)*(y-c.y)); } class ctriangle { private: CPoint A,B,C; float AB,BC,AC; public: ctriangle(CPoint a,CPoint b,CPoint c):A(a),B(b),C(c) { AB=A-B; BC=B-C; AC=A-C; } void display(); bool fun(); }; bool ctriangle::fun() { float a=AB,b=BC,c=AC,t; if(a>c) { t=c; c=a; a=t; } if(b>c) { t=b; b=c; c=t; } if((b*b+a*a-c*c)<10e-6) return true; else return false; } void ctriangle::display() { cout<<"直角三角形的周长为:"<<AB+BC+AC<<endl; } int main() { int x1,x2,x3,y1,y2,y3; cout<<"请依次输入三个点的坐标:"; cin>>x1>>y1>>x2>>y2>>x3>>y3; CPoint a(x1,y1),b(x2,y2),c(x3,y3); ctriangle T(a,b,c); if(T.fun()) T.display(); else cout<<"不是直角三角形!"<<endl; system("pause"); return 0; }