全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:813
推到 Plurk!
推到 Facebook!

型別轉換的問題

尚未結案
liyanzi
一般會員


發表:51
回覆:45
積分:19
註冊:2005-01-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-01-25 12:50:39 IP:218.165.xxx.xxx 未訂閱
#pragma hdrstop
#include
#define MAXLEN 1000 
int cost[7][7]; 
int dist[7]; 
//-------------------------------------------------------------------    #pragma argsused    void creategraph(int *node,int num)
{
   int from; 
   int to; 
   int i;       for ( i = 0; i < num; i   ) 
   {
     from = node[i*3]; 
     to = node[i*3 1]; 
     cost[from][to] = node[i*3 2];
   }
}    int main(int argc, char* argv[])
{
  int node[7][3] = { {1, 2, 35}, 
     {2, 3, 45},
     {2, 4, 30},
     {3, 5, 25},
     {4, 5, 45},
     {4, 6, 130},
     {5, 6, 100} };
   int i,j;       for ( i = 1; i <= 6; i   )
     for ( j = 1; j <= 6; j   )
     cost[i][j] = MAXLEN; 
       creategraph(node,7); 
       printf("加權圖形的鄰接陣列內容:\n");
   for ( i = 1; i <= 6; i   )
   {
     for ( j = 1; j <= 6; j   )
     printf(" M ",cost[i][j]); 
     printf("\n"); }
   printf("\n從頂點1到各頂點最近距離計算過程:\n");
   shortestpath(1,6); 
     return 0;
}    這兩行~~
creategraph(node,7);
return 0;
一直出現以下錯的訊息:
[C   Error] Unit1.cpp(88): E2034 Cannot convert 'int ( *)[3]' to 'int *'    [C   Error] Unit1.cpp(88): E2342 Type mismatch in parameter 'node' (wanted 'int *', got 'int ( *)[3]')    [C   Error] Unit1.cpp(99): E2188 Expression syntax    [C   Warning] Unit1.cpp(100): W8004 'node' is assigned a value that is never used    拜託各位大大~~~會的話請解答一下~~~~拜託拜託


        
ddy
站務副站長


發表:262
回覆:2105
積分:1169
註冊:2002-07-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-01-25 13:44:16 IP:202.145.xxx.xxx 未訂閱
   shortestpath(1,6);
我測試一下,將這行拿掉後,編譯成功也能執行無誤 大致看起來沒什麼問題,你試試看 如果還有問題的話…是否也將 shortestpath() 一併附上
liyanzi
一般會員


發表:51
回覆:45
積分:19
註冊:2005-01-24

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-01-25 14:04:50 IP:218.165.xxx.xxx 未訂閱
void shortestpath(int begin,int num)
{
   int selected[7];              
   int min;                       
   int s;                       
   int i,j;       for ( i = 2; i <= num; i   )  
   {
      selected[i] = 0;           
      dist[i] = cost[begin][i];   
   }
   selected[begin] = 1;          
   dist[begin] = 0;             
   printf("頂點1    2     3     4     5     6\n");
   for ( j = 1; j <= num; j   )   
         printf(" M ",dist[j]); 
   printf("\n");                 
   for ( i = 1; i <= num - 1; i   )
   {
      min = MAXLEN;              
      for ( j = 1; j <= num; j   )
        
         if ( min > dist[j] && selected[j] == 0 )
         {
            s = j;                
            min = dist[j];       
         }
      selected[s] = 1;           
      
      for ( j = 1; j <= num; j   )
      {
         if ( selected[j] == 0 && 
              dist[s]   cost[s][j] < dist[j] )
            
            dist[j] = dist[s]   cost[s][j];
         printf(" M ",dist[j]); 
      }
      printf("\n");                 }
}
謝謝ddy這位大大ㄚ~~~ 大致上是沒有問題了`~不過return 0 這行`~在執行時會變成綠色的ㄟ 這又是哪裡的問題呢? 這是shortestpath()這部分~~~感謝解答~~~^^ 發表人 - taishyang 於 2005/01/25 15:07:02
ddy
站務副站長


發表:262
回覆:2105
積分:1169
註冊:2002-07-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-01-25 18:10:18 IP:202.145.xxx.xxx 未訂閱
試試看,應該是可以,我什麼都沒做,只是把code重新排過 ps:如果要張貼程式碼,記得要加上
 
#pragma hdrstop
#include
#define MAXLEN 1000 
int cost[7][7]; 
int dist[7]; 
//-------------------------------------------------------------------
 
#pragma argsused
 
void creategraph(int *node,int num)
{
   int from; 
   int to; 
   int i;
 
   for ( i = 0; i < num; i++ ) 
   {
        from = node[i*3]; 
        to = node[i*3+1]; 
        cost[from][to] = node[i*3+2];
   }
}
     void shortestpath(int begin,int num)
{
    int selected[7];              
    int min;                       
    int s;                       
    int i,j;
    
    for ( i = 2; i <= num; i++ )  
    {
        selected[i] = 0;           
        dist[i] = cost[begin][i];   
    }
    selected[begin] = 1;          
    dist[begin] = 0;             
    printf("頂點1    2     3     4     5     6\n");
    
    for ( j = 1; j <= num; j++ )   
        printf(" %4d ",dist[j]); 
        
    printf("\n");                 
    
    for ( i = 1; i <= num - 1; i++ )
    {
        min = MAXLEN;              
        for ( j = 1; j <= num; j++ )
        
        if ( min > dist[j] && selected[j] == 0 )
        {
            s = j;                
            min = dist[j];       
        }
        selected[s] = 1;           
        
        for ( j = 1; j <= num; j++ )
        {
            if (selected[j] == 0 && dist[s] + cost[s][j] < dist[j])
                dist[j] = dist[s] + cost[s][j];
            printf(" %4d ",dist[j]); 
        }
        printf("\n");                 
    }
}
     int main(int argc, char* argv[])
{
    int i,j;
    int node[7][3] = 
                    {{1, 2, 35}, 
                     {2, 3, 45},
                     {2, 4, 30},
                     {3, 5, 25},
                     {4, 5, 45},
                     {4, 6, 130},
                     {5, 6, 100}};
 
    for ( i = 1; i <= 6; i++ )
        for ( j = 1; j <= 6; j++ )
            cost[i][j] = MAXLEN; 
 
 
    creategraph(node,7); 
    
    
    printf("加權圖形的鄰接陣列內容:\n");
    for ( i = 1; i <= 6; i++ )
    {
        for ( j = 1; j <= 6; j++ )
            printf(" %4d ",cost[i][j]); 
        printf("\n"); 
    }
    printf("\n從頂點1到各頂點最近距離計算過程:\n");
    shortestpath(1,6); 
    
    getch();
    return 0;
}    
系統時間:2024-11-23 10:32:10
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!