博客
关于我
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/

你可能感兴趣的文章
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
mutiplemap 总结
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>