# 第六周实验报告4

#include<iostream>
using namespace std;
#include<Cmath>

class CPoint
{
private:
double x;  // 横坐标
double y;  // 纵坐标
public:

CPoint(double xx=0,double yy=0);
double Distance(CPoint p) const;   // 两点之间的距离(一点是当前点，另一点为参数p)
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();
double area();
bool isRightTriangle();
bool isIsoscelesTriangle();
private:
CPoint A,B,C;
};
void CPoint::input()
{

char m;

while(1)
{
cin>>x>>m>>y;
if(m !=',')
{
cout<<"输入错误"<<endl;

}
else
break;
}
}

void CPoint::output()
{
cout <<"("<< x <<","<< y<<")"<<endl;
}

double CPoint::Distance(CPoint p) const
{
double n;
n=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
return n;
}

CPoint::CPoint(double xx, double yy)
{
x=xx;
y=yy;
}

void CTriangle::setTriangle(CPoint &X, CPoint &Y, CPoint &Z)
{
A=X;
B=Y;
C=Z;
}

double CTriangle::perimeter()
{
double d1=A.Distance(B),d2=A.Distance(C),d3=B.Distance(C);
return (d1+d2+d3);
}

double CTriangle::area()
{
double d1,d2,d3,s;
d1=A.Distance(B);
d2=A.Distance(C);
d3=B.Distance(C);
s=(d1+d2+d3)/2;
return sqrt(s*(s-d1)*(s-d2)*(s-d3));
}
bool CTriangle::isRightTriangle()
{
double d1,d2,d3;
d1=A.Distance(B);
d2=A.Distance(C);
d3=B.Distance(C);
if((abs(d1*d1+d2*d2-d3*d3)<0.000001) || (abs(d1*d1+d3*d3-d2*d2)<0.000001) || (abs(d2*d2+d3*d3-d1*d1)<0.000001))
{
return true;
}
else
return false;
}
bool CTriangle::isIsoscelesTriangle()
{
double d1,d2,d3;
d1=A.Distance(B);
d2=A.Distance(C);
d3=B.Distance(C);
if((abs(d1-d2)<0.000001) || (abs(d1-d3)<0.000001) || (abs(d2-d3)<0.000001) )
{
return true;
}
else
return false;
}
int main()
{
CTriangle tri1(CPoint(0,0),CPoint(0,2),CPoint(2,0));
cout<<"周长是："<<tri1.perimeter()<<endl;
cout<<"面积是："<<tri1.area()<<endl;
cout<<"三角形"<<(tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;
cout<<"三角形"<<(tri1.isIsoscelesTriangle ()?"是":"不是")<<"等腰三角形"<<endl;
return 0;
}



• 本文已收录于以下专栏：

举报原因： 您举报文章：第六周实验报告4 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)