用C语言 定义一个关于空间点的结构体,它包含了点的3个坐标值,编写一个函数,计算两个点之间

   更新日期:2024.05.28

1、首先我们找到头文件与main函数之间。

2、写上,我们的第一个关键字【struct】。

3、然后我们对该结构体进行命名。

4、在里面,我们便可以编写他的成员。

5、可以编写数组也可以是普通变量。

6、书写完毕后,我们一定要用分号结束。



#include <stdio.h>
#include <math.h>

typedef struct
{
int x;
int y;
int z;
}Point;

double distance3D(Point p1, Point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) + (p1.z - p2.z) * (p1.z - p2.z));
}

int main()
{
Point p1, p2;
p1.x = 3; p1.y = 4; p1.z = 5;
p2.x = 0; p2.y = 0; p2.z = 0;
printf("%lf\n", distance3D(p1, p2));
return 0;
}

c++版本:

#include <iostream>
#include <cmath>

using namespace std;

struct Point
{
int x;
int y;
int z;
Point(int xx, int yy, int zz)
{
x = xx;
y = yy;
z = zz;
}
double distance(Point p2)
{
return sqrt((p2.x - x) * (p2.x - x) + (p2.y - y) * (p2.y - y) + (p2.z - z) * (p2.z - z));
}
};

int main()
{
Point p1(3, 4, 5), p2(0, 0, 0);
cout << p1.distance(p2) << endl;
return 0;
}

#include <stdio.h>
#include <math.h>

typedef struct situation
{
int x;
int y;
int z;
}STTN;

float Math(STTN A, STTN B);
void main()
{
STTN A, B;
printf("A:(x,y,z)=");
scanf("(%d,%d,%d)", &A.x, &A.y, &A.z);//按格式输入
printf("B:(x,y,z)=");
scanf("(%d,%d,%d)", &B.x, &B.y, &B.z);//按格式输入

printf("|A,B|=%f", Math(A,B));
}

float Math(STTN A, STTN B)
{
float lenth;
lenth=sqrt(((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z)));
return lenth;
}

#include<stdio.h>
#include<math.h>

struct point
{
int x,y,z;
};
double distance(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
void main()
{
int i;
point a,b,c;
puts("请依次输入x、y、z的坐标值:");
scanf("%d%d%d",&a.x,&a.y,&a.z);
puts("请依次输入x、y、z的坐标值:");
scanf("%d%d%d",&b.x,&b.y,&b.z);
puts("请依次输入x、y、z的坐标值:");
scanf("%d%d%d",&c.x,&c.y,&c.z);
puts("各点之间的距离为:");
printf("a、b:%6.2f\na、c:%6.2f\nb、c:%6.2f\n",distance(a,b),distance(a,c),distance(b,c));
}

#include<stdio.h>
#include<math.h>

struct SP
{
int x;
int y;
int z;
};

float Distance(struct SP point1, struct SP point2)
{
float Dis;
Dis=(float)(sqrt(pow((point1.x-point2.x),2)+pow((point1.y-point2.y),2)+pow((point1.z-point2.z),2)));
return Dis;
}

main()
{
struct SP P1;
struct SP P2;
printf("Please input the first point(x y z):");
scanf("%d%d%d",&P1.x,&P1.y,&P1.z);
printf("Please input the second point(x y z):");
scanf("%d%d%d",&P2.x,&P2.y,&P2.z);

printf("The distance of the two point is %f\n",Distance(P1,P2));
getch();
}

相关链接

欢迎反馈与建议,请联系电邮
2024 © 视觉网