c++计算输入和保存点的结构与原点之间的距离,根据距离打印出列表

C++ calculate distance between structure of inputed and saved points and the origin, print out list according on distance

本文关键字:距离 列表 打印 之间 输入 计算 保存 c++ 结构 原点      更新时间:2023-10-16

在本任务中,障碍物的简化版本应由放置在二维局部笛卡尔坐标系原点的虚拟传感器系统检测到的点来建模。车辆前方任意多的障碍物/点都可以存储在一个列表中。该列表将按照到原点的欧氏距离排序(参见下面的示例)。(提示:写一个函数,计算坐标为(x1,y1)和(x2,y2)的两个障碍物/点P1和P2的欧氏距离d,公式为d=√(x1−x2)2+(y1−y2)2为了简单地识别它们,每个障碍/点都应该存储一个字符串("a","B",…)在上面的例子中,距离和它的坐标(xi,yi)。(提示:用这四个数据作为结构的组件/变量,给出结构的类型定义。)在函数main的循环中,任意许多这样的障碍/点应该可以输入并存储在列表中。然后输出按距离排序的列表。除了字符串之外,每个输出中的距离和坐标,以及离它最近的障碍物/点的字符串,都需要计算并额外输出(见下面的示例)。(提示:编写一个进一步的函数,将所有障碍/点的列表作为第一个参数,一个障碍/点作为第二个参数,计算离它最近的其他障碍/点,并将其作为指针返回。注意障碍物/点不会返回并输出距离0,并且至少需要存在两个障碍物/点;否则返回空指针nullptr。)

我能够创建结构函数,距离函数和列表,不幸的是,我不能对它进行排序,并使用函数距离来计算实际距离。

希望有人能帮助我!

#include<iostream>
#include<cmath>
using namespace std;
double distanceCalculate(double x1, double x2, double y1, double y2)
{
    double x = x1-x2;
    double y = y1-y2;
    double dist;
    dist = pow(x, 2) + pow(y , 2);
    dist = sqrt(dist);
    return dist;
}
struct point
{
    char name [1];
    double x, y, origin_distance ;
    struct point *next;
};
int main(void)
{
    int choice;
    struct point *head = nullptr, *newElement, *p ;
    do
    {
        cout << "0 end"<< endl;
        cout <<"1 input point"<< endl;
        cout <<"2 print list of points" << endl;
        cin >> choice;
        switch(choice)
        {
        case 1:
            newElement = new point;
            if (newElement == nullptr)
                cerr << "Not enough free memory " << endl;
            else
            {
                cout<< "Please enter point's name: ";
                cin>> newElement ->name;
                cout << "Please input value x:";
                cin >> newElement->x;
                cout <<"Please input value y:";
                cin >> newElement->y;
                newElement ->next = head;
                head = newElement;
            }
            break;
        case 2:
            cout<< "Print list of points"<< endl;
            cout<< "name.t x.t y.t" << endl;
            p=head;
            while (p!= nullptr)
            {
                cout<< p->name<<"t"<< p->x<<"t" << p->y << "t" << endl;
                p=p->next;
            }
            break;

        }
    } while (choice !=0);
    return 0;
}

我注意到的第一件事是您混合了点和(链接)列表节点的定义。更典型的实现是定义两种类型——一种用于点,另一种用于列表节点。比如:

typedef struct point
{
 double x, y;
};
typedef struct listNode
{
  point data;
  listNode *next;
};

然而,阅读你的问题——似乎没有定义一个能够表示列表的类型是你的任务的一部分。因此,您不妨将其留给std库。我已经这样做了,使用std::vector作为容器来保存点列表。

现在你不仅需要自己定义一个数据类型来保存2d点,你还可以得到简单的排序,基本上是免费的。定义一个函数返回true或false,取决于一个元素是否应该出现在下一个元素之前。

你的距离函数看起来没有什么问题,尽管我避免在平方时使用pow函数-我只使用x*x + y*y

使用std::vector类型的另一个优点是,您可以使用标准数组表示法访问它包含的元素。考虑到这一点,要显示离原点最近和最远的点,您可以对列表进行排序,然后简单地显示pointList[0]pointList[numElements-1]

这里是一个粗略的实现:

#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
typedef struct vec2_t
{
    double x;
    double y;
} *pVec2;
typedef std::vector<vec2_t> vecVectors;
typedef vecVectors::iterator vecVectorsIter;
double distBetween(vec2_t &p1, vec2_t &p2)
{
    double dx = p1.x - p2.x, dy = p1.y - p2.y;
    return sqrt( dx*dx + dy*dy);
}
bool orderPointAsc(vec2_t &p1, vec2_t &p2)
{
    vec2_t origin = {0.0,0.0};
    return (distBetween(origin,p1) < distBetween(origin,p2));
}
int main()
{
    vecVectors pointList;
    vec2_t origin = {0.0, 0.0};
    int i, n=10;
    vec2_t tmp;
    for (i=0; i<n; i++)
    {
        tmp.x = 100 * (rand() % 0xFFFF) / 65535.0;
        tmp.y = 100 * (rand() % 0xFFFF) / 65535.0;
        pointList.push_back(tmp);
        printf("%d. - <%5.02f, %5.02f> - dist from origin: %0.02fn", i, tmp.x, tmp.y, distBetween(origin, tmp));
    }
    printf("Sorting...n");
    std::sort(pointList.begin(), pointList.end(), orderPointAsc);
    for (vecVectorsIter iter = pointList.begin(); iter!=pointList.end(); iter++)
    {
        printf("<%5.02f, %5.02f> - %5.02fn", (*iter).x, (*iter).y, distBetween(origin, (*iter)) );
    }
    return 0;
}

的示例输出
0. - < 0.06, 28.18> - dist from origin: 28.18
1. - < 9.67, 40.44> - dist from origin: 41.58
2. - <29.25, 23.99> - dist from origin: 37.83
3. - <17.51, 44.80> - dist from origin: 48.10
4. - <41.14, 37.33> - dist from origin: 55.55
5. - < 8.71, 42.95> - dist from origin: 43.82
6. - <35.52, 25.68> - dist from origin: 43.83
7. - <15.20,  0.75> - dist from origin: 15.22
8. - < 4.57, 18.22> - dist from origin: 18.79
9. - < 7.37,  8.29> - dist from origin: 11.09
Sorting...
< 7.37,  8.29> - 11.09
<15.20,  0.75> - 15.22
< 4.57, 18.22> - 18.79
< 0.06, 28.18> - 28.18
<29.25, 23.99> - 37.83
< 9.67, 40.44> - 41.58
< 8.71, 42.95> - 43.82
<35.52, 25.68> - 43.83
<17.51, 44.80> - 48.10
<41.14, 37.33> - 55.55