博客
关于我
C语言 求出平面直角坐标系中两点的距离
阅读量:502 次
发布时间:2019-03-07

本文共 1091 字,大约阅读时间需要 3 分钟。

在这里插入图片描述

#include 
#include
double dist(double x1, double y1, double x2, double y2){ return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));}int main(void){ double x1, y1; double x2, y2; puts("求两点间的距离。 \n"); puts("点A: "); printf("X坐标:"); scanf("%lf", &x1); printf("Y坐标:"); scanf("%lf", &y1); printf("A (%lf, %lf)\n", x1, y1); puts("点B: "); printf("X坐标:"); scanf("%lf", &x2); printf("Y坐标:"); scanf("%lf", &y2); printf("B(%lf, %lf)\n", x2, y2); printf("两点之间的距离d = %f\n", dist(x1, y1, x2, y2)); return 0;}

运行结果:

在这里插入图片描述
注:
double sqrt(double x)函数:
计算x的平方根(实参为复数时会发生定义域错误)。

使用结构体计算两点的距离
#include 
#include
#define sqr(n) ((n) * (n))typedef struct{ double x; double y;} Point;double distance_of(Point pa, Point pb){ return sqrt(sqr(pa.x - pb.x) + sqr(pa.y - pb.y));}int main(void){ Point crnt, dest; printf("当前地点的X坐标:"); scanf("%lf", &crnt.x); printf("当前地点的Y坐标:"); scanf("%lf", &crnt.y); printf("目的地点的Y坐标:"); scanf("%lf", &dest.x); printf("目的地点的Y坐标:"); scanf("%lf", &dest.y); printf("到目的地的距离为 %.2f。\n", distance_of(crnt, dest)); return 0; }

转载地址:http://oaccz.baihongyu.com/

你可能感兴趣的文章
Mysql join原理
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql where中如何判断不为空
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>