0

以下のプログラムを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() 関数を呼び出しますか? コードのエラーが何であるかわかりません...

4

1 に答える 1

2

EOF-1は ( ) で定義されていますが、 +をstdio.h使用してメッセージを送信すると、別の文字値 ( )が送信されます。( )の定義は、ファイルの終わりまたはその他のエラーが原因で失敗した関数の戻り値であることを意味します。したがって、入力値 (または) をと比較する代わりに、 の戻り値を と比較する必要があります。CtrlDEOF4EOF-1dadnodesonnodeEOFscanf()EOF

の戻り値scanf()は、読み取られたアイテムの数 (この場合は のみである必要があります1)、またはユーザーが+EOFを送信した場合(Windows ユーザーは + を送信する必要がありますCtrl)です。DCtrlZ

例:

int dadnode, sonnode;
int result;

while (true)
{
    result = scanf("%d", &dadnode);
    if (result < 1) break;

    result = scanf("%d", &sonnode);
    if (result < 1) break;
}
于 2012-04-29T05:47:52.033 に答える