/* *Copyright(c) 2016, 烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作 者:李德坤 *完成日期:2016年4月8日 *版本号:v1.0 * *问题描述:求两点之间的距离 *输入描述:两点坐标 *输出描述:两点距离 */ #include <iostream> #include<cmath> using namespace std; class CPoint { private: double x; // 横坐标 double y; // 纵坐标 public: CPoint(double xx=0,double yy=0):x(xx),y(yy){} float getjuli(CPoint &p1,CPoint &p2); friend float dist(CPoint &p1,CPoint &p2); double getx(){return x;} double gety(){return y;} }; float dist(CPoint &p1,CPoint &p2) { double a,b; a=(p1.x-p2.x)*(p1.x-p2.x); b=(p1.y-p2.y)*(p1.y-p2.y); return static_cast<float>(sqrt(a+b)); } float CPoint::getjuli(CPoint &p1,CPoint &p2) { double a,b; a=(p1.x-p2.x)*(p1.x-p2.x); b=(p1.y-p2.y)*(p1.y-p2.y); return static_cast<float>(sqrt(a+b)); } float getjuli(CPoint &p1,CPoint &p2); int main() { double x1,y1,x2,y2; float a,b,c; cout<<"请输入坐标:"<<endl; cin>>x1>>y1>>x2>>y2; CPoint t1(x1,y1); CPoint t2(x2,y2); cout<<"一般函数求两点之间的距离为:"; a=getjuli(t1,t2); cout<<a<<endl; cout<<endl; cout<<"成员函数求两点之间的距离为:"; b=t1.getjuli(t1,t2); cout<<b<<endl; cout<<endl; cout<<"友元函数求两点之间的距离为:"; c=getjuli(t1,t2); cout<<c<<endl; } float getjuli(CPoint &p1,CPoint &p2) { double a,b,c,d,e,f; a=p1.getx(); b=p2.getx(); c=p1.gety(); d=p2.gety(); e=(a-b)*(a-b); f=(c-d)*(c-d); return static_cast<float>(sqrt(e+f)); }