1
// Construct an array of length MAX_POINTS.
// Populate the array with given points and initialize the number points being handled.
// Print the given points for the user.
struct Point points[MAX_POINTS];
int numPoints = readPoints(points);
printf("Set of points: \n");
displayPoints(points, numPoints);

// Construct an array to represent the hull with max possible points (numPoints).
struct Point hull[numPoints];
struct Point pointOnHull = leftmostPoint(points, numPoints);
struct Point endPoint;
int e = 0;

// Perform Jarvis March.
do {
    hull[e] = pointOnHull;
    endPoint = points[0];
    for ( int i = 1; i < numPoints; i++ )
        if ( (endPoint == pointOnHull) || (ccw(hull[e], endPoint, points[i]) > 0) )
            endPoint = points[i];
    ++e;
} while ( endPoint != hull[0] );        // we've looped back to the beginning.

// Print the mapped hull points.
printf("Convex hull: \n");
displayPoints(hull, numPoints);

这是我的 Jarvis March(礼品包装)程序的代码,用于解决凸包问题。GCC 在我的 struct Point 比较(endPoint == pointOnHullendPoint != hull[0]). 我是 C 的新手,这是我正在做的第一个使用该语言的项目之一。谁能帮我看看我在哪里搞砸了?

具体错误:

jarvis.c:31:19:错误:二进制 == 的无效操作数(具有“结构点”和“结构点”)

jarvis.c:34:21:错误:二进制操作数无效!=(具有“结构点”和“结构点”)

4

1 回答 1

2

中的结构没有比较运算符C。您必须定义自己的比较函数并使用它来代替==. 尝试这样的事情:

bool equalPoints(const struct Point p1, const struct Point p2) {
    return (p1.x == p2.x) && (p1.y == p2.y);
}

if (equalPoints(endPoint, pointOnHull)) { /* then code here */ }

或者如果您的结构更复杂(但可能并非如此):

bool equalPoints(const struct Point *p1, const struct Point *p2) {
    /* 
     * In case your structure is more complex here goes
     * more complex comparison code
     */
    return (p1->x == p2->x) && (p1->y == p2->y);
}

if (equalPoints(&endPoint, &pointOnHull) { /* then code here */ }

后者不会复制整个Point结构以将其传递给equalPoints,而是会传递指针(引用类型)。const很重要,因为当您只想比较它们时,它不会让您意外修改点。

于 2013-10-06T20:20:18.277 回答