2012-08-27 88 views
1

我收到上述錯誤。我在C編碼並使用標準的gcc編譯器(命令窗口)。這是我的代碼中有問題的部分。我沒有看到任何錯誤,也許是我的眼睛累了看同樣的事情預計';'之前的'{'令牌C代碼

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; 
      } 
     } 

    } 
} 
+1

'else'不採用條件語句,你可能意味着'else if(r1 birryree

+1

我修復了你的格式。但爲了將來的參考,儘量避免在代碼塊中使用製表符,因爲它們可能會弄亂格式。 – Mysticial

回答

3

你並不需要一個條件else。它在所有if的條件失敗時執行。刪除的東西,如果括號,你會很好:

else { 
... 
3

... else if (r1 < r2) ...將是該語句的條件形式,但作爲拳頭if有一個互補的條件(r1 >= r2)簡單else做它。

所以,你將有:

if (r1 >= r2){ 
    leader-> price1 = p_max; 
} 
else { 
    leader -> price1 = p_max; 
... 
3

嘗試改變

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

else if (r1 < r2) { //try to route s2 
相關問題