Description
类Distance定义为类Point的友元类来实现计算两点之间距离的功能。
Point类中有两个私有数据成员X和Y来表示点的两个坐标(横坐标和纵坐标), 成员函数需要自己定义。
主程序输入两个Point点的坐标,计算两个点之间的距离。
Input
两个点的坐标(横坐标和纵坐标)
Output
两个点的距离(保留了两位小数)
Sample Input
1.0 1.0 2.0 2.0
Sample Output
1.41
/* All rights reserved.
* 文件名称:test.cpp
* 作者:陈丹妮
* 完成日期:2015年 6 月 25 日
* 版 本 号:v1.0
*/
#include <iostream>
#include <iomanip>
using namespace std;
#include <cmath>
class Point;
class Distance
{
public:
float Dis(Point & p,Point & q);
};
class Point
{
public:
Point(double xx,double yy):x(xx),y(yy) {}
friend Distance;
private:
double x;
double y;
};
float Distance::Dis(Point & p,Point & q)
{
double s;
s=sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
return s;
}
int main()
{
float x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
Point p(x1,y1), q(x2,y2);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Distance d;
cout<<d.Dis(p,q)<<endl;
return 0;
}
学习心得:很顺利,继续努力!!讲刷题进行到底!!