我正在尝试根据这篇Wikipedia 文章中的 Java 代码编写一个 quine 。到目前为止,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void print(int x)
{
char * buf = itoa(x,NULL,10);
puts(buf);
}
int main(void)
{
rsize_t opening = 15;
rsize_t i = 0;
char * q[] =
{
"#include <stdio.h>",
"#include <stdlib.h>",
"#include <stdint.h>",
"",
"void printf(int x)",
"{",
" char * buf = itoa(x,NULL,10);",
" puts(buf);",
"}",
"",
"int main(void)",
"{",
" rsize_t opening = 15",
" rsize_t i = 0;",
" char * q[] =",
" {",
" };",
" while (i <= opening)",
" {",
" printf(q[i]);putchar(10);i++;",
" }",
" i = 0;",
" while (q[i] != NULL)",
" {",
" putchar(34);printf(q[i]);putchar(34);putchar(44);putchar(10);i++;",
" }",
" i = opening+1;",
" while (q[i] != NULL)",
" {",
" printf(q[i]);putchar(10);i++;",
" }",
" return 0;",
"}",
};
while (i <= opening)
{
printf(q[i]);putchar(10);i++;
}
i = 0;
while (q[i] != NULL)
{
putchar(34);printf(q[i]);putchar(34);putchar(44);putchar(10);i++;
}
i = opening+1;
while ( q[i] != NULL )
{
printf(q[i]);putchar(10);i++;
}
return 0;
}
该程序的输出如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void printf(int x)
{
char * buf = itoa(x,NULL,10);
puts(buf);
}
int main(void)
{
rsize_t opening = 15
rsize_t i = 0;
char * q[] =
{
"#include <stdio.h>",
"#include <stdlib.h>",
"#include <stdint.h>",
"",
"void printf(int x)",
"{",
" char * buf = itoa(x,NULL,10);",
" puts(buf);",
"}",
"",
"int main(void)",
"{",
" rsize_t opening = 15",
" rsize_t i = 0;",
" char * q[] =",
" {",
" };",
" while (i <= opening)",
" {",
" printf(q[i]);putchar(10);i++;",
" }",
" i = 0;",
" while (q[i] != NULL)",
" {",
" putchar(34);printf(q[i]);putchar(34);putchar(44);putchar(10);i++;",
" }",
" i = opening+1;",
" while (q[i] != NULL)",
" {",
" printf(q[i]);putchar(10);i++;",
" }",
" return 0;",
"}",
"",
正如我们所看到的,程序在打印最后一个用引号括起来的换行符并后跟逗号:("",) 之后就停止打印。它应该打印数组 (char * q[]) 的封闭括号 (};),然后是所有三个 while 循环,而不用将它们包裹在可见的引号中以完成打印 quine。过去,我必须缩短代码行数才能让 quine 完成打印。因此,让我感到惊讶的是,我再次遇到了这种写 quine 的问题。