Переменная не хранится в соответствии с запросом - PullRequest
1 голос
/ 21 августа 2011

Вот мой код:

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 in pointsToCalculate[i] is 1 or 2 , depends on how many times I have ran through the loop and it also shows that = i; is also 1 or 2` в зависимости на петле.

Кто-нибудь знает, почему это? Это назначение, которое должно состояться через несколько часов, и я очень долго над ним работаю. Тем не менее, просто не могу понять это.

Спасибо, Brandon

1 Ответ

1 голос
/ 21 августа 2011

, если distance <= threshold_distance, i не будет увеличиваться, а цикл while будет зацикливаться

...