在这里获得帮助。我必须为Djikstra的算法编写一个程序。不必从用户或任何东西获取输入,只需硬编码。这是我第一次用C语言编写任何内容。用它来做的并不是那么好。我得到了我的代码,它在逻辑上将如何在我脑海中起作用。我的问题是,当我运行它时,我什么也得不到。什么都没打印出来。有人可以帮助我和我一起解决这个问题,那会很棒。我会保持更新,因为我试图找到问题,但是在C语言中比我更聪明的人可能会更容易理解它。
#include <stdio.h>
void main (){
int ab = 3;//path from a to b
int ac = 7;//path from a to c
int ad = 9;//path from a to d
int bc = 2;//path from b to c
int bd = 4;//path from b to d
int cd = 1;//path from c to d
int a = 10;//number values for position
int b = 20;
int c = 30;
int d = 40;
int position = 10;//starting position a
int currenttravel = 0;
//starting at a
//if (position == 10){
int checker = 40;//check for when at d
do
{
//check for if at a
if (position == 10){
//if path a to b is shortest
if (ab < ac && ab < ad){
position = b;//go to b
printf("%d", &position);
currenttravel+=ab;
}
//or if path a to c is shortest
else if (ac < ad){
position = c;//go to c
printf("%d", &position);
currenttravel+=ac;
}
else{
position = d;
printf("%d", &position);
currenttravel+=ad;
}
}
if (position == 20)//at b
{
if (bc < bd){
position = c;
printf("%d", &position);
currenttravel+=bc;
}
else{
position = d;
printf("%d", &position);
currenttravel+=bd;
}
}
if (position == 30){
position = d;
printf("%d", &position);
currenttravel+=cd;
}
}
while(position != checker);
// }//end if start position is a
printf("%d", currenttravel);
return; //leave function
}
我尽可能地评论,所以希望可以遵循我的逻辑。我可能过于复杂了,但这应该是一种可行的方法。
有效的固定代码!
#include <stdio.h>
int main (){
int ab = 3;//path from a to b
int ac = 7;//path from a to c
int ad = 9;//path from a to d
int bc = 2;//path from b to c
int bd = 4;//path from b to d
int cd = 1;//path from c to d
int a = 10;//number values for position
int b = 20;
int c = 30;
int d = 40;
int position = 10;//starting position a
int currenttravel = 0;
//starting at a
//if (position == 10){
int checker = 40;//check for when at d
do
{
printf("starting at a \n");
//check for if at a
if (position == a){
//if path a to b is shortest
if (ab < ac && ab < ad){
position = b;//go to b
printf("b \n");
currenttravel+=ab;
}
//or if path a to c is shortest
else if (ac < ad){
position = c;//go to c
printf("c \n");
currenttravel+=ac;
}
else{
position = d;
printf("d \n");
currenttravel+=ad;
}
}
if (position == b)//at b
{
if (bc < bd){
position = c;
printf("c \n");
currenttravel+=bc;
}
else{
position = d;
printf("d \n");
currenttravel+=bd;
}
}
if (position == c){
position = d;
printf("d \n");
currenttravel+=cd;
}
}
while(position != checker);
// }//end if start position is a
printf("%d", currenttravel);
// return; //leave function
}
谢谢大家的帮助。现在我只需将其转换为Prim的算法(这将非常简单,因为我只是不添加所有内容)。如果不同的起始位置也可以玩,但现在可能已经足够了。
答案 0 :(得分:2)
以下是使用gcc编译时得到的输出:
main.cpp:2:12: error: ‘::main’ must return ‘int’
void main (){
^
main.cpp: In function ‘int main()’:
main.cpp:64:2: error: return-statement with no value, in function returning ‘int’ [-fpermissive]
return;
^
main.cpp: In function ‘int main()’:
main.cpp:26:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:32:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:37:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:46:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:51:43: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
^
main.cpp:57:39: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
printf("%d", &position);
首先,它应该是int main
和printf("%d", position)
,你也应该删除return;
。该函数应返回int
。
然后代码执行并打印出一些内容:
2030406
您可能需要介于两者之间的换行符,请使用printf("%d\n", position)
。然后:
20
30
40
6
我还没有检查输出的正确性。