实施 quinine 的技术

发布于 2024-11-30 21:25:39 字数 208 浏览 2 评论 0原文

几年前,我正在考虑如何制作一个打印自己源代码的程序,我想出了这两种方法:

  • 程序根据可执行文件名称打印 .c 或 .cpp 文件(app1 打印 app1.cpp) 。但如果 .cpp 文件位于其他位置,这将会失败。
  • 该程序“聪明”地使用了字符串(我忘记了它的源代码),并打印自身。

程序是否可以使用其他算法来打印自己的源代码?

A few years back I was thinking about ways to make a program that prints its own source code and I came up with these two approaches:

  • the program prints the .c or .cpp file based upon the executable name (app1 prints app1.cpp). but this will fail in case the .cpp file is located somewhere else.
  • the program makes "clever" use of strings (I forgot the source code of this), and prints itself.

Is there any other algorithm a program can use to print its own source code?

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

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

发布评论

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

评论(4

[浮城] 2024-12-07 21:25:39

我认为你的两个案例涵盖了所有选项。情况(1)涵盖“从外部设备加载程序源”形式的情况,而情况(2)涵盖“以编程方式生成程序源”形式的情况。您当然可以考虑采用混合方法,例如“从文件中读取程序的前半部分,然后以编程方式生成后半部分”,但这似乎与您上面描述的没有任何不同。

I think your two cases cover all the options. Case (1) covers cases of the form "load the program source from an external device," while case (2) covers cases of the form "generate the program source programmatically." You could of course consider a hybrid approach like "read the first half of the program from a file and then generate the second half programmatically," but this doesn't seem any different from what you described above.

情释 2024-12-07 21:25:39

正如 Steve 在评论中指出的那样,情况 1) 通常不被视为 quine,可能是因为在任何可以执行文件 I/O 的语言中,它本质上都是微不足道的,

情况 2) 是大多数人在说 quine 时的意思,即“巧妙使用”弦的部分是你炫耀的部分。

在某些语言中,存在第三种情况(通常也不将其视为真正的奎因,因为它比情况 1 更微不足道)。如果一种语言允许一个完全没有语句的程序结构良好,那么这个“空”程序通常不会打印任何内容,这当然与其源代码相同。例如 TCL 脚本:

将打印:

;)

As Steve pointed out in comments case 1) is usually not considered a quine, probably becasue its essetially trivial to do in any language that can do file I/O

case 2) is what most people mean when they say quine, the 'clever use of strings' being the part you are showing off with.

in some languages there is a 3rd case (which is also not usually counted as a true quine as it is even more trivial than case 1). If a language allows a program with no statements in at all to be well formed then this 'empty' program will usual print nothing, which is of course the same as its source code. e.g. the TCL script:

will print:

;)

小忆控 2024-12-07 21:25:39

程序不必是具有特定“名称”的“可执行文件”,并且源代码不必位于具有特定“名称”的“文件”中。这些都是现代操作系统的产物,与手头的工作完全无关。

A program need not be an "executable" with a particular "name", and the source code need not be in a "file" with a particular "name". These are all artifacts of modern operating systems, totally irrelevant to the job at hand.

彼岸花ソ最美的依靠 2024-12-07 21:25:39
 char*f="char*f=%c%s%c;main()
 {printf(f,34,f,34,10);}%c";
 main(){printf(f,34,f,34,10);}

(一行)

有很多这样的代码
http://www.nyx.net/~gthompso/quine.htm

对我来说这是最好的方法:将代码分配给变量并重复使用它。

 char*f="char*f=%c%s%c;main()
 {printf(f,34,f,34,10);}%c";
 main(){printf(f,34,f,34,10);}

(in one line)

there are many codes like this in
http://www.nyx.net/~gthompso/quine.htm

to me this is the best way: assign code to variable and use it repeatly.

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