1

我正在做一个任务,我必须为用户创建一个程序来输入矩形的坐标。

该程序旨在成为结构中的结构。

如果无效,我必须输出一条错误消息并让用户无限期地重试,直到用户正确为止。

程序会反复询问用户一个点的坐标,当用户分别为 x 和 y 输入 0 和 0 时,程序将退出。

程序必须说明点是在矩形的内部还是外部,并打印出内部的点。我还需要弄清楚将 main 函数放在哪里,以及在其中放置什么。谢谢。

这是我的代码:

#include <stdio.h>
typedef struct
{
int x;
int y;
} point_t;

typedef struct
{
point_t upper_left;
point_t lower_right;
} rectangle_t;

int is_inside (point_t* pPoint, rectangle_t* pRect)
{
return ((pPoint->x >= pRect->upper_left.x ) &&
(pPoint->x <= pRect->lower_right.x) &&
(pPoint->y >= pRect->upper_left.y ) &&
(pPoint->y <= pRect->lower_right.y));

}

point_t get_point(char* prompt)
{
point_t pt;
printf("Given a rectangle with a side parallel to the x axis and a series of points on          the xy plane this program will say where each point lies in relation to the rectangle. It   considers a point on the boundary of the rectangle to be inside the rectangle\n");
printf ("Enter coordinates for the upper left corner\n");
printf ("X: ");
scanf ("%d", &pt.x);
printf ("Y: ");
scanf ("%d", &pt.y);

return pt;
}

rectangle_t get_rect(char* prompt)
{
rectangle_t rect;
printf (prompt);
rect.upper_left = get_point("Upper left corner: \n");
rect.lower_right = get_point("Lower right corner: \n");

return rect;
}
int main(){
/* calls goes here */
return 0;
}

编辑:我添加了 main,但我不知道如何调用函数。

4

1 回答 1

0

我建议你修改一下你的风格,并应用这样的东西:

  1. 在 main 之前声明函数的原型。
  2. 我认为您不需要为大多数函数使用指针(引用传递函数)is_inside,因为它们不会修改值。
  3. 将矩形点声明为点类型(因为实际上您将它们用作点)。

要进行调用,您只需在 main.xml 中填充函数的参数。例子:

/* Headers, libs, definitions, etc must be here */
/* Prototypes must be here */

int main (void)
{
    /* Variables declarations */
    int test_point;
    point_t point;
    rectanle_t rectangle;

    /* Functions calls... */
    /* holder = function_name( argument1, argument2 ); */

    test_point = is_inside( &point, &rectangle ); 


}
/*Function Declarations must be here*/
于 2012-11-06T22:00:47.157 回答