博客
关于我
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基础day07_mysql集群实例-MySQL 5.6
查看>>
Mysql基础命令 —— 数据库、数据表操作
查看>>
Mysql基础命令 —— 系统操作命令
查看>>
MySQL基础学习总结
查看>>
mysql基础教程三 —常见函数
查看>>
mysql基础教程二
查看>>
mysql基础教程四 --连接查询
查看>>
MySQL基础知识:创建MySQL数据库和表
查看>>
MySQL基础系列—SQL分类之一
查看>>
MySQL处理千万级数据分页查询的优化方案
查看>>
mysql备份
查看>>
mysql备份与恢复
查看>>
mysql备份工具xtrabackup
查看>>
mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
查看>>
mysql复制内容到一张新表
查看>>
mysql复制表结构和数据
查看>>
mysql复杂查询,优质题目
查看>>
MySQL外键约束
查看>>
MySQL多表关联on和where速度对比实测谁更快
查看>>
MySQL多表左右连接查询
查看>>