OJ第六批——Problem H: B 友元类-计算两点间距离
来源:互联网 发布:七友软件 编辑:程序博客网 时间:2024/05/24 01:55
问题及代码:
Problem H: B 友元类-计算两点间距离
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 396 Solved: 206
[Submit][Status][Web Board]
Description
类Distance定义为类Point的友元类来实现计算两点之间距离的功能。
Point类中有两个私有数据成员X和Y来表示点的两个坐标(横坐标和纵坐标), 成员函数需要自己定义。
主程序输入两个Point点的坐标,计算两个点之间的距离。
类Distance的声明如下:
class Distance
{ public:
float Dis(Point & p,Point & q);
};
程序主函数如下:
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;
}
要求:将整个程序补充完整,即添加Point类,并完成Distance类成员函数的类外定义。
注意:提交时不用提交主程序,其它都要提交。
Input
两个点的坐标(横坐标和纵坐标)
Output
两个点的距离(保留了两位小数)
Sample Input
1.0 1.0 2.0 2.0
Sample Output
1.41
HINT
#include <iostream> #include <cmath> #include <iomanip> using namespace std; class Point { protected: double dx; double dy; public: double getdx() { return dx; } double getdy() { return dy; } Point(double x,double y):dx(x),dy(y) {} friend class Distance; }; class Distance { public: float Dis(Point & p,Point & q); }; float Distance::Dis(Point &p,Point & q) { return sqrt((q.getdx()-p.getdx())*(q.getdx()-p.getdx())+(q.getdy()-p.getdy())*(q.getdy()-p.getdy())); } 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; }
0 0
- OJ第六批——Problem H: B 友元类-计算两点间距离
- 嘻唰唰第六批Problem H: B 友元类-计算两点间距离
- 第十六周oj刷题——Problem D: B 友元类-计算两点间距离
- YTU-OJ-友元类-计算两点间距离
- OJ友元类-计算两点间距离
- hdu-oj 2001 计算两点间距离
- YTU 2619: B 友元类-计算两点间距离
- [YTU]_2619 (B 友元类-计算两点间距离)
- OJ第二批——Problem D:友元类-计算两点间的距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 第八周任务—计算两点间距离
- 计算两点间距离公式
- 嘻唰唰第六批 Problem D: A代码完善--系统日期
- 如何在Linux系统Ubuntu版本下安装Python开发包NumPy、Matplotlib、SciPy
- 12天学好C语言——记录我的C语言学习之路(Day 12)
- 嘻唰唰第六批Problem E: AB编程题--世界杯小组赛
- 文件操作
- OJ第六批——Problem H: B 友元类-计算两点间距离
- 汉字统计
- 嘻唰唰第六批Problem F: A代码完善--简易二元运算
- 试用AWS的Lambda和API Gateway
- 内存泄露——不包含头文件类的析构函数不执行
- Android设计模式之美---Factory Method
- poj 2312 Battle City 优先队列
- day06
- OJ第六批——Problem I: B 继承 圆到圆柱体