这里给出了两种方法。
方法1
#include <stdio.h>
#include <math.h>
int main()
{
double x1,y1,z1,x2,y2,z2,distance;
printf("请输入第一个点的坐标:\n x1");
scanf("%lf",&x1);
printf("y1=");
scanf("%lf",&y1);
printf("z1=");
scanf("%lf",&z1);
printf("请输入第二个点的坐标:\n x2");
scanf("%lf",&x2);
printf("y2=");
scanf("%lf",&y2);
printf("z2=");
scanf("%lf",&z2);
distance=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
printf("这两点之间的距离为:%lf\n",distance);
return 0;
}
运行结果
方法2自定义函数
#include <stdio.h>
#include <math.h>
struct point
{
float x;
float y;
float z;
};
float dist(struct point p1,struct point p2)
{
float a,b,c;
float d;
a=fabs(p1.x-p2.x);
b=fabs(p1.y-p2.y);
c=fabs(p1.z-p2.z);
d=sqrt(a*a+b*b+c*c);
return d;
}
int main()
{
struct point p1,p2;
printf("Enter point p1: ");
scanf("%f,%f,%f",&p1.x,&p1.y,&p1.z);
printf("Enter point p2:");
scanf("%f,%f,%f",&p2.x,&p2.y,&p2.z);
printf("distance: %f\n",dist(p1,p2));
return 0;
}