C++根据点的类构造线三角形类,并测试(江苏大学平时作业)

(附完整代码)问题描述: 根据点的类构造线三角形类,并测试,题目如下(部分代码如下)

class  myPoint
{
public:
        myPoint();
        myPoint(double  x,  double  y);
        double  getX();
        double  getY();
private:
        double  mX,mY;
};
 








int  main()
{
        double  x1,  x2,  x3,  y1,  y2,  y3;
        cout  <<  "请输入点1的x的值:";
        cin  >>  x1;
        cout  <<  "请输入点1的y的值:";
        cin  >>  y1;
        cout  <<  "请输入点2的x的值:";
        cin  >>  x2;
        cout  <<  "请输入点2的y的值:";
        cin  >>  y2;
        cout  <<  "请输入点3的x的值:";
        cin  >>  x3;
        cout  <<  "请输入点3的y的值:";
        cin  >>  y3;
        cout  <<  "点1的坐标为:("  <<  x1  <<  ","  <<  y1  <<  ")"  <<  endl;
        cout  <<  "点2的坐标为:("  <<  x2  <<  ","  <<  y2  <<  ")"  <<  endl;
        cout  <<  "点3的坐标为:("  <<  x3  <<  ","  <<  y3  <<  ")"  <<  endl;
        myPoint  p1(x1,  y1),  p2(x2,  y2),  p3(x3,  y3);
        Line    line1(p1,p2);
        cout<<"线长度:"<<line1.GetDistance()<<endl;
        Triangle  t(p1,  p2,  p3);
        cout  <<  "该三角形的周长为:"  <<  t.getGirth()  <<  endl;
        cout  <<  "该三角形的面积为:"  <<  t.getArea()  <<  endl;
        return  0;
}

 完整代码如下:

class  myPoint//点类
{
public:
        myPoint();//缺省赋值
        myPoint(double  x,  double  y);//默认构造函数
        double  getX();//获得x坐标
        double  getY();//获得y坐标
private:
        double  mX,mY;//x坐标y坐标
};
#include<iostream>
#include<cmath>//包含sqrt的头文件
using namespace std;
myPoint::myPoint(double x,double y)//函数实现
{mX=x;
    mY=y;
}
myPoint::myPoint()
{mX=0;
    mY=0;
}
double myPoint::getX()
{return mX;}
double myPoint::getY()
{return mY;}
class Line//线类
{myPoint x;//定义2个点
    myPoint y;
public:
    Line(myPoint b,myPoint k):x(b),y(k)//初始化
    {
    }
    double GetDistance()//计算线段长度的函数
    {double b;
        b=sqrt((x.getX()-y.getX())*(x.getX()-y.getX())+((x.getY()-y.getY())*(x.getY()-y.getY())));
        return b;
    }};
    class Triangle//三角类
    {myPoint x,y,z;//定义3个点
    public:
        Triangle(myPoint h,myPoint s,myPoint a):x(h),y(s),z(a)//初始化
        {
        }
        double getGirth()//计算三角形周长的函数
        {Line a(x,y);//定义三条线段
            Line b(y,z);
            Line c(x,z);
            double C;
            C=a.GetDistance()+b.GetDistance()+c.GetDistance();//调用线类的函数
            return C;
        }
        double getArea()//海伦公式=√[p(p-a)(p-b)(p-c)],计算三角形面积的函数
        {Line a(x,y);//定义三条线段
            Line b(y,z);
            Line c(x,z);
            double p;
            p=(this->getGirth())/2;
            double f;
            f=sqrt(p*(p-a.GetDistance())*(p-b.GetDistance())*(p-c.GetDistance()));//海伦公式计算三角形面积
            return f;
        }
    };
int  main()
{
        double  x1,  x2,  x3,  y1,  y2,  y3;
        cout  <<  "请输入点1的x的值:";
        cin  >>  x1;
        cout  <<  "请输入点1的y的值:";
        cin  >>  y1;
        cout  <<  "请输入点2的x的值:";
        cin  >>  x2;
        cout  <<  "请输入点2的y的值:";
        cin  >>  y2;
        cout  <<  "请输入点3的x的值:";
        cin  >>  x3;
        cout  <<  "请输入点3的y的值:";
        cin  >>  y3;
        cout  <<  "点1的坐标为:("  <<  x1  <<  ","  <<  y1  <<  ")"  <<  endl;
        cout  <<  "点2的坐标为:("  <<  x2  <<  ","  <<  y2  <<  ")"  <<  endl;
        cout  <<  "点3的坐标为:("  <<  x3  <<  ","  <<  y3  <<  ")"  <<  endl;
        myPoint  p1(x1,  y1),  p2(x2,  y2),  p3(x3,  y3);
        Line    line1(p1,p2);
        cout<<"线长度:"<<line1.GetDistance()<<endl;
        Triangle  t(p1,  p2,  p3);
        cout  <<  "该三角形的周长为:"  <<  t.getGirth()  <<  endl;
        cout  <<  "该三角形的面积为:"  <<  t.getArea()  <<  endl;
        return  0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值