博客
关于我
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存储IP地址的数据类型
查看>>
mysql存储中文 但是读取乱码_mysql存储中文乱码
查看>>
MySQL存储引擎
查看>>
MySQL存储引擎
查看>>
MySQL存储引擎--MYSIAM和INNODB引擎区别
查看>>
Mysql存储引擎(1):存储引擎体系结构和介绍
查看>>
Mysql存储引擎(2):存储引擎特点
查看>>
MySQL存储引擎--MyISAM与InnoDB区别
查看>>
mysql存储总结
查看>>
mysql存储登录_php调用mysql存储过程会员登录验证实例分析
查看>>
MySql存储过程中limit传参
查看>>
MySQL存储过程入门
查看>>
mysql存储过程批量建表
查看>>
MySQL存储过程的使用实现数据快速插入
查看>>
mysql存储过程详解
查看>>
Mysql存表情符号发生错误
查看>>
MySQL学习-group by和having
查看>>
MySQL学习-MySQL数据库事务
查看>>
MySQL学习-MySQL条件查询
查看>>
MySQL学习-SQL语句的分类与MySQL简单查询
查看>>