使用类的组合方式实现点、线段、三角形类C++

 【问题描述】

使用类的组合方式,实现点、线段、三角形类,请补充线段类与三角形类的代码。

【输入形式】

3个点的X坐标、Y坐标

【输出形式】

前两个点构成的线段长度、若三个点可以构成三角形,则输出三角形的周长和面积,否则输出“It is not a triangle!”


【样例输入1】

-1 -1 3 4 2 -5

【样例输出1】

6.40312

20.4585

15.5

【样例输入2】

1 2 3 4 5 6

【样例输出2】

2.82843

It is not a triangle!

#include<math.h>
#include <iostream>
using  namespace  std;

//点类
class Point
{
public:
    Point();
    Point(double x, double y);
    double getX();
    double getY();
private:
    double mX,mY;
};

Point::Point()
{

}
double Point::getX()
{
    return mX;
}

double Point::getY()
{
    return mY;
}

Point::Point(double x,double y):mX(x),mY(y)
{

}
//线段类
class Line
{
public:
    Line();
    Line(Point a, Point b);
    double GetDistance();    //返回线段的长度
private:
    Point pts,pte;
};

Line::Line(Point a,Point b){
    pts = a;
    pte = b;
}

double Line::GetDistance(){
    double a;
    a = sqrt(pow(pts.getX()-pte.getX(),2) + pow(pts.getY()-pte.getY(),2));
    return a;
}

//三角形类
class Triangle
{
public:
    Triangle(Point a,Point b,Point c);
    Triangle();
    double getGirth(); //三角形周长
    double getArea();  //三角形面积
    int IsATriangle(); //判断三个点是否能构成三角形
private:
    Point pa,pb,pc;
    Line la,lb,lc;
};
Triangle::Triangle(Point a,Point b,Point c):la(a,b),lb(a,c),lc(b,c){

}

double Triangle::getGirth(){
    return la.GetDistance()+lb.GetDistance()+lc.GetDistance();
}

double Triangle::getArea(){
    double halfGirth;
    halfGirth = (la.GetDistance()+lb.GetDistance()+lc.GetDistance())/2;
    return  sqrt(halfGirth*(halfGirth-la.GetDistance())
                *(halfGirth-lb.GetDistance())*(halfGirth-lc.GetDistance()));;
}

int Triangle::IsATriangle(){
    double a,b,c;
    a = la.GetDistance();
    b = lb.GetDistance();
    c = lc.GetDistance();
    if ((a+b > c)&(a+c > b) &(b+c > a))
        return 1;
    else
        return 0;
}


int main()
{
    double x1, x2, x3, y1, y2, y3;
    cin >> x1>> y1>> x2>> y2>> x3>> y3;
    Point p1(x1, y1), p2(x2, y2), p3(x3, y3);
    Line  line1(p1,p2);
    cout<<line1.GetDistance()<<endl;//线段长度
    Triangle t(p1, p2, p3);
    if(t.IsATriangle()==1)
    {
        cout << t.getGirth() << endl;//三角形的周长
        cout << t.getArea() << endl;//三角形的面积
    }
    else
    {
        cout<<"It is not a triangle!"<<endl;
    }
    return 0;
}



共有测试数据:3
平均占用内存:1.399K    平均运行时间:0.00351S
 

测试数据评判结果
测试数据1完全正确
测试数据2完全正确
测试数据3完全正确

附赠一个小题目

对时钟类进行运算符重载

【问题描述】对时钟类进行重载

 

#include <iostream>
using namespace std;
class Clock	//时钟类声明
{
public:	//外部接口
	Clock(int NewH=0, int NewM=0, int NewS=0);
	void ShowTime();
	 Clock operator ++();        //前置单目运算符重载
 //后置单目运算符重载
	 Clock  operator  ++(int);
   
 //+运算符重载
         Clock  operator  +(Clock c);
  
private:	//私有数据成员
	int Hour, Minute, Second;
};
Clock::Clock(int h,int m,int s){
    Hour = h; Minute = m ; Second = s;
}

Clock Clock::operator++(){
    Second++;
    return *this;
}

Clock Clock::operator++(int){
    Second++;
    if(Second>=60){
        Second = 0;
        Minute++;
        if(Minute>=60){
            Minute = 0;
            Hour++;
            if(Hour>=24){
                Hour = 0;
            }
        }
    }
    return *this;
}

Clock Clock::operator+(Clock c){
    Clock temp;
    temp.Hour = Hour+c.Hour;
    temp.Minute  = Minute+c.Minute;
    temp.Second = Second+c.Second;
    return temp;
}

void Clock::ShowTime()
{  
   cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}

int main()
{
	Clock c1(23,59,59),c2(5,12,10),c3;
	c1.ShowTime();
	c1++;
	c1.ShowTime();
	c3=++c2;
	c3.ShowTime();
	c3=c2+c1;
	c3.ShowTime();
	return 0;
}


  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kun.A.A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值