在这里插入图片描述
在这里插入图片描述

注意:
1、若距离为0,则输出0,而非0.00;
2、友元函数的声明是要带上参数的。

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

class point
{
	double x;
	double y;
	
	public:
		point(double a1,double b1)
		{
			x=a1;
			y=b1;
		}
		friend void dis(point &p,line &l);
};

class line
{
	double a,b,c;
		
	public:
		line(double x,double y,double z)
		{
			a=x;
			b=y;
			c=z;
		}
		friend void dis(point &p,line &l);
};

void dis(point &p,line &l)
{

	double ans=abs(p.x*l.a+l.b*p.y+l.c)/sqrt(l.a*l.a+l.b*l.b);
	cout.precision(2);
	if(ans==0) 
	{
		cout<<"The distance is: 0";
	}
	else cout<<"The distance is: "<<fixed<<ans;
}
int main()
{
	double x,y,a,b,c;
	cin>>x>>y>>a>>b>>c;
	point p(x,y);
	line l(a,b,c);
	dis(p,l);	
	return 0;
}
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐