我昨天发布了这个问题,但这是我的第一篇文章,所以我的问题不清楚。我有一个作业,输出在代码下方。我坚持创建一个函数来计算最近的距离。代码是:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
struct obstacle {
string carname;
float x, y;
float distance;
obstacle *next;
};
obstacle *head = nullptr;
float DistanceToOrigin (float x, float y)
{
float distance;
distance = sqrt((x*x) + (y*y));
return distance;
}
float CalcNearestPoint(obstacle points[], obstacle point, int n)
{
obstacle temp;
for (int i=0;i<n;i++){
if(points[i].distance>points[i-1].distance)
{
temp.distance = points[i].distance;
points[i].distance = points[i-1].distance ;
points[i-1].distance = temp.distance;
}
}
DistanceToOrigin(points[0].x,points[0].y);
}
void insertObstacle (string name, float a, float b)
{
obstacle *newObstacle = new obstacle;
newObstacle->carname = name;
newObstacle->x = a;
newObstacle->y = b;
newObstacle->next = head;
head = newObstacle;
}
int main()
{
int x, y;
string name;
while (name != "0"){
{
cout << "string describing obstacle (""0"" for end of input):";
cin >>name;
cout << "\nx and y coordinate: ";
cin >> x >> y;
insertObstacle(name, x, y);
}
}
}
任务是:
编写一个函数,该函数将指向障碍物/最近点的指针返回为障碍物/点的列表作为第一参数,并将障碍物/点作为第二参数。请注意,障碍物/点不会返回自身(距离0),并且必须至少有两个障碍物/点,否则零指针应返回nullptr。 /
And the required output:
string describing obstacle ("end" for end of input): A
x and y coordinate: 0 1
string describing obstacle ("end" for end of input): X
x and y coordinate: 1 1
string describing obstacle ("end" for end of input): E
x and y coordinate: 0 3
string describing obstacle ("end" for end of input): K
x and y coordinate: -1 4
string describing obstacle ("end" for end of input): W
x and y coordinate: 0 10
obstacle obstacle (end of end): end
obstacle A: (0.00, 1.00), distance: 1.00m, nearest to this: X
obstacle X: (1.00, 1.00), distance: 1.41m, nearest to this: A
obstacle E: (0.00, 3.00), distance: 3.00m, nearest to this: K
obstacle K: (-1.00, 4.00), distance: 4.12m, nearest to this: E
obstacle W: (0.00, 10.00), distance: 10.00m, nearest to this: K
delete: AX
根据您在此处显示的内容,您似乎在尝试创建链接列表,并被要求在链接列表中查找障碍物,该障碍物是您当前代码(在插入障碍物之内)永不链接的最短距离障碍物进入一个列表,它仅将下一个对象链接到头部,其他障碍物也消失了,因为不再指向它们,因此您再也没有列表可以浏览了。您可能想阅读linked lists.
接下来,您的CalcNearstPoint函数基于您使用数组的假设,但是正如我之前所说,它看起来像您尝试使用链接列表,在这种情况下,您需要对其进行修改,以使用while循环逐步浏览链接列表这样检查下一个指针是否不为空]
obstacle *temp = head;
while(temp->next != nullptr)
{
...
temp = temp->next;
}
因此,总而言之,您首先需要修复您的insertOstacle函数,然后您需要编写CalcNearstPoint才能逐步浏览链接列表。
Key Ponts:1)您的CalcNearstPoint
函数接受array
而不是linked_list
的参数(由@That_Linux_Guy指出)
2)函数point
的第二个参数CalcNearstPoint
从未使用过..也许您正在尝试计算point
与一组障碍物[ C0]
points[]
如果d0为零,则在障碍物组
bool is_zero(double d) { static const double epsilon = 1e-6; return ((d >= -epsilon) && (d <= epsilon)); } double distance(obstacle *A, obstacle *B) { double x = (B->x - A->x); double y = (B->y - A->y); return std::sqrt((x * x) && (y * y)); } obstacle* CalcNearestPoint(linked_list<obstacle> points, obstacle point) { obstacle ptr = points.BEGIN, *tmp = nullptr; double d0; for (; (ptr != nullptr); ptr = ptr->next) { double d = distance(ptr, point); if ((tmp == nullptr) || (d < d0)) { d0 = d; tmp = ptr; } } return (((tmp != nullptr) && (is_zero(d0) == false)) ? tmp : nullptr); }
中定义point
3)您应该在points
上阅读,这是一个简单的实现
linked-list