C++:友元1(两点之间的距离)
2016-01-08 18:49
477 查看
C++:友元1(两点之间的距离)
题目描述:
Description定义一个二维平面中的点(point)类,类中的数据成员为点的坐标,然后定义友元函数dist()用来计算两点之间的距离。
将下面的程序1 和程序2填写完整。
程序1 :
#include <iostream> #include <iomanip> #include <cmath> using namespace std; …………………………………… …………………………………… …………………………………… int main() { int n; double x1,x2,y1,y2; cin>>n; while (n--) { cin>>x1>>y1>>x2>>y2; point p1(x1,y1),p2(x2,y2); cout<<fixed<<setprecision(3)<<dist(p1,p2)<<endl; } return 0; }
程序2:
#include <iostream> #include <iomanip> #include <cmath> using namespace std; …………………………………… …………………………………… …………………………………… int main() { int n; double x1,x2,y1,y2; test t; cin>>n; while (n--) { cin>>x1>>y1>>x2>>y2; point p1(x1,y1),p2(x2,y2); cout<<fixed<<setprecision(3)<<t.dist(p1,p2)<<endl; } }
Input
输入包含n组测试例, 第1行是测试组数。
第2行–第n+1行为测试数据,每组测数据有4个实数,表示 2个点的坐标(x1,y1)和(x2,y2)。
Output
两点之间的距离(保留3位小数)。
Sample Input
2
0 0 3 4
1 1 2 2
Sample Output
5.000
1.414
代码块:
#include <iostream> #include<cmath> #include<iomanip> using namespace std; class point { double x,y,d,m; public: point(double r=0 , double i=0); friend point dis(point b, point c); void show(); }; point::point(double r, double i) { x = r; y = i; } void point::show() { double d=0,m=0; cout<<fixed<<setprecision(3)<<sqrt(x+y)*1.0<<endl; } point dis(point b, point c) { double x1, y1; x1 = (b.x - c.x)* (b.x - c.x); y1 = (b.y - c.y)*(b.y - c.y); return point (x1,y1); } int main() { double a,b,c,d; int n; cin>>n; for(int i=0;i<n;i++) { while (cin>>a>>b>>c>>d) { point c1(a,b),c2(c,d),c3,c4; c3 =dis(c1, c2); c3.show(); } } return 0; }
相关文章推荐
- C++:类与对象6(静态成员:平均分)
- iOS学习第二天,C语言知识学习
- 学习iOS第一天,iOS的简介,C语言的简介
- Sicily 1639. Run Length Encoding
- 【土狗之路】coursera上C语言基础第七周作业
- C语言实现一个最简单的队列
- 传智博客2015年~C语言深入浅出
- 经典排序算法的详细分析及C++实现代码
- c++学习之构造函数和析构函数篇
- 【Visual C++】游戏开发笔记十七 游戏基础算法(一) 游戏随机系统初步
- 想要学好C++,必看,能看完,证明你有学习C++的潜质,否则趁早放弃。
- c++ map unordered_map
- 【C语言】-返回指针的函数与指向函数的指针
- 【C语言】-指针和字符串
- 【C语言】-指向一维数组元素的指针
- 解读C++编程中类模板的三种特化
- C++中的常见错误
- c++error:C2664“intCWnd::MessageBoxW(LPCTSTR,LPCTSTR,UINT)”: 无法将参数 1 从“const char [12]”转换为“LPCTSTR”
- C++ 面向对象编程
- C语言 内存管理详解