从c中终端的执行行输入

发布于 2024-12-18 18:03:45 字数 147 浏览 3 评论 0 原文

我遇到的问题是我必须用 c 语言编写一个河内塔游戏,并且环数的输入不能在程序中,但代码必须在执行中读取环数。

示例: ./hanoitower 3

代码应将 3 作为输入。我怎样才能做到这一点?

The problem that i have is that i have to write a hanois tower game in c and the input for the number of the rings must not be in the programm but the code must read the number of rings in the execution.

Example: ./hanoistower 3

And the code should get the 3 as the input. How can i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见 2024-12-25 18:03:46

命令行参数作为字符串通过 C 程序的 main() 函数传播。

在 int main(int argc, char *argv[]) 中,argc 是参数的数量,argv 是包含参数的字符串数组。请注意,程序名称本身始终是第一个“参数”。

由于参数作为字符串传递,您可能需要将 3 转换为整数,这可以使用 atoi 函数来完成。这是一个开始:

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

int main(int argc, char *argv[])
{
   int rings;
   if(argc != 2) {
       printf("Usage: %s number-of-rings\n",argv[0]);
       return 1;
   }

   rings = atoi(argv[1]);
   printf("Using number-of-rings = %d\n", rings);
...

   return 0;
}

Command line arguments are propagated as strings through the main() function of your C program.

In int main(int argc, char *argv[]) argc is the number of arguments, and argv is an array of strings containing the arguments. Note that the program name itself is always the first "argument".

As the arguments are passed as strings, you likely need to convert your 3 to an integer, which can be done with the atoi function. Here's a start:

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

int main(int argc, char *argv[])
{
   int rings;
   if(argc != 2) {
       printf("Usage: %s number-of-rings\n",argv[0]);
       return 1;
   }

   rings = atoi(argv[1]);
   printf("Using number-of-rings = %d\n", rings);
...

   return 0;
}
断爱 2024-12-25 18:03:46

我强烈建议阅读一本好的 C 编程书籍(2020 年,现代 C)。

这比在这里提问要快得多。不要忘记阅读您的 C 编译器的文档(可能是 GCC)、您的 构建自动化工具(例如GNU makeninja)和调试器(也许GDB)。如果您在 Linux 上编写代码,另请阅读高级 Linux 编程syscalls(2),以及文档GNU glibc (或 musl-libc,如果你使用它)。

然而,程序参数以空终止字符串数组的形式提供给主函数,通常声明它就像

 int main (int argc, char**argv) { /*...*/ }

使用 ./hanoistower 3 运行程序,并且如果您的 hanoistower.c 是你的源代码(你需要在启用调试和警告的情况下进行编译,即 Linux 上的 gcc -Wall -g hanoistower.c -o hanoistower ),那么你就多了一个参数,因此

  1. argc == 2
  2. argv[0]"./hanoitower" 字符串
  3. argv[1] > 是 "2" 字符串(使用 atoi 将其转换为 int
  4. argv[2]NULL

拜托,请学会使用调试器(Linux 上的 gdb)。

I strongly suggest reading a good C programming book (in 2020, Modern C).

It will be much faster than asking questions here. Don't forget to also read the documentation of your C compiler (perhaps GCC), your build automation tool (e.g. GNU make or ninja), and debugger (perhaps GDB). If you code on and for Linux, read also Advanced Linux Programming and syscalls(2), and the documentation of GNU glibc (or of musl-libc, if you use it).

Hovever, the program arguments is given as a null terminated array of strings to the main function, which is usually declared as

 int main (int argc, char**argv) { /*...*/ }

if you run your program with ./hanoistower 3 and if your hanoistower.c is your source code (which you need to compile with debugging and warning enabled, i.e. gcc -Wall -g hanoistower.c -o hanoistower on Linux) then you have one extra argument, so

  1. argc == 2
  2. argv[0] is the "./hanoistower" string
  3. argv[1] is the "2" string (use atoi to convert it to an int)
  4. argv[2] is NULL

Please, please learn to use the debugger (gdb on Linux).

御守 2024-12-25 18:03:46

只需将 argcargv 添加到 main 方法参数列表中,如下所示:

int main ( int argc, char *argv[] )

然后使用 argv 作为用于指定代码中环数的变量。

Just add, argc and argv to the list of main method parameters, as shown below:

int main ( int argc, char *argv[] )

Then use argv as the variable to specify number of rings inside your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文