以下のプログラムをC言語で書いています。
このプログラムは隣接行列であり、ノード間の接続を設定し、ノード A とノード B の間に接続があるかどうかを確認するようにユーザーに要求します。
# include <stdio.h>
# include <stdlib.h>
#define N 11
#define FALSE 0
#define TRUE 1
typedef int adj_mat[N][N]; /*defining adj_mat */
int path (adj_mat A, int u, int v);
メイン関数は、ユーザーに有向グラフを作成するように求め、次にユーザーに 2 つのノードを入力して、ノード A とノード B の間に接続が存在するかどうかを確認するように求めます。
int main()
{
adj_mat Matrix; /*intializing a new graph adjacency matrix.
on this moment nodes are disconnected every cell contains zero */
int dadnode, sonnode; /*intializing dad node and son node*/
printf("Hello. Enter now the pairs of connected nodes.\n");
printf("enter EOF after finishing of connecting all the nodes\n");
do { /*here user enter the nodes to connect */
printf("Enter the number of first node\n");
scanf("%d", &dadnode);
printf("Enter the number of second node\n");
scanf("%d", &sonnode);
if ((dadnode < sonnode) && (sonnode <= N) && (dadnode > 0)) /*checking if nodes are legal*/
Matrix[dadnode][sonnode] = 1; /*if legal - connect*/
} while ( (dadnode != EOF ) && (sonnode != EOF)); /*until user enter EOF */
printf("Now enter u and v nodes to check if exists way from u node to we node\n");
/*here user enter the nodes to check */
printf("Enter the number of u node\n");
scanf("%d", &dadnode);
printf("Enter the number of v node\n");
scanf("%d", &sonnode);
if ((dadnode < sonnode) && (sonnode <= N) && (dadnode > 0)) /*checking if nodes are legal*/ {
if( path(Matrix,dadnode,sonnode) == TRUE ) /*if exisits way from u to v*/
printf ("Exists way from node u to node v ");
}
else printf ("Not exists way from node u to node v ");
}
次の関数は、u (お父さんノード) から v (息子ノード) までの間に存在する場合に TRUE を返し、それ以外の場合は FALSE を返します。
int path (adj_mat A, int u, int v) {
if (v >= u) /*no sense to check if dad node yonger than son node or dad of himself */
return FALSE;
int nodenum; /*number of node*/
/* "nodenum = v - 1" because node v cannot be son of node >= v */
for(nodenum = v - 1; nodenum > 0; nodenum-- ) {
if (A[nodenum][v] == TRUE) /*dad detected*/
{
if (nodenum == u) {
return TRUE; //complete
} else if (path (A, u, nodenum)) {
return TRUE; //maybe dad is a node that we are looking for (recursion)
}
}
}
return FALSE; /*all parents of v node were cheked and noone of them isnt u node*/
}
最後に、これを gdb (ubuntu) で実行します。
do { /*here user enter the nodes to connect */
printf("Enter the number of first node\n");
scanf("%d", &dadnode);
printf("Enter the number of second node\n");
scanf("%d", &sonnode);
if ((dadnode < sonnode) && (sonnode <= N) && (dadnode > 0)) {/*checking if nodes are legal*/
Matrix[dadnode][sonnode] = 1; /*if legal - connect*/
}
} while ( (dadnode != EOF ) && (sonnode != EOF)); /*until user enter EOF */
Ctrl+d を押してこのループを (メイン関数から) 停止しようとすると、ループが継続し、数値の 1 つが -1 である数値のペアが見つかった後にのみ停止するのはなぜですか?
OK、「-1」と入力すると、メイン関数が path() 関数を呼び出して、ノード a とノード b が接続されているかどうかを確認する必要があります。そうであれば、path(Matrix,dadnode,sonnode) の結果に従ってメッセージを出力する必要があります。
ただし、この動作の代わりに、「プログラムは正常に終了しました」というメッセージが表示されます。このメッセージが表示されるのはなぜですか?
main 関数は path() 関数を呼び出しますか? コードのエラーが何であるかわかりません...