【问题标题】:to write a function that tests whether a Point is in a Rectangle编写一个函数来测试一个点是否在一个矩形中
【发布时间】:2016-01-06 00:51:55
【问题描述】:

问题如下:

编写并测试具有以下功能的程序。

首先,定义一个名为 Point 的新结构化类型,用浮点数表示 x 和 y 值

。另外,定义一个名为 Rectangle 的新结构化类型,它的边平行于 x 轴和 y 轴,允许您使用 bottom_left 和 top_right 点来表示矩形。

接下来编写一个函数,根据传递给函数的 Rectangle 参数计算并返回 Rectangle 的面积。

避免按值传递,确保函数具有按引用传递的行为

确保函数返回适当类型的数据

接下来编写一个函数来测试一个点是否在一个矩形中。此函数应通过引用接收两个参数,即要测试的 Point 和 Rectangle。如果点在矩形内,该函数必须返回整数值 1,否则返回 0。编写一个主函数,用适当的局部变量作为测试数据,然后在上面的两个函数上使用

 #include <stdio.h>

 struct Point
 {
     float x;
     float y;
 };

 struct Rectangle
 {
     struct Point lb;    // left below point
     struct Point ru;    // right upper point
 };

 float getArea(struct Rectangle r)
 {
     return (r.ru.x - r.lb.x)*(r.ru.y - r.lb.y);
 }

 void setValue(struct Point* p, float x, float y)
 {
     p->x = x;
    p->y = y;
 }

 void setValueP(struct Rectangle* r, struct Point* lb, struct Point* ru)
 {
    r->lb = *lb;
     r->ru = *ru;
 }

 void setValueR(struct Rectangle* r, float x1, float y1, float x2, float y2)
 {
     r->lb.x = x1;
     r->lb.y = y1;
     r->ru.x = x2;
     r->ru.y = y2;
 }

 int contains(struct Rectangle r, struct Point p)
 {
     if((p.x > r.lb.x && p.x && p.x < r.ru.x) && (p.y > r.lb.y && p.y && p.y < r.ru.y))
        return 1;
     return 0;
 }

 int main()
 {
     struct Rectangle r;
    setValueR(&r, 1, 2, 6, 8);

     printf("%f\n", getArea(r));

     struct Point p1;
    setValue(&p1, 4, 5);
     struct Point p2;
     setValue(&p2, 4, 1);

     if(contains(r, p1))
         printf("inside the Rectangle\n");
     else
         printf("outside the Rectangle\n"); 

     if(contains(r, p2))
         printf("inside the Rectangle\n");
     else
         printf("outside the Rectangle\n"); 
 }

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整和可验证的示例。
  • 这里不是您完成家庭作业的地方。首先努力找到解决方案。如果你不能,发表你的意见。提问时要更加具体。
  • @Mohammad.L 请不要对我的 cmets 抱有负面的看法。我只是添加它以鼓励您投入更多的研究工作。就这样。 :)
  • 你的代码是C++代码,不是C代码!它将在 C++ 编译器上输出“30.000000 inside the Rectangle outside the Rectangle”
  • @Constantin 我需要它是一个 c 编程代码,你能帮帮我吗?

标签: c arrays pointers struct floating-point


【解决方案1】:

我需要它是一个c编程代码,你能帮帮我吗?

C语言没有'reference'参数,没有'classes',也没有'this'的概念。

代码需要更改为不使用引用而不是类或“this”。

你可以从改变开始:

struct Point
{
    float x;
    float y;
    Point(float x, float y) : x(x), y(y)
{}
Point()
{}
};

到这里:

struct Point
{
    float x;
    float y;
};

然后,在引用“Point”时需要包含“struct”修饰符,因为没有“Point”类但有一个“Point”结构。

所以改变这个:

struct Rectangle
{
    Point lb;       //  left below point
    Point ru;       //  right upper point
    ...

到这里:

struct Rectangle
{
    struct Point lb;       //  left below point
    struct Point ru;       //  right upper point
};

setValue() 函数使用参数引用并使用 C++ 'this' 来指示当前对象:

void setValue(Point& lb, Point& ru)
{
    this->lb = lb;
    this->ru = ru;
}

但是,C 没有“this”或“references”。要为 C 编写它,请使用:

void setValue( struct rectangle *pRect, struct Point *pLB, struct Point *pRU)
{
    pRect->lb.x = pLB->x;
    pRect->lb.y = pLB->y;
    pRect->ru.x = pRU->x;
    pRect->ru.y = pRU->y;
} // end function: setValue

发布的其余代码也有类似的考虑。

【讨论】:

    【解决方案2】:

    确保你编译了 c++,这些错误看起来像是用 c 编译器编译的 c++ 代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多