用C语言 定义一个关于空间点的结构体,它包含了点的3个坐标值,编写一个函数,计算两个点之间
定义一个关于空间点的结构体,它包含了点的3个坐标值,编写一个函数,计算两个点之间的距离,在主函数中调用。...
定义一个关于空间点的结构体,它包含了点的3个坐标值,编写一个函数,计算两个点之间的距离,在主函数中调用。
展开
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 <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 <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<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();
}
#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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询