我收到了上述错误。我在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;
}
}
}
}
答案 0 :(得分:3)
... else if (r1 < r2) ...
将是声明的条件形式,但由于if
具有互补条件(r1 >= r2)
,因此简单else
就可以了。
所以你会:
if (r1 >= r2){
leader-> price1 = p_max;
}
else {
leader -> price1 = p_max;
...
答案 1 :(得分:3)
尝试更改
else (r1 < r2) { //try to route s2
到
else if (r1 < r2) { //try to route s2
答案 2 :(得分:3)
您不需要else
的条件。当所有if
的条件都失败时执行它。如果括号删除东西,你会没事的。:
else {
...