Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言格式化河内塔的二维输出_C_Towers Of Hanoi - Fatal编程技术网

用C语言格式化河内塔的二维输出

用C语言格式化河内塔的二维输出,c,towers-of-hanoi,C,Towers Of Hanoi,我已经写了一个代码来解决河内塔之谜,但需要帮助格式化输出,以打印每个移动的二维表示。我知道我需要使用堆栈来解决这个问题,但我不知道怎么做。有人能帮我想出解决这个问题的办法吗 项目信息: #include <stdio.h> #include <stdlib.h> int step = 1; //Global variable void solver(int size, char fromRod, char toRod, char extraRod); //Pro

我已经写了一个代码来解决河内塔之谜,但需要帮助格式化输出,以打印每个移动的二维表示。我知道我需要使用堆栈来解决这个问题,但我不知道怎么做。有人能帮我想出解决这个问题的办法吗

项目信息:

#include <stdio.h>
#include <stdlib.h>

int step = 1;   //Global variable

void solver(int size, char fromRod, char toRod, char extraRod);  //Prototyping functionx
int main(int argc, char *argv[]) {   //Main function

   int size, step = 1, i, j, k, width = 1;   //Declaring variables

   if(argc <= 1) {   //Displays error if there is a missing line on the command
      printf("Missing command line argument.\n");
      printf("Usage: ./hanoi numberOfDisks\n");
      exit(1);
   }

   size = atoi(argv[1]);   //Finding size of puzzle from user input

   if (size > 15  || size < 2) {   //If the size is too big or too small displays an error
      printf("Invalid input. Size should be between 1 and 15.\n");
      return -1;
   }

   char setA[size + 1][31], setB[size + 1][31], setC[size + 1][31];   //Tower number only on last row

   solver(size, '1', '3', '2');   //Calls solver function

   return 0;

}

void solver(int size, char fromRod, char toRod, char extraRod) {   //Solver function

   if (size == 1) {   //If size is 1
      printf("%d: Move top disk from %c --> %c\n", step, fromRod, toRod);   //Prints step number and instructions
      return;
   }

   solver(size - 1, fromRod, extraRod, toRod);   //Recursive statement
   step++;   //Increments the number of steps
   printf("%d: Move top disk from %c --> %c\n", step, fromRod, toRod);
   step++;
   solver(size - 1, extraRod, toRod, fromRod);
}
“在此项目中,您将通过磁盘数(不超过15)作为参数,然后严格按照下面的规范显示逐步移动

每个控制棒应显示31个空格,然后在每个控制棒后显示5个空格。 杆应在中间显示,其高度应与磁盘的数量相同,每个磁盘应放在棒上,宽度正确,以反映磁盘的大小。最小的磁盘应显示为两个破折号(一个在杆的每一侧),第二个最小磁盘应显示为4个破折号。(杆每侧2个)等。杆每侧可能最大的圆盘应显示为15个破折号。当圆盘从一根杆移动到另一根杆时,尺寸应保持不变

例如,如果有5个磁盘,它们在杆上应显示为:

@@@@@@@@@@@@@@_|_@@@@@@@@@@@@@@&&&&&
@@@@@@@@@@@@@__|__@@@@@@@@@@@@@&&&&&
@@@@@@@@@@@@___|___@@@@@@@@@@@@&&&&&
@@@@@@@@@@@____|____@@@@@@@@@@@&&&&&
@@@@@@@@@@_____|_____@@@@@@@@@@&&&&&
1.
  • 5条(|)代表杆 _表示磁盘
  • @和&表示空间。
    • @表示磁盘前后的空间。如果磁盘是最大的,则两端都没有@
    • &表示杆后的空间
  • 杆下的编号为杆编号:1、2或3。”
我的代码:

#include <stdio.h>
#include <stdlib.h>

int step = 1;   //Global variable

void solver(int size, char fromRod, char toRod, char extraRod);  //Prototyping functionx
int main(int argc, char *argv[]) {   //Main function

   int size, step = 1, i, j, k, width = 1;   //Declaring variables

   if(argc <= 1) {   //Displays error if there is a missing line on the command
      printf("Missing command line argument.\n");
      printf("Usage: ./hanoi numberOfDisks\n");
      exit(1);
   }

   size = atoi(argv[1]);   //Finding size of puzzle from user input

   if (size > 15  || size < 2) {   //If the size is too big or too small displays an error
      printf("Invalid input. Size should be between 1 and 15.\n");
      return -1;
   }

   char setA[size + 1][31], setB[size + 1][31], setC[size + 1][31];   //Tower number only on last row

   solver(size, '1', '3', '2');   //Calls solver function

   return 0;

}

void solver(int size, char fromRod, char toRod, char extraRod) {   //Solver function

   if (size == 1) {   //If size is 1
      printf("%d: Move top disk from %c --> %c\n", step, fromRod, toRod);   //Prints step number and instructions
      return;
   }

   solver(size - 1, fromRod, extraRod, toRod);   //Recursive statement
   step++;   //Increments the number of steps
   printf("%d: Move top disk from %c --> %c\n", step, fromRod, toRod);
   step++;
   solver(size - 1, extraRod, toRod, fromRod);
}

另外,我有通过解算器函数正确输出的分步指令。这些都设置好了。格式化输出是这里的大问题。

欢迎来到SO!有趣的问题。我很好奇,
char setA[size+1][31]、setB[size+1][31]、setC[size+1][31];
?您当前的“解算器”是什么意思基本上已硬编码解决方案算法,但不知道每极上有多少个磁盘,这是制作每个谜题状态ASCII表示的重要步骤。欢迎使用堆栈溢出。请注意,您可以编辑自己的问题,通常您应该这样做,而不是在下面添加注释,尤其是不是对其他人的评论/问题的回应的评论。(对评论的回应可能也比评论更好地通过编辑来处理。)这次我是为你做的。请以后自己做。你可以删除你的评论;如果你不这样做,清理小组可能会这么做,但如果他们不为此烦恼,那就更好了。你打算如何显示?诅咒库是一个明智的选择吗?或者你需要使用一些Windows API函数吗?我有代码追溯到1987年,它使用curses API,格式要求略有不同-磁极之间的间隙随磁盘数量而变化,而不是固定的;我使用
=
而不是
-
(如图所示,或
)要标记磁盘,移动信息在显示屏上方,而不是下方,磁极也没有编号(我发现人们可以很容易地从1数到3)。但需要仔细跟踪哪个磁盘在哪里。