【C语言】求两个坐标点之间的距离

方法一:

注意:结构体初始化是两种方法。

#include <stdio.h>
#include <math.h>

struct point
{
        float x;        //x axis
        float y;        //y axis
};

int main(void)
{
        float x,y;
        struct point p;
        struct point p2 = {1.5,2.5}; //结构体初始化
        float distance;
        float dx,dy;
        //p = p2; //结构体赋值
        printf("demo struct\n");

        scanf("%f %f",&p.x,&p.y);
        printf("p.x = %.1f,p.y = %.2f\n",p.x,p.y);

        dx = p.x - p2.x;   //x 差值
        dy = p.y - p2.y;   //y 差值
        distance = sqrt(dx * dx + dy * dy);
        printf("distance = %f\n",distance);
#if 0   
        printf("sizeof struct point = %d\n",sizeof(struct point));
        printf("sizeof float = %d\n",sizeof(float));
        printf("sizeof double = %d\n",sizeof(double));
#endif

        return 0;
}


猜你喜欢

转载自blog.csdn.net/zjy900507/article/details/80924073