为什么函数没有调用?

时间:2012-04-29 05:23:44

标签: c

我正在用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);

主要功能要求用户制作有向图,然后要求用户输入两个节点,检查节点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(dad节点)到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”然后main函数应该调用path()函数来检查节点a和节点b是否连接。如果是,那么它应该根据路径的结果(Matrix,dadnode,sonnode)输出一条消息。

但是,我收到消息“程序正常退出”,而不是这种行为。 为什么我收到此消息?

main函数是否甚至调用path()函数? 我不确定我的代码中的错误是什么......

1 个答案:

答案 0 :(得分:2)

EOF-1中定义为(stdio.h),但是当您使用 Ctrl + D 发送{时{1}}消息,您发送的是不同的字符值(EOF)。 (4)的EOF定义意味着由于文件结束或其他错误而失败的函数的返回值。因此,您应该将-1的返回值与dadnode进行比较,而不是将输入值(sonnodeEOF)与scanf()进行比较。

EOF的返回值是读取的项目数(在您的情况下,它应该只是scanf()),或者1如果用户发送 Ctrl + D (Windows用户必须发送 Ctrl + Z )。

示例:

EOF
相关问题