计算两点间距离
#include <stdio.h>#include <math.h>
typedef struct point {
double x;
double y;
} Point;
int main(int argc, char *argv[])
{
Point p1, p2;
double distance;
scanf("%lf%lf%lf%lf", &p1.x, &p1.y, &p2.x, &p2.y);
distance = sqrt((p1.x-p2.x)*(p1.x-p2.x)+
(p1.y-p2.y)*(p1.y-p2.y));
printf("%.3lf\n", distance);
return 0;
}
typedef:用Point代替struct point;
即 Point p1,p2;=struct point p1,p2;
第一个小写的point也是可以省略的,Point的定义如下:
或者 调用函数+结构体;#include <stdio.h>
#include <math.h>
typedef struct point
{
double x;
double y;
} Point;
double distance(Point p1, Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+
(p1.y-p2.y)*(p1.y-p2.y));
}
int main(int argc, char *argv[])
{
Point p1, p2;
scanf("%lf%lf%lf%lf", &p1.x, &p1.y, &p2.x, &p2.y);
printf("%.3lf\n", distance(p1, p2));
return 0;
}