liyanzi
一般會員
![](./myimg/board/mystar_empty.gif)
![](images/icon_photo_none.gif) 發表:51 回覆:45 積分:19 註冊:2005-01-24
發送簡訊給我
|
#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
站務副站長
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](http://profile.blog.yam.com/d/d/ddy1280/index.jpg) 發表:262 回覆:2105 積分:1169 註冊:2002-07-13
發送簡訊給我
|
shortestpath(1,6);
我測試一下,將這行拿掉後,編譯成功也能執行無誤
大致看起來沒什麼問題,你試試看
如果還有問題的話…是否也將 shortestpath() 一併附上 ![](http://delphi.ktop.com.tw/loadfile.php?TOPICID=8147403&CC=182217)
|
liyanzi
一般會員
![](./myimg/board/mystar_empty.gif)
![](images/icon_photo_none.gif) 發表:51 回覆:45 積分:19 註冊:2005-01-24
發送簡訊給我
|
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
站務副站長
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](http://profile.blog.yam.com/d/d/ddy1280/index.jpg) 發表:262 回覆:2105 積分:1169 註冊:2002-07-13
發送簡訊給我
|
試試看,應該是可以,我什麼都沒做,只是把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;
} ![](http://delphi.ktop.com.tw/loadfile.php?TOPICID=8147403&CC=182217)
|