发布网友 发布时间:2024-10-23 21:54
共2个回答
热心网友 时间:2024-11-05 19:28
//定义Point类
class Point
{
public Point(){x = y = 0;}
public Point(double x,double y){this.x = x;this.y = y;}
public double Distance(const Point &p){return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p*y));}
public double x,y;
};
//测试代码
#include <iostream>
#include <math>
using namespace std;
class Point
{
public Point(){x = y = 0;}
public Point(double x,double y){this.x = x;this.y = y;}
public double Distance(const Point &p){return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p*y));}
public double x,y;
};
void main(){
Point p1(2,-3.5),p2(-2.1,3);
cout<<"距离为:"<<p1.Distance(p2)<<endl;
}
热心网友 时间:2024-11-05 19:27
#include<iostream>
#include<cmath>
using namespace std;
class Point //点类
{
public:
double sqrtToPoint(Point& q) {return sqrt((X-q.X)*(X-q.X)+(Y-q.Y)*(Y-q.Y));};//两点距离函数
Point ( float a = 0,float b = 0 ) //构造函数
{
X=a;
Y=b;
}
float Dis(Point& p, Point& q);
void Print()
{
cout << "X=" << X <<endl;
cout << "Y=" << Y <<endl;
}
private:
float X, Y;
};
float Point::Dis(Point& p, Point& q)
{
float result;
result = p.sqrtToPoint(q); //调用内部函数
cout << result << endl;
return result;
}
void main()
{
cout<<"测试两点为p(2,11),q(2, 21)"<<endl;
cout<<"p,q 之间距离为"<<ends;
Point d;
Point p(2, 11), q(2, 21);
d.Dis(p, q);
}