【问题标题】:Variable not storing as requested变量未按要求存储
【发布时间】:2011-08-21 02:29:30
【问题描述】:

这是我的代码:

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] is1or2, depends on how many times I have ran through the loop and it also shows that= i;is also1or2` 取决于在循环中。

有人知道这是为什么吗?这是一个几个小时后到期的任务,我已经为此工作了很长时间。不过还是想不通。

谢谢, 布兰登

【问题讨论】:

  • 请不要发布带有注释部分的代码
  • pointTwo.end 是否曾经等于 false?
  • 抱歉,Mitch 我删除了大部分内容。是的,它用于赋值,我们可以保证 pointTwo.end 总是等于 false ,除非它是数组中的最后一个点。通过根据讲师将要测试的代码检查它,我如何设置分数的默认值?也许这会解决问题。他用这条线测试point p[3] = {{0,0}, {0,3}, {0,1, true}}; point longest[2] = {{0,1}, {0,3,true}}; BOOST_CHECK_EQUAL(calculateLineLength(p), 5);
  • 布兰登,您在评论中给出的代码 sn-p 的问题是它不起作用。假设你的点结构有三个属性{num, num, boolean},你的值{{0,0}, {0,3}.... {0,1} 不会飞,因为你需要{{0,0,false}, {0,3,false} 等来使声明有效。
  • @Brandon:在你的例子中,我看到了一些正确的。你确定它被设置为假吗?

标签: c++ loops int increment


【解决方案1】:

如果distance &lt;= threshold_distancei 不会递增,while 循环会一直循环

【讨论】:

    猜你喜欢
    • 2016-03-30
    • 2023-03-22
    • 2020-08-24
    • 2017-03-13
    • 2020-11-28
    • 2018-04-12
    • 2017-09-30
    • 2011-09-29
    • 2014-02-07
    相关资源
    最近更新 更多