【发布时间】:2017-12-26 12:20:02
【问题描述】:
我必须使用函数 dist(double p[2][3]) 找到两对点之间的距离,每个点由三个坐标定义。 我将总共 12 个坐标放在一个文件中,用空格分隔它们。 我试过这样做,但循环永远不会中断,它只是一遍又一遍地打印前两点之间的距离......
#include <stdio.h>
#include <math.h>
double dist(double p[2][3])
{
double distance=sqrt(((p[0][0]-p[1][0])*(p[0][0]-p[1][0]))+((p[0][1]-p[1][1])*(p[0][1]-p[1][1]))+((p[0][2]-p[1][2])*(p[0][2]-p[1][2])));
return distance;
}
int main()
{
double p[2][3];
FILE*in;
in=fopen("file","r");
while(1)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
if(fscanf(in, "%lf ", &p[i][j])==EOF)
{break;}
}
}
printf("the distance is %lf\n", dist(p));
}
fclose(in);
}
【问题讨论】:
-
这个文件怎么样?你能提供一小部分吗?
-
当然!它只是一个文本文件,就像这样。 2.0 4.0 7.0 1.5 6.7 9.1 2.0 4.0 7.0 1.5 6.7 9.1