第六周任务4

#include<iostream>  
  
#include<cmath>  
  
using namespace std;  
  
class CPoint  
{  
private:  
      
    mutable double x;  // 横坐标  
      
    mutable double y;  // 纵坐标  
      
public:  
      
    CPoint(double xx=0,double yy=0);  
      
    double Distance(CPoint p);   
      
    void input();  //以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(); //是否为等腰三角形  
      
    double geta();//用于取出私有数据成员的值  
      
    double getb();//用于取出私有数据成员的值  
      
    double getd();//一开始用了getc()...差点玩死自己...  
      
      
      
private:  
      
    CPoint A,B,C; //三顶点  
      
    double a, b, c;//三条边  
};  
  
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)//取三角形三边长度  
{  
    A = X;  
      
    B = Y;  
      
    C = Z;  
      
    a = B.Distance(C);//对三条边取长度  
      
    b = C.Distance(A);  
      
    c = A.Distance(B);  
}  
  
double CTriangle::geta()//类外定义  
{  
    return a;  
}  
  
  
double CTriangle::getb()  
{  
    return b;  
}  
  
double CTriangle::getd()  
{  
    return c;  
}  
  
double CTriangle::perimeter(void)//计算三角形的周长  
{  
    return a + b + c;  
}  
  
double CTriangle::area(void)//计算并返回三角形的面积  
{  
    double p = (a + b + c) / 2;  
      
    return  sqrt(p * (p - a) * (p - b) * (p - c));    
}  
  
bool CTriangle::isRightTriangle()//判断三角形是否为直角三角形  
{  
    if(abs(a * a - b * b - c * c) < 1e-6 || abs(b * b - a * a - c * c) < 1e-6 || abs(c * c - a * a - b * b) < 1e-6  )  
    {  
        return true;  
    }  
      
    else  
    {  
        return false;  
    }  
}  
  
bool CTriangle::isIsoscelesTriangle()//判断三角形是否为等腰三角形  
{  
    if(abs(a - b) < 1e-6 || abs(a - c) < 1e-6 || abs(b - c) < 1e-6 )  
    {  
        return true;  
    }  
      
    else  
    {  
        return false;  
    }  
}  
  
CPoint::CPoint(double xx, double yy)  
{  
    x = xx;  
      
    y = yy;  
}  
  
double CPoint::Distance(CPoint p) //计算两点间距离  
{  
    return(sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)));  
}  
  
void CPoint::input()//输入坐标  
{  
    char z;  
      
    while(1)  
    {  
        cout << "请以 x , y 形式输入坐标点:";  
          
        cin >> x >> z >> y;  
          
        if(z == ',')  
        {  
            break;  
        }  
          
        else  
        {  
            cout<< "输入格式有误!!!" << endl;  
        }  
    }  
}  
  
void main()  
{  
    CPoint p1, p2, p3;  
      
    CTriangle tri(p1, p2, p3);  
      
    double b1, b2, b3;  
      
    while(1)  
    {  
        p1.input();  
          
        p2.input();  
          
        p3.input();  
          
        tri.setTriangle(p1, p2, p3);//经过多次走查证明···应该放在这里···  
          
        b1 = tri.geta();  
          
        b2 = tri.getb();  
          
        b3 = tri.getd();  
          
        if((b1 + b2) > b3 && (b2 + b3) && (b1 + b3) > b2)  
        {  
            break;  
        }  
          
        else  
        {  
            cout << "输入三点的坐标无法构成三角形,请重新输入!!!" <<endl;  
        }  
    }  
      
    cout << "三角形的周长为:" << tri.perimeter() << endl;  
      
    cout << "三角形的面积为:" << tri.area() << endl;  
      
    cout << "该三角形" << (tri.isRightTriangle()?"是":"不是") << "直角三角形" << endl;    
      
    cout << "该三角形" << (tri.isIsoscelesTriangle()?"是":"不是") << "等腰三角形" << endl;    
      
    system("pause");  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值