結構體、共用體和列舉型別(一)
阿新 • • 發佈:2018-12-01
定義描述三維座標點(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;
}