我遇到了这个术语 - 奎因(也称为自我复制程序)。只是想了解更多。如何写奎因?它们在任何地方都可以使用,还是只是一种娱乐练习?

我已经开始使用Python,并且我可能会尝试用Python 编写一个。有什么建议么?

有帮助吗?

解决方案

基内斯是在实际意义上没用,但他们是一个很好的锻炼,以帮助您了解更多关于语言。

下面是一个非常简洁一个在python:

a='a=%r;print a%%a';print a%a

其他提示

至少,基内斯是产生他们自己的源作为其输出的程序。他们在构建哥德尔不完备性上证明一个必要的步骤。

这是否构成了实际应用是我提供的任何评论。

  

一个quine指其产生它自己的源代码的副本作为其唯一的输出的计算机程序。

我还没有看到一个实际用途,但我敢肯定有一个在某处。


的Python实施例(这里找到)

print (lambda s:s+`s`+')')("print (lambda s:s+`s`+')')(")

℃实施例(这里找到

#include <stdio.h>

int main(int argc, char** argv)
{
/* This macro B will expand to its argument, followed by a printf
 command that prints the macro invocation as a literal string */
#define B(x) x; printf("  B(" #x ")\n");

/* This macro A will expand to a printf command that prints the
 macro invocation, followed by the macro argument itself. */
#define A(x) printf("  A(" #x ")\n"); x;

/* Now we call B on the text of the program
 up to this point. It will execute the command, and then cause
 itself to be printed. */
  B(printf("#include <stdio.h>\n\nint main(int argc, char** argv)\n{\n/*
    This macro B will expand to its argument, followed by a printf\n
    command that prints the macro invocation as a literal string
    */\n#define B(x) x; printf(\"  B(\" #x \")\\n\");\n\n/* This macro
    A will expand to a printf command that prints the\n
    macro invocation, followed by the macro argument itself. */\n#define A(x)
    printf(\"  A(\" #x \")\\n\"); x;\n\n/* Now we call B on the text
    of the program\n up to this point. It will execute the command,
    and then cause\n itself to be printed. */\n"))
  A(printf("/* Lastly, we call A on a command to print the remainder
    of the program;\n it will cause itself to be printed, and then
    execute the command. */\n}\n"))
/* Lastly, we call A on a command to print the remainder of the program;
 it will cause itself to be printed, and then execute the command. */
}

正如其他解释,基内斯是自我复制的精确副本的程序。

至于应用程序,如果你认为的DNA编码的逻辑来解释自身和自我复制 - 答案是非常简单的,没有基内斯我们就不会在这里的概念,我们将永远无法创造人工(自繁殖)的生活。

这是我最喜欢的C例

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

两件事情我学到从它:

  1. 空白不是必需的,但确实帮助的可读性
  2. 该prinftf功能是真正强大的

我不能存在任何数据来说,编写一奎因,或两个具有扩展我的主意或让我成为一个更好的程序员。但它 乐趣这样做,至少最初的几倍。无论如何,你要求有关如何编写之一。我可以点你写得很好的参考文件:

克雷格*卡普兰有一个整洁的文件,其中介绍了如何实际产生quines:

  • 寻找自我的代码记录
    • 本报告审查了该问题写一个自我的记录程序:一程序,运行时,会产生本身作为输出。问题是审查的观点看,自基准,酒店的一个自记录的程序必须表现。该报告进行从早期的程序,无法正常工作,通过连续地复杂的程序,这种做法的一个解决方案、工作自的记录程序。然后它步回一点并表示了某些程序如何似乎可以作弊,并仍然适合该定义的一个自记录的程序,提出改进建议以这一定义。在每一个步骤,报告讨论如何给定的程序证明的微妙之间关系的计算机编程和自参考。

你也可能会找到大卫*马度尔的 "Quines(自我复制计划)" 有趣阅读。

最后,如果你想看到实现,检查出来的 奎因页 你在哪里可以找到quines在各种语言和其他相关问题。

使用什么基内斯为?编程练习和病毒。

有一个病毒需要以某种方式复制 - 和一个办法就是使之成为奎因。比方说,一个假想的防病毒会标志,上面写着自己的二进制到内存中的任何处理程序(将它传递给目标受害者);的方法来解决,可以更好地把它输出本身。

记住,在机器代码喹将不需要编译。

下面是一个在Python(它的丑陋,我只是写它来尝试一下)。甚至不知道这是一个叫蒯当时的情况。

def e(s): print s[:42]+s[42:].replace('#','"'); print 'e("""'+s+'""")'
e("""def e(s): print s[:42]+s[42:].replace('#','"'); print 'e(###'+s+'###)'""")

哦,并回答您的其他问题:基内斯是完全无用的。

我写我的第一奎因在1979年 - 在Fortran语言。我有一个随机的思考基内斯PHP中的一天,感觉就像张贴了同Q作为OP但是做一个好孩子我先检查了Q&A d / B。反正后人这里是我的PHP(CLI)奎因。我会在任何较短的变体的利益。 : - )

<?php $x='<?php $x=0;echo strtr( $x, array(chr(39).$x.chr(39)));';echo strtr( $x, array(chr(39).$x.chr(39)));

109个字节,但与最后CR切断。这还不包括在“欺骗”:

<?php readfile( __FILE__);

和此 QuineProgram 维基引述甚至更短的一个:

<?php printf($a='<?php printf($a=%c%s%c,39,$a,39);',39,$a,39);

这是 C++ 中的一个有趣的 quine: http://npcomplete.weebly.com/1/post/2010/02/self-reducing-c-program-quine.html

奎因是为了好玩。据我所知,它们没有实际用途。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top