用C++设计点类Point,输出点的信息,输出点的个数,计算点到原点的距离,两点的中点
时间: 2023-05-28 14:05:00 浏览: 123
#include <stdio.h>
#include <math.h>
typedef struct {
double x;
double y;
} Point;
int main() {
Point p1 = {3, 4};
Point p2 = {6, 8};
Point p3 = {-2, 1};
Point points[] = {p1, p2, p3};
int num_points = sizeof(points) / sizeof(Point);
printf("Point 1: (%.2f, %.2f)\n", p1.x, p1.y);
printf("Point 2: (%.2f, %.2f)\n", p2.x, p2.y);
printf("Point 3: (%.2f, %.2f)\n", p3.x, p3.y);
printf("Number of points: %d\n", num_points);
double dist_p1 = sqrt(p1.x * p1.x + p1.y * p1.y);
double dist_p2 = sqrt(p2.x * p2.x + p2.y * p2.y);
double dist_p3 = sqrt(p3.x * p3.x + p3.y * p3.y);
printf("Distance from point 1 to origin: %.2f\n", dist_p1);
printf("Distance from point 2 to origin: %.2f\n", dist_p2);
printf("Distance from point 3 to origin: %.2f\n", dist_p3);
double mid_x = (p1.x + p2.x) / 2;
double mid_y = (p1.y + p2.y) / 2;
Point mid_point = {mid_x, mid_y};
printf("Midpoint of point 1 and point 2: (%.2f, %.2f)\n", mid_point.x, mid_point.y);
return 0;
}
阅读全文