空间中两点间距离
#include<stdio.h>#include<math.h>
int main()
{
float GPS1,GPS2;
int i=0,j=0;
float d;
printf("Please input the first coordinate of the point:\n");
for(i;i<3;i++)
{
scanf("%f",GPS1);
}
printf("Please input the second coordinate of the point");
for(j;j<3;j++)//从这开始代码就没有继续运行下去了,为什么?如何改进?
{
printf("--------");
scanf("%f",GPS2);
}
d=sqrt((GPS1-GPS2)*(GPS1-GPS2)
+(GPS1-GPS2)*(GPS1-GPS1)
+(GPS1-GPS2)*(GPS1-GPS2));
printf("The distance of the two points is %f",d);
return 0;
} 两个输入函数没有&符号 scanf() 读取数值给出的参数必须是变量地址,不可以直接使用变量本身。
#include<stdio.h>
#include<math.h>
int main(void)
{
float d , e , GPS ;
int i , j ;
for(i = 0 ; i < 2 ; i ++) {
printf("Please input the No. %d coordinate of the point:\n" , i + 1) ;
for(j = 0 ; j < 3 ; j ++) scanf("%f" , & GPS) ;
}
for(e = 0.0 , i = 0 ; i < 3 ; i ++) e += (GPS - GPS) * (GPS - GPS) ;
d = sqrt(e) ;
printf("The distance of the two points is : %.6f\n" , d) ;
}
vc6.0 环境编译、运行实况
D:\0002.Exercise\C>cl x.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
x.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:x.exe
x.obj
D:\0002.Exercise\C>x
Please input the No. 1 coordinate of the point:
0 0 0
Please input the No. 2 coordinate of the point:
10 10 10
The distance of the two points is : 17.320508
D:\0002.Exercise\C>
页:
[1]