变量不按要求存储

时间:2011-08-21 02:29:31

标签: c++ loops int increment

这是我的代码:

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.当我在运行时检查值时,它显示i中的pointsToCalculate[i] is 1 or 2 {{ 1}} = i; , depends on how many times I have ran through the loop and it also shows that 1 is also 2`取决于循环。

任何人都知道这是为什么?这是在几个小时内完成的任务,我已经在很长一段时间内完成了这项任务。但仍然无法理解。

谢谢, 布兰登

1 个答案:

答案 0 :(得分:1)

如果distance <= threshold_distance,则i不会递增,而while循环将循环前进