/*
* 程序的版权和版本声明部分
* Copyright (c)2014, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: fibnacci.cpp
* 作 者:高古尊
* 完成日期:2014年4月3日
* 版本号: v1.0
*
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:
*/
#include <iostream>
#include <cmath>
using namespace std;
struct CPoint
{
double x;
double y;
};
class CTriangle
{
public:
CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){}//给出三点的构造函数
CTriangle(){}
void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
float perimeter(void);//计算三角形的周长
float area(void);//计算并返回三角形的面积
bool isRightTriangle(); //是否为三角形
bool isIsoscelesTriangle(); //是否为等腰三角形
bool bianchang(CPoint , CPoint);//计算边长
private:
CPoint A,B,C; //三顶点
};
int main()
{
CTriangle t;
CPoint X={0,0},Y={0,0},Z={0,0};
cout<<"请输入三角形的顶点坐标:";
cin>>X.x>>X.y>>Y.x>>Y.y>>Z.x>>Z.y;
t.setTriangle(X,Y,Z);
if(t.isRightTriangle())
{
if(t.isIsoscelesTriangle())
{
cout<<"是等腰三角形。"<<endl;
}
else
cout<<"是三角形,但不是等腰三角形"<<endl;
cout<<"三角形的周长为:"<<t.perimeter()<<endl;
cout<<"三角形的面积为:"<<t.area()<<endl;
}
else
{
cout<<"不是三角形。"<<endl;
}
return 0;
}
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{
A=X,B=Y,C=Z;
return;
}
float CTriangle::perimeter(void)
{
return bianchang(A,B)+bianchang(A,C)+bianchang(C,B);
}
float CTriangle::area(void)
{
float sum=2*(bianchang(A,B)+bianchang(A,C)+bianchang(C,B));
return sqrt(sum*(sum-bianchang(A,B))*(sum-bianchang(A,C))*(sum-bianchang(C,B)));
}
bool CTriangle::isRightTriangle()
{
bool f=false;
if(bianchang(A,B)<bianchang(A,C)+bianchang(C,B)&&
bianchang(C,B)<bianchang(A,C)+bianchang(A,B)&&
bianchang(A,C)<bianchang(A,B)+bianchang(C,B))
f=true;
return f;
}
bool CTriangle::isIsoscelesTriangle()
{
bool f=false;
if(bianchang(A,B)==bianchang(A,C)||
bianchang(C,B)==bianchang(A,C)||
bianchang(B,C)==bianchang(A,B))
f=true;
return f;
}
bool CTriangle::bianchang(CPoint A, CPoint B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
对象作为数据成员
最新推荐文章于 2020-04-30 20:45:02 发布