具体代码如下(仔细看下应该能明白):
/*输入n个点,求平面上所有各点之间的最长距离,要求定义和调用函数计算距离*/
#include
#include
#include
typedef struct point
{
double x;
double y;
}point;
double calculate_distance(point* a, point* b)
{
double i = a->x - b->x;
double j = a->y - b->y;
double k = pow(i, 2) + pow(j, 2);
return sqrt(k);
}
void main()
{
int i = 0;
int j = 0;
int n = 0;
double distance = 0;
double max_distance = 0;
printf("Enter n:");
scanf("%d", &n);
point* store_point = NULL;
store_point = (point*)malloc(n*sizeof(point));
point* i_point = store_point;
printf("input x, y\n");
for (i=0; i{
scanf("%lf %lf", &(i_point->x), &(i_point->y));
i_point++;
}
i_point = store_point;
for (i=0; i{
for (j=1; j{
distance = calculate_distance((i_point+i), (i_point+j));
if (distance > max_distance)
{
max_distance = distance;
}
}
}
free(store_point);
store_point = i_point = NULL;
printf("Max Distance = %lf\n", max_distance);
}