Я пишу следующую программу на C.Почему функция не называется?
Эта программа представляет собой матрицу смежности, которая запрашивает у пользователя, чтобы установить соединение между узлами и чем проверить, есть ли связь между узлом А и узлом В.
# 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);
Основная функция запрашивает у пользователя, чтобы сделать ориентированный граф, а затем запрашивает пользователю ввести два узла, чтобы проверить, если они существует связь между узлом а и узлом 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 ");
}
следующая функция в отставке Урны ИСТИНА, если существует путь от и (папа узла) к V (сын узла) в противном случае возвращается 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*/
}
Наконец, я запускаю это в БГД (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», а затем основная функция должна вызвать функцию path(), чтобы проверить, связаны ли узлы a и узел b. Если они есть, то он должен выводить сообщение в соответствии с результатом пути (Matrix, dadnode, sonnode).
Однако вместо этого я получаю сообщение «Программа вышла нормально». Зачем мне это сообщение?
Включает ли главная функция функцию path()? Я не уверен, что ошибка в моем коде ...
Хотя я, как ваша '' dadnode' и sonnode' :), это более convential использовать '' parentNode' и childNode'. –
* нет. Почему функция не вызывается. или ... Почему функция не называется –