2个回答
展开全部
声明x1、y1、x2、y2浮点型变量为点p1和p2的座标,输入数值后直接由公式√(x1-x2)^2+(y1-y2)^2求出。代码如下:
#include "stdio.h"
#include "math.h"//调用sqrt需要包含此文件
int main(int argc,char *argv[]){
double x1,y1,x2,y2;
printf("Please enter the coordinates of 2 points...\n");
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);//输入点座标
printf("The distance(p1(%g,%g) to p2(%g,%g)) is ",x1,y1,x2,y2);
printf("%g\n",sqrt((x1-=x2)*x1+(y1-=y2)*y1));//直接用公式求结果
return 0;
}
运行样例如下:
2015-11-01
展开全部
#include <stdio.h>
typedef struct {
double x;
double y;
} Position;
double get_distance(Position a, Position b) {
double distance = sqrt((a.x - b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
return distance;
}
int is_online(Position a, Position b, Position c) {
int flag = 0;
printf("a(%.2lf, %.2lf), b(%.2lf, %.2lf), c(%.2lf, %.2lf) ",
a.x, a.y, b.x, b.y, c.x, c.y);
if (a.x*b.y == a.y*b.x
&& a.x*c.y == a.y*c.x
&& b.x*c.y == b.y*c.x) {
flag = 1;
}
return flag;
}
int main(void)
{
Position a = {0, 0}, b = {4, 3}, c = {8, 6};
double distance = get_distance(a, b);
printf("a(%.2lf, %.2lf), b(%.2lf, %.2lf), distance=%.2lf\n",
a.x, a.y, b.x, b.y, distance);
if (is_online(a, b, c)) {
printf("online\n");;
} else {
printf("disonline\n");
}
b.y = 2;
c.x = 9;
distance = get_distance(a, b);
printf("a(%.2lf, %.2lf), b(%.2lf, %.2lf), distance=%.2lf\n",
a.x, a.y, b.x, b.y, distance);
if (is_online(a, b, c)) {
printf("online\n");;
} else {
printf("disonline\n");
}
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询