[YTU]_2619 (B 友元类-计算两点间距离)
来源:互联网 发布:jcl外国语学院知乎 编辑:程序博客网 时间:2024/06/05 18:57
题目描述
类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类成员函数的类外定义。
注意:提交时不用提交主程序,其它都要提交。
输入
两个点的坐标(横坐标和纵坐标)
输出
两个点的距离(保留了两位小数)
样例输入
1.0 1.0 2.0 2.0
样例输出
1.41#include <iostream>#include <iomanip>#include <math.h>using namespace std;class Point;class Distance{public: float Dis(Point &p,Point &q);};class Point{public: friend class Distance; Point(float x1,float y1):x(x1),y(y1){}private: float x,y;};float Distance::Dis(Point &p,Point &q){ return sqrt((q.x-p.x)*(q.x-p.x)+(q.y-p.y)*(q.y-p.y));}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
- [YTU]_2619 (B 友元类-计算两点间距离)
- YTU 2619: B 友元类-计算两点间距离
- YTU-OJ-友元类-计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- 计算两点间距离
- OJ友元类-计算两点间距离
- 第十六周oj刷题——Problem D: B 友元类-计算两点间距离
- OJ第六批——Problem H: B 友元类-计算两点间距离
- 嘻唰唰第六批Problem H: B 友元类-计算两点间距离
- 计算两点间距离公式
- 计算两点间距离 tzc
- 1014. 两点间距离计算
- HDOJ2001_计算两点间距离
- myeclipse项目编码方式彻底设置
- 深入浅出OOP(五): C#访问修饰符
- android7.0中文API -- LinearLayout
- 快捷键清除浏览器缓存
- 利用Linux中的crontab实现分布式项目定时任务
- [YTU]_2619 (B 友元类-计算两点间距离)
- hdu 5975 树状数组原理题(16亚洲区域赛大连站)
- 2017校招面经(BAT、搜狗、搜狐、一点资讯、360、华为优招)
- 自定义View
- 怎么更改Web Project的 Web Context-root的路径
- 游戏引擎的联想
- ubuntu 安装ssh
- 4.Flume client 开发
- 面向对象分析类图