/*
*Copyright(c)2014,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:曾晓
*完成日期:2015年 4月 10日
*版本号:v1.0
*/
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
public:
CPoint(double xx=0,double yy=0);
double Distance1(CPoint p) const;
double Distance0() const;
void input();
void output();
private:
double x,y;
};
CPoint::CPoint(double xx,double yy)
{
x=xx;
y=yy;
}
double CPoint::Distance1(CPoint p) const
{
double d;
d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
return d;
}
double CPoint::Distance0() const
{
double d;
d=sqrt(x*x+y*y);
return d;
}
void CPoint::input()
{
char ch;
cout<<"请以x,y形式输入坐标点:"<<endl;
\
while(1)
{
cin>>x>>ch>>y;
if(ch==',')
break;
else
cout<<"您的输入有误,请重新输入!"<<endl;
}
}
void CPoint::output()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
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; //三顶点
};
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{
A=X;
B=Y;
C=Z;
}
double CTriangle::perimeter(void)
{
double a=B.Distance1(C),b=C.Distance1(A),c=A.Distance1(B);
return a+b+c;
}
double CTriangle::area(void)
{
double a=B.Distance1(C),b=C.Distance1(A),c=A.Distance1(B);
double p;
p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
bool CTriangle::isRightTriangle()
{
double max;
double a=B.Distance1(C),b=C.Distance1(A),c=A.Distance1(B);
max=a;
if(b>a)
max=b;
if(c>a)
max=c;
if(((max==a)&&(b*b+c*c-a*a==0))||((max==b)&&(a*a+b*b-c*c==0))||((max==c)&&(a*a-b*b-c*c==0)))
return true;
else
return false;
}
bool CTriangle::isIsoscelesTriangle()
{
double a=B.Distance1(C),b=C.Distance1(A),c=A.Distance1(B);
if(a==b&&b==c&&a==c)
return true;
else
return false;
}
int main()
{
CPoint X(2,5),Y(5,2),Z(7,8);
CTriangle Tri1(X,Y,Z);
cout<<"该三角形的周长为:"<<Tri1.perimeter()<<",面积为:"<<Tri1.area()<<endl<<endl;
cout<<"该三角形"<<(Tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;
cout<<"该三角形"<<(Tri1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;
return 0;
}
运行结果:
Fighting! 有些事就是要逼自己去做才行