#include
#include
#include
using namespace std;
class Point {
double x, y;
public:
Point() {
x = 0; y = 0;
cout << "Constructor." << endl;
}
Point(double x_value, double y_value) {
x = x_value;
y = y_value;
}
double getX() {
return x;
}
double getY() {
return y;
}
void steXY(double x1, double y1) {
x = x1;
y = y1;
//cout << "1";不会输出,不知为何
}
void setX(double x_value) {
x = x_value;
}
void setY(double y_value) {
y = y_value;
}
double getDisTo(const Point &p) {
double a, b, c, s;
a = (x - p.x) * (x - p.x);
b = (y - p.y) * (y - p.y);
c = a + b;
s = sqrt(c);
return s;
}
void setXY(double x1, double y1)
{
x = x1;
y = y1;
}
~Point()
{
cout << "Distructor." << endl;
}
Point(const Point &p) {
x = p.x;
y = p.y;
}
};
class ArrayPoint {
public:
ArrayPoint(int n) {
num = n;
ptr = new Point[n];
}
~ArrayPoint() {
num = 0;
delete[]ptr;
}
Point& Element(int n){
return ptr[n];
}
private:
Point* ptr;
int num;
};
int main()
{
double max;
int m, x,y,t,a,b;
cin>>m;
for (int i = 0; i < m; i++) {
cin >> t;
ArrayPoint Points(t);
for (int i = 0; i < t; i++) {
cin >> x >> y;
Points.Element(i).setXY(x, y);
}
max= Points.Element(0).getDisTo(Points.Element(1));
a = 0;b=1;
for (int i = 0; i < t; i++) {
for (int j = i + 1; j < t; j++) {
if (Points.Element(i).getDisTo(Points.Element(j)) > max) {
max = Points.Element(i).getDisTo(Points.Element(j));
a = i;
b = j;
}
}
}
cout << "The longeset distance is " << fixed << setprecision(2) << max << "," << "between p[" << a << "] and p[" << b << "]." << endl;
};
}
这是我的输入
2
4
0 0
5 0
5 5
2 10
3
-1 -8
0 9
5 0
这是我的输出:
```c++
Constructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 10.44,between p[1] and p[3].
Distructor.
Distructor.
Distructor.
Distructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 17.03,between p[0] and p[1].
Distructor.
Distructor.
Distructor.
```完全没有这一行的输出 //cout << "1";不会输出,不知为何
没看到你的注释,析构函数执行但是看不到结果很正常。因为控制台可能先于对象销毁,之后没有输出。
不知道你这个问题是否已经解决, 如果还没有解决的话: