对象作为数据成员

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

class CPoint
{
private:
    double x;  // 横坐标
    double y;  // 纵坐标
public:
    CPoint(double xx=0,double yy=0):x(xx),y(yy) {};//构造函数初始化;
    double Distance1(CPoint p) const;   // 两点之间的距离(一点是当前点,另一点为参数p)
    void input();  //以x,y 形式输入坐标点
    void output(); //以(x,y) 形式输出坐标点
    void judge(CPoint X,CPoint Y,CPoint Z);//判断三点是否在同一直线上
};
void CPoint::input()
{
    char a,b,c;
    cin>>a>>x>>b>>y>>c;
    if(a!='('||b!=','||c!=')')
        exit(0);
}
void CPoint::output()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
double CPoint:: Distance1(CPoint p) const
{
    double d;
    d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
    return d;

}
void CPoint::judge(CPoint X,CPoint Y,CPoint Z)//判断三点是否在同一直线上
{
    double k1,k2;
    k1=(Z.y-X.y)/(Z.x-X.x);
    k2=(Y.y-X.y)/(Y.x-X.x);
    if (k1==k2)
    {cout<<"此三点在同一直线上,构不成三角形!"<<endl;
    exit(0);}

}
class CTriangle
{
public:
    CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z) {} //给出三点的构造函数
    CTriangle(double m=0,double n=0,double z=0):a(m),b(n),c(z) {};//构造函数初始化
    void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
    float perimeter(void);//计算三角形的周长
    float area(void);//计算并返回三角形的面积
    bool isRightTriangle(); //是否为直角三角形
    bool isIsoscelesTriangle(); //是否为等腰三角形
private:
    CPoint A,B,C; //三顶点
    double a,b,c;//三边长
};
float CTriangle::perimeter(void)//计算三角形的周长
{
    double l;
    l=a+b+c;
    return l;
}
float CTriangle::area(void)//计算并返回三角形的面积
{
    double p,s;
    p=(a+b+c)/2;
    s=sqrt(p*(p-a)*(p-b)*(p-c));
    return s;
}
bool CTriangle::isRightTriangle() //是否为直角三角形
{
    bool f=0;
    if((a*a==b*b+c*c)||(b*b==a*a+c*c)||(c*c==a*a+b*b))
        f=1;
    return f;
}
bool CTriangle::isIsoscelesTriangle() //是否为等腰三角形
{
    bool h=0;

    if((a==b)||(b==c)||(a==c))
        h=1;
    return h;
}
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{

    double d1,d2,d3;
    d1=X.Distance1(Y);
    d2=Y.Distance1(Z);
    d3=Z.Distance1(X);
    a=d1;
    b=d2;
    c=d3;
}
int main()
{
    CPoint r,s,t,v;
    CTriangle u;
    r.input();
    s.input();
    t.input();
    cout<<"三角形的三边坐标为:"<<endl;
    r.output();
    s.output();
    t.output();
    v.judge(r,s,t);
    u.setTriangle(r,s,t);
    cout<<"三角形的周长为:"<<u.perimeter()<<endl;
    cout<<"三角形的面积为:"<<u.area()<<endl;
    while(u.isRightTriangle())
    {
        cout<<"三角形是直角三角形!"<<endl;
        break;
    }
    while(u.isIsoscelesTriangle())
    {
        cout<<"三角形是等腰三角形!"<<endl;
        break;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值