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

定义描述三维坐标点(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;

}

 

转载于:https://www.cnblogs.com/Daylight-Deng/p/10050573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值