输入长方形两个顶点,求长方形中点
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int x;
int y;
}pt;
typedef struct {
pt pt1;
pt pt2;
}rect;
rect* read(void) {
rect* p_r = (rect*)malloc(sizeof(rect));
if (p_r) {
printf("请输入长方形的位置:");
scanf("%d %d %d %d", &p_r->pt1.x, &p_r->pt1.y, &p_r->pt2.x, &p_r->pt2.y);
}
return p_r;
}
pt* midpt(const rect* p_r) {
pt* p_pt = (pt*)malloc(sizeof(pt));
if (p_pt) {
p_pt->x = (p_r->pt1.x + p_r->pt2.x) / 2;
p_pt->y = (p_r->pt1.y + p_r->pt2.y) / 2;
}
return p_pt;
}
int main() {
rect* p_r = read();
pt* p_mid = midpt(p_r);
if (!p_r)
{
return 0;
}
printf("长方形位置是(%d,%d)到(%d,%d)\n", p_r->pt1.x, p_r->pt1.y, p_r -> pt2.x, p_r->pt2.y);
if (!p_mid)
{
free(p_r);
p_r = NULL;
return 0;
}
printf("中点是(%d,%d)\n", p_mid->x, p_mid->y);
free(p_mid);
p_mid = NULL;
free(p_r);
p_r = NULL;
return 0;
}