中易网

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

答案:6  悬赏:30  
解决时间 2021-04-27 18:11
  • 提问者网友:夕夏残阳落幕
  • 2021-04-27 10:42
定义一个关于空间点的结构体,它包含了点的3个坐标值,编写一个函数,计算两个点之间的距离,在主函数中调用。
最佳答案
  • 二级知识专家网友:放肆的依賴
  • 2021-04-27 12:05
1、首先我们找到头文件与main函数之间。



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



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



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



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



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

全部回答
  • 1楼网友:伤口狠精致
  • 2021-04-27 16:32
#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; }
  • 2楼网友:萌萌哒小可爱
  • 2021-04-27 15:02
#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(); }
  • 3楼网友:恕我颓废
  • 2021-04-27 14:11
#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)); }
  • 4楼网友:陪我到地狱流浪
  • 2021-04-27 13:03
#include<stdio.h> #include<math.h> typedef struct pair{ int x; int y; }pair; float fun(pair a,pair b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main(){ pair k[3]; int i;float a,b,c,p,s; for(i=0;i<3;i++) scanf("%d%d",&k[i].x,&k[i].y); a=fun(k[0],k[1]); b=fun(k[1],k[2]); c=fun(k[0],k[2]); p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c)); printf("%.2f",s); return 0; }
  • 5楼网友:請叫我丶偏執狂
  • 2021-04-27 12:29
#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; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息