C language programming (third edition) He Qinming exercises 5-3
Article Directory
topic
Use the function to calculate the distance between two points:
Given the coordinates of any two points on the plane (x1, y1) and (x2, y2),
find the distance between these two points (with 2 decimal places).
It is required to define and call the function dist (x1, y1, x2, y2) to calculate the distance between two points. Try to write the corresponding program.
Analysis process
enter
Condition: Enter (x1, y1) and (x2, y2)
Output
Condition: the distance between the two points (retaining 2 decimal places), the function dist (x1, y1, x2, y2) is required to be defined and called to calculate the distance between the two points
Code
#include <stdio.h>
#include <math.h>
double dist(double x1, double y1, double x2, double y2);/*函数声明*/
int main () {
/*定义变量*/
double x1, y1, x2, y2; /*定义变量,存储输入的(x1,y1)和(x2,y2)*/
/*赋值*/
printf("请输入(x1,y1)和(x2,y2):\n"); /*输入提示*/
scanf("%lf %lf %lf %lf \n", &x1, &y1, &x2, &y2); /*输入并赋给变量*/
/*计算*/
printf("(%lf %lf)和(%lf %lf)之间的距离为%.2f \n", x1, y1, x2, y2, dist(x1, y1, x2, y2)); /*输出计算结果*/
return 0;
}
double dist(double x1, double y1, double x2, double y2){
return sqrt((pow(fabs(x1-x2), 2) + pow(fabs(y1-y2), 2)));
}