1

我收到上述错误。我正在用 C 编码并使用标准 gcc 编译器(命令窗口)。这是我的代码中有问题的部分。我没有看到任何错误,也许我的眼睛已经厌倦了看同一件事

LEADER *call_leader(double st1, double st2, double incentive, double pdis)
{
    LEADER *leader= (LEADER *)malloc(sizeof(LEADER));
    double shortest = MIN(st1, st2) ; //shortest station
    if (shortest == st1 && s1 != 0){ //no congestion
        leader->price1 = p_max;
        leader->price2 = p_max;
    }

    if (shortest == st2 && s2 != 0){ //no congestion
        leader->price1 = p_max;
        leader->price2 = p_max;
    }

    if (shortest == st1 && s1 == 0){ //congestion at station 1
        if (s2 != 0){ // no congestion at station 2, try to route
            leader->price1 = p_max;
            double cost_1 = shortest*pdrive + p_max;
            double p2 = cost1 - st2*pdrive - (sqrt(pow((st2 - st1),2)))*pdis -     incentive;
        if (p2 >= p_min){
            leader->price2 = p2;
        }else{
            leader->price2 = p_min;
        }
    }
    else if (s2 == 0){
        if (r1 >= r2){ // st1 less congestion
            leader-> price1 = p_max;
        }
Problematic Line =>     else (r1 < r2) {   //try to route s2
            leader -> price1 = p_max;
            double cost_1 = shortest*pdrive + p_max;
            double p2 = cost1 - st2*pdrive - (sqrt(pow((st2 - st1),2)))*pdis - incentive;
            if (p2 >= p_min){
                leader->price2 = p2;
            }
            else{
                leader->price2 = p_min;
            }
        } 

    }
}
4

3 回答 3

3

... else if (r1 < r2) ...将是语句的条件形式,但由于拳头if有一个补充条件,所以它(r1 >= r2)很简单。else

所以你将拥有:

if (r1 >= r2){
    leader-> price1 = p_max;
}
else {
    leader -> price1 = p_max;
...
于 2012-08-27T14:11:33.317 回答
3

尝试改变

else (r1 < r2) {   //try to route s2

else if (r1 < r2) {   //try to route s2
于 2012-08-27T14:11:52.677 回答
3

你不需要条件 for else。它在 allif的条件失败时执行。如果括号删除东西,你会没事的。:

else {
...
于 2012-08-27T14:12:06.813 回答