C语言 结构体:求空间两点的距离。下面这段代码,编译能通过,可是数据不...
发布网友
发布时间:2024-10-23 21:54
我来回答
共1个回答
热心网友
时间:2024-10-26 01:39
# include <stdio.h>
# include <math.h>
struct Point
{
double x;
double y;
double z;
};
double distance(struct Point p1,struct Point p2)
{
double d;
struct Point c;
c.x = (p1.x-p2.x)*(p1.x-p2.x);
c.y = (p1.y-p2.y)*(p1.y-p2.y);
c.z = (p1.z-p2.z)*(p1.z-p2.z);
d = sqrt(c.x+c.y+c.z);
return d;
}
void main ()
{
struct Point p1,p2;
double dis;
printf("Input:\n");
scanf("%lf%lf%lf",&p1.x,&p1.y,&p1.z);
scanf("%lf%lf%lf",&p2.x,&p2.y,&p2.z); //&lf 写错了吧
dis = distance(p1,p2);
printf("%.3lf\n",dis);
}