43课的第二题
我这个程序一直执行不了打印距离那里,我对比了以下答案感觉没啥区别,请各位帮我看看是哪里出了问题!!!#include<stdio.h>
#include<math.h>
struct SYSTEM
{
double x;
double y;
double z;
};
int main(void)
{
int n;
int j;
printf("请输入需要录入数据的数量;");
scanf("%d",&n);
struct SYSTEM system;
int i;
for(i=0;i<n;i++)
{
printf("请输入第%d个点的x坐标;",i+1);
scanf("%lf",&system.x);
printf("请输入第%d个点的y坐标;",i+1);
scanf("%lf",&system.y);
printf("请输入第%d个点的z坐标;",i+1);
scanf("%lf",&system.z);
}
double distant;
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
distant=sqrt(pow(system.x-system.x,2)+pow(system.y-system.y,2)+pow(system.z-system.z,2));
printf("第%d个点和第%d个点之间的距离大约是 %lf\n",i+1,j+1,distant);
}
j++;
}
return 0;
} {:10_277:} 在每个scanf语句后面加个getchar()试试 jhq999 发表于 2021-10-22 21:50
在每个scanf语句后面加个getchar()试试
可以哦,但这是为啥 superswagy2002 发表于 2021-10-22 22:27
可以哦,但这是为啥
因为缓冲区有个回车‘\n’需要getchar接收 jhq999 发表于 2021-10-22 22:40
因为缓冲区有个回车‘\n’需要getchar接收
那我以后怎么能知道什么时候用这个来接收 按一下回车一个,多加getchar只不过你再按下回车程序继续运行 jhq999 发表于 2021-10-22 22:57
按一下回车一个,多加getchar只不过你再按下回车程序继续运行
了解谢谢 {:10_256:} {:10_254:} #include <stdio.h>
#include <math.h>
struct POINTS { double x, y, z; };
int main()
{
int n;
printf("请输入坐标的数量:"); scanf("%d", &n);
struct POINTS p;
for(int i = 0; i < n; i++)
{
printf("请输入第 %d 个坐标 x y z:", i+1);
scanf("%lf %lf %lf", &p.x, &p.y, &p.z);
getchar();
}
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(i != j) printf("坐标 (%.1lf, %.1lf, %.1lf) 和 坐标 (%.1lf, %.1lf, %.1lf) 的距离大约是:%.2lf\n", p.x, p.y, p.z, p.x, p.y, p.z, sqrt(pow(p.x-p.x, 2) + pow(p.y-p.y, 2) + pow(p.y-p.y, 2)));
return 0;
}请输入坐标的数量:2
请输入第 1 个坐标 x y z:3 4 5
请输入第 2 个坐标 x y z:5 4 3
坐标 (3.0, 4.0, 5.0) 和 坐: (5.0, 4.0, 3.0) 的距离大约是:2.00
坐标 (5.0, 4.0, 3.0) 和 坐标 (3.0, 4.0, 5.0) 的距离大约是:2.00 {:10_256:} 谢谢你 {:5_95:} {:10_256:} 2 222222222222222222222222222;;;;;;;;;;;;;
{:10_256:}
页:
[1]