04737 c++ 自学考试2019版 第二章课后程序设计题 2

/*
*	04737 c++ 自学考试2019版 第二章课后练习
*	程序设计题 2
*	需求:设计并实现二维坐标系下点的类Point.... 
*/
//标准流 
#include<iostream>
//科学计算函数 
#include<cmath>
using namespace std;

class Point
{
	private:
		//横坐标 
		double x;
		//纵坐标 
		double y;
	public:
		//construtor
		Point();
		Point(double x1,double y1);
		//getter
		double getX();
		double getY();
		//setter
		void setX(double x1);
		void setY(double y1); 
		//计算给定点到0,0的距离
		double  distance(Point p);
		//计算给定两点之间的距离
		double  distanceB2Points(Point p,Point p2);
		//给定的三个点是否是三角形
		bool isTriangle(Point p,Point p2,Point p3);		 
};
//construtor
Point::Point(double x1,double y1)
{
	x = x1;
	y = y1;
}
//getter
double Point::getX(){return x;}
double Point::getY(){return y;}
//setter
void Point::setX(double x1){x = x1;}
void Point::setY(double y1){y = y1;}
//计算给定点到0,0的距离 公式: 根号(x1-x2)平方+(y1-y2)平方 的绝对值 
double Point::distance(Point p)
{
	return abs(	sqrt(	pow(p.getX(),2) + pow(p.getY(),2)	)	);
}
//计算两点之间的距离
double Point::distanceB2Points(Point p,Point p2)
{
	return abs(	sqrt(	pow(p.getX()-p2.getX(),2) + pow(p.getY()-p2.getY(),2)	)	);
}
//给定的三个点是否是三角形 任意两边之和大于第三边即成立  或者判断是否在一条直线 
bool Point::isTriangle(Point p,Point p2,Point p3)
{
	//第一条边
	double a =   abs(	sqrt(	pow(p.getX()-p2.getX(),2) + pow(p.getY()-p2.getY(),2)	)	);
	//第二条边
	double b =   abs(	sqrt(	pow(p.getX()-p3.getX(),2) + pow(p.getY()-p3.getY(),2)	)	);
	//第三条边
	double c =   abs(	sqrt(	pow(p2.getX()-p3.getX(),2) + pow(p2.getY()-p3.getY(),2)	)	);
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;
	cout<<"c="<<c<<endl;
	if( a+b > c && a+c > b &*& b+c > a)return true;
	return false;
}
//主函数
int main()
{
	Point p(3,3),p2(4,4),p3(5,6);
	cout<<p.distance(p)<<endl;
	cout<<p.distanceB2Points(p,p2)<<endl;
	cout<<p.isTriangle(p,p2,p3)<<endl;
} ```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值