变量没有按要求存储

Variable not storing as requested

本文关键字:存储 变量      更新时间:2023-10-16

下面是我的代码:

point * findLongPaths(point * points, double threshold_distance) {
    unsigned int i = 0;
    int locationToStore = 0;
    int pointsAboveThreshold = countPointsAboveThreshold(points, threshold_distance);
    //int totalPoint = totalPoints(points);
    point * pointsByThreshold = new point[pointsAboveThreshold];
    pointValues * pointsToCalculate = new pointValues[pointsAboveThreshold];
    //pointValues pointsToCalculate[pointsAboveThreshold];
    //point orderedPoints[pointsAboveThreshold];
    while (points[i].end != true && i < pointsAboveThreshold) {
        point pointOne = points[i];
        point pointTwo = points[i + 1];
        //Check to see if the distance is greater than the threshold, if it is store in an array of pointValues
        double distance = distanceBetweenTwoPoints(pointOne, pointTwo);
        if (distance > threshold_distance) {
            pointsToCalculate[i].originalLocation = i;
            pointsToCalculate[i].distance = distance;
            pointsToCalculate[i].final = pointTwo;
            pointsToCalculate[i].stored = false;
            //If the final point has been calculated, break the loop
            if (pointTwo.end == true) {
                pointsToCalculate[i].end = true;
                break;
            } else {
                pointsToCalculate[i].end = false;
                i++;
                continue;
            }
        } 
    }
    if (points[0].end == true && pointsAboveThreshold == 0) {
        point emptyPoint;
        emptyPoint.x = 0.0;
        emptyPoint.y = 0.0;
        emptyPoint.end = true;
        pointsByThreshold[0] = emptyPoint;
        return pointsByThreshold;
    }
    //Find the point with the lowest distance
    i = 1;
    //double lowest = 0.0;
    //EDITED
    pointValues pointWithLowest;
    pointWithLowest = pointsToCalculate[0];
    while (pointsToCalculate[i].end != true) {
        for (int j = 0; pointsToCalculate[j].end != true; j++) {
            if (pointsToCalculate[j].stored == true) {
                j++;
                continue;
            } else {
                if (pointsToCalculate[j].distance < pointWithLowest.distance) {
                    pointWithLowest = pointsToCalculate[j];
                    j++;
                    continue;
                } else if (pointsToCalculate[j].distance == pointWithLowest.distance) {
                    if (pointWithLowest.originalLocation > pointsToCalculate[j].originalLocation) {
                        pointWithLowest = pointsToCalculate[j];
                        j++;
                        continue;
                    }
                } else {
                    pointWithLowest.stored = true;
                    pointsByThreshold[locationToStore] = pointWithLowest.final;
                    locationToStore++;
                    break;
                }
            }
        }
        i++;
    }
    delete[] pointsToCalculate;
    return pointsByThreshold;
}

由于一些奇怪的原因当我将i存储在pointsToCalculate[i].originalLocation = i;行时,它总是存储为0。我在它上面运行了断点,它显示i的值在while循环中增加了,但它仍然将originalLocation存储为0。当我在运行时检查值时,显示pointsToCalculate[i] is 1 or 2 , depends on how many times I have ran through the loop and it also shows that = I中的i;is also 1 or 2 '取决于循环。

有谁知道这是为什么吗?这是几个小时后要交的作业,我已经做了很长时间了。但我还是想不明白。

谢谢,布兰登

如果distance <= threshold_distance, i不会增加,while循环将永远循环