结构体、共用体和枚举类型(一)

定义描述三维坐标点(x,y,z)的结构体类型变量,完成坐标点的输入和输出,并求出两点之间的距离

 

代码如下:

#include <iostream>

#include <cmath>

#define N 2

using namespace std;

struct Coordinate{

       double x,y,z;

}Coe[N];

void Input()

{

       cout<<"输入点的坐标(x,y,z):(分别输入x、y、z,用空格隔开,不需要括号)"<<endl;

       for(int i=0; i<N; i++)

          cin>>Coe[i].x>>Coe[i].y>>Coe[i].z;

}

void Output()

{

       cout<<"输入点的坐标为:"<<endl;

       for(int i=0; i<N; i++)

          cout<<i+1<<"("<<Coe[i].x<<","<<Coe[i].y<<","<<Coe[i].z<<")"<<endl;

}

int main(){

       double a,b,c,distance;

       Input();

       cout<<endl;

       Output();

       a=(Coe[1].x-Coe[0].x);

       b=(Coe[1].y-Coe[0].y);

       c=(Coe[1].z-Coe[0].z);

       distance=sqrt(a*a+b*b+c*c);

       cout<<endl;

       cout<<"两坐标点的距离为:"<<distance;

       return 0;

}

 

posted @ 2018-12-01 18:38  .Daylight  阅读(547)  评论(0编辑  收藏  举报