gpt4 book ai didi

c - 我在某个时候失去了我的对象的类型

转载 作者:太空宇宙 更新时间:2023-11-04 02:54:22 25 4
gpt4 key购买 nike

// 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 在我的两个结构点比较 (endPoint == pointOnHullendPoint != hull[0]) 上都出现了错误。我是 C 语言的新手,这是我为玩弄这门语言所做的首批项目之一。谁能帮我看看我哪里搞砸了?

具体错误:

jarvis.c:31:19: error: invalid operands to binary == (have 'struct Point' and 'struct Point')

jarvis.c:34:21: error: invalid operands to binary != (have 'struct Point' and 'struct Point')

最佳答案

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 很重要,因为它不会让您在只想比较点时不小心修改点。

关于c - 我在某个时候失去了我的对象的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com