用C语言编写的Quine中的错误仅打印到用引号引起来的最后一行代码

时间:2019-03-16 05:04:52

标签: c quine

我正在尝试根据Java代码编写一个quine Wikipedia article。到目前为止,这是我的代码:

#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循环中打印它们,而不必将它们包裹在可见的引号中以完成打印奎纳。过去,我不得不缩短代码行,以使木盒完成打印。因此,令我感到惊讶的是我再次写出藜时遇到了这种问题。

0 个答案:

没有答案