【18年扬大真题】定义一个Point类,要求如下所述。
(1)用构造函数初始化Point类的对象
(2)定义函数Distance,计算平面上两点之间的距离
#include<stdio.h>
#include<math.h>
typedef struct {
double x;
double y;
}Point;
double Distance(double x1,double y1, double x2, double y2) {
double x_2 = pow(x1 - x2, 2);//ps:pow(x,n);计算x的n次方,需要math.h头文件
double y_2 = pow(y1 - y2, 2);
return pow(x_2+y_2,0.5);
}
int main() {
printf("下面进行你想计算的两点距离:\n");
Point p1;
Point p2;
printf("请输入第一个点的横坐标和纵坐标:");
scanf("%lf", &p1.x);
scanf("%lf", &p1.y);
printf("请输入第二个点的横坐标和纵坐标:");
scanf("%lf", &p2.x);
scanf("%lf", &p2.y);
printf("两点的距离为:%f", Distance(p1.x,p1.y,p2.x,p2.y));
return 0;
}
测试用例: