首页 文章

为什么函数没有调用?

提问于
浏览
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);

主要功能要求用户制作有向图,然后要求用户输入两个节点以检查它们是否存在节点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 回答

  • 2

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

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

    例:

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

相关问题