1974: 两点之间的距离(结构体专题)
题目描述
已知两点的坐标,求两点间的距离。
输入
两行,两点的坐标,中间用空格隔开
输出
它们之间的距离,保留两位小数
#include <stdio.h>
#include <string.h>
#include <math.h>
struct point{ //定义一个“点”结构体。
double x;
double y;
};
double sum(struct point p1,struct point p2)
{ //定义一个函数,用于计算两点间的距离。
double j;
j=sqrt(pow((p2.x-p1.x),2)+pow((p2.y-p1.y),2));
return j;
}
int main()
{
double j;
struct point p1,p2; //定义两个struct point 类型变量。
scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y);
j=sum(p1,p2); //调用sum函数。
printf("%.2lf",j);
return 0;
}