Problem F: B2 友元光顾
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 360 Solved: 225
[Submit][Status][Web Board]
Description
定义一个平面上的点类Point,其中设置成员函数distance1求当前对象与另一点的距离,并设置友员函数distance2也完成相同的工作。
请在划横线的部分填上需要的成份,使程序能正确运行,得到要求的输出结果。
请提交begin到end部分的代码。
//************* begin *****************
#include<iostream>
#include<cmath>
#include <iomanip>
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 &);
____(1)_____ double distance2(CPoint &, CPoint &);
};
double CPoint::distance1(CPoint &p)
{
double dx,dy;
dx=_____(2)_____;
dy=_____(3)_____;
return sqrt(dx*dx+dy*dy);
}
double distance2(CPoint &p1,CPoint &p2)
{
double dx,dy;
dx=_____(4)_____;
dy=_____(5)_____;
return sqrt(dx*dx+dy*dy);
}
//************* begin *****************
int main()
{
double px1, py1, px2, py2;
cin>>px1>>py1>>px2>>py2;
CPoint p1(px1, py1), p2(px2, py2);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<"1. "<<p1.distance1(p2)<<endl;
cout<<"2. "<<distance2(p1,p2)<<endl;
return 0;
}
Input
四个以空格隔开的小数,表示平面中两点的横纵坐标
Output
两次输出两点间的距离,分别用成员函数和友元函数完成计算
Sample Input
2.5 4.3 7.8 6.7
Sample Output
1. 5.82
2. 5.82
Submit:
//************* begin *****************
#include<iostream>
#include<cmath>
#include <iomanip>
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 &);
friend double distance2(CPoint &, CPoint &);
};
double CPoint::distance1(CPoint &p)
{
double dx,dy;
dx=x-p.x;
dy=y-p.y;
return sqrt(dx*dx+dy*dy);
}
double distance2(CPoint &p1,CPoint &p2)
{
double dx,dy;
dx=p1.x-p2.x;
dy=p1.y-p2.y;
return sqrt(dx*dx+dy*dy);
}
//************* begin *****************
int main()
{
double px1, py1, px2, py2;
cin>>px1>>py1>>px2>>py2;
CPoint p1(px1, py1), p2(px2, py2);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<"1. "<<p1.distance1(p2)<<endl;
cout<<"2. "<<distance2(p1,p2)<<endl;
return 0;
}