1. 实验13-1:计算两点间距离(结构体) 定义二维空间中,描述点的结构体类型,包括x轴和y轴坐标值。输入两个点的坐标,输出它们之间的距离,输出格式:%.2f 【输入形式】 两个点的坐标 【输出形式】 距离 【样例输入】 1.2 2.6 3.2 10.4 【样例输出】 8.05
时间: 2023-12-03 10:03:11 浏览: 60
好的,这是一个关于计算两点间距离的问题。以下是参考代码:
```c
#include <stdio.h>
#include <math.h>
struct point {
double x;
double y;
};
int main() {
struct point p1, p2;
double distance;
scanf("%lf %lf %lf %lf", &p1.x, &p1.y, &p2.x, &p2.y);
distance = sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
printf("%.2f", distance);
return 0;
}
```
这个代码定义了一个结构体类型 `point`,包含了两个成员变量 `x` 和 `y`,分别表示一个点的横坐标和纵坐标。然后通过 `scanf` 函数读入两个点的坐标,计算它们之间的距离并输出,距离的计算使用了数学库中的 `sqrt` 和 `pow` 函数。输出格式使用了 `printf` 函数的格式化输出,保留小数点后两位。
希望能够帮到你!
相关问题
设计二维平面的点 结构体point类型设计和函数计算两个点的距离
//定义点的结构体
struct point {
double x;
double y;
};
//计算两个点之间的距离
double distance(point p1, point p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx * dx + dy * dy);
}
编写程序 定义描述三维空间中点与线的结构体类型与变量,计算空间中线段的中间并输出
以下是定义描述三维空间中点与线的结构体类型与变量的程序示例,并计算空间中线段的中点并输出:
```c++
#include <iostream>
using namespace std;
struct Point3D { // 点的结构体
float x; // x坐标
float y; // y坐标
float z; // z坐标
};
struct Line3D { // 线的结构体
Point3D start; // 起点
Point3D end; // 终点
};
int main() {
// 创建两个点
Point3D p1 = {1.0f, 2.0f, 3.0f};
Point3D p2 = {4.0f, 5.0f, 6.0f};
// 创建线段
Line3D line = {p1, p2};
// 计算线段中点
Point3D midPoint = {
(line.start.x + line.end.x) / 2.0f,
(line.start.y + line.end.y) / 2.0f,
(line.start.z + line.end.z) / 2.0f
};
// 输出线段中点
cout << "线段中点坐标为 (" << midPoint.x << ", " << midPoint.y << ", " << midPoint.z << ")" << endl;
return 0;
}
```
输出结果为:
```
线段中点坐标为 (2.5, 3.5, 4.5)
```