V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dikT
V2EX  ›  编程

写一段代码,这段代码就是打印这段代码,谁来试试☻

  •  
  •   dikT · 2016-05-20 16:39:37 +08:00 · 4733 次点击
    这是一个创建于 3100 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题

    29 条回复    2016-05-21 16:51:00 +08:00
    hanai
        1
    hanai  
       2016-05-20 16:42:30 +08:00   ❤️ 1
    function funcX() {console.log(arguments.callee.toString())}
    SakuraSa
        2
    SakuraSa  
       2016-05-20 16:46:59 +08:00   ❤️ 2
    sinalvee
        3
    sinalvee  
       2016-05-20 16:52:34 +08:00
    function func() {
    console.log(func);
    }
    yangg
        4
    yangg  
       2016-05-20 17:00:03 +08:00
    funcX, func 的都不是吧,你们都没调用。
    @hanai
    @sinalvee
    6IbA2bj5ip3tK49j
        5
    6IbA2bj5ip3tK49j  
       2016-05-20 17:07:50 +08:00 via Android
    定位到本文件,读出来,打印……😂
    cdwyd
        6
    cdwyd  
       2016-05-20 17:08:28 +08:00 via Android
    读取源码,然后打印出来?
    innoink
        7
    innoink  
       2016-05-20 17:10:20 +08:00
    以前看过好几个 c 语言版本的,看之前还以为 c 语言做不到。。
    SakuraSa
        8
    SakuraSa  
       2016-05-20 17:11:09 +08:00
    作为 py 使用者表示, py 写 Quine 还是比较简单的
    ```python
    s = 's = %r\nprint(s%%s)'
    print(s%s)
    ```
    sinalvee
        9
    sinalvee  
       2016-05-20 17:12:38 +08:00   ❤️ 1
    @yangg 好吧,逼我
    (function func() {
    console.log('(' + func + ')();');
    })();
    kindjeff
        10
    kindjeff  
       2016-05-20 17:13:40 +08:00   ❤️ 1
    function a() { console.log(a.toString(), "a()"); } a()
    GtDzx
        11
    GtDzx  
       2016-05-20 17:15:50 +08:00
    @innoink 任何一个图灵完全的语言能能写出无限多种 quine
    Troevil
        12
    Troevil  
       2016-05-20 17:16:56 +08:00
    @sinalvee 自我补全 lol
    lovedebug
        13
    lovedebug  
       2016-05-20 17:18:32 +08:00
    eval('var a = arguments.callee.toString();console.log(a.slice(a.indexOf("{") + 1, a.lastIndexOf("}")));');
    congeec
        14
    congeec  
       2016-05-20 17:20:09 +08:00
    ``` C
    char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}
    ```
    blahgeek
        15
    blahgeek  
       2016-05-20 17:25:30 +08:00 via iPhone   ❤️ 2
    从备忘录里面找出来的:

    Quine: Python & Brainfuck

    ```python
    s='s=print s[:2]+chr(39)+s+chr(39)+chr(10)+s[2:]'
    print s[:2]+chr(39)+s+chr(39)+chr(10)+s[2:]
    ```

    So … the key is … 把那些在字符串中表达方式不一样的字符用其他方式代替(`”’”`, `”\n ”`)

    另外…用`repr`(`%r`)算作弊吧……= =

    ```python
    s='s={};print s.format(repr(s))';print s.format(repr(s))
    ```

    更加 general 的方法:

    ```python
    import sys
    w = sys.stdout.write
    s = [105,109,112,111,114,116,32,115,121,115,10,119,32,61,32,115,121,115,46,115,116,100,111,117,116,46,119,114,105,116,101,10,115,32,61,32,91,10,119,40,39,39,46,106,111,105,110,40,109,97,112,40,99,104,114,44,32,115,91,58,51,55,93,41,41,41,10,119,40,39,44,39,46,106,111,105,110,40,109,97,112,40,115,116,114,44,32,115,41,41,41,10,119,40,39,39,46,106,111,105,110,40,109,97,112,40,99,104,114,44,32,115,91,51,55,58,93,41,41,41,10
    w(''.join(map(chr, s[:37])))
    w(','.join(map(str, s)))
    w(''.join(map(chr, s[37:])))
    ```

    And … Brainf**k! (use `cat quine.bf | sed 's/#.*//' | tr '\n' ' ' | sed 's/ //g ’`to remove comments and white spaces)

    ```
    # Character map (according to frequency):
    # + > < [ ] - .
    # 1 2 3 4 5 6 7
    # 43 62 60 91 93 45 46

    # start
    >>>

    # data
    >++
    >++
    >++

    >++++
    >+++
    >+++++
    >++
    >++++
    >++++
    >+++
    >+
    >+++
    >+
    >++
    >++
    >++++++
    >+++++
    >+++
    >++++
    >++
    >+
    >+++
    >++++++
    >+++++
    >++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+++
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >+++++
    >++
    >+++++
    >+++
    >+++
    >++++++
    >++
    >++
    >++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >++
    >+
    >+
    >+
    >+
    >+
    >+
    >+
    >+
    >++++
    >+++
    >+
    >+
    >+
    >+
    >+
    >++
    >++++++
    >+++++
    >+++
    >+
    >+
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+++
    >++
    >+
    >+
    >+
    >+
    >++++
    >+++
    >+
    >+
    >+
    >+
    >++
    >++++++
    >+++++
    >+++
    >+
    >+
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+++
    >++++++
    >++++++
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+++
    >++
    >+
    >+
    >+
    >+
    >+
    >++++
    >+++
    >+
    >+
    >+
    >+
    >+
    >+
    >++
    >++++++
    >+++++
    >+++
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+++
    >+
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+++
    >++
    >+
    >+
    >+
    >+
    >+
    >+
    >+
    >++++
    >+++
    >++++++
    >++++++
    >++++++
    >++++++
    >++++++
    >++++++
    >++++++
    >++
    >++++++
    >+++++
    >+++
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >++++
    >++++
    >++
    >+++++
    >++
    >++++
    >++
    >+++++
    >+++
    >+
    >++++
    >+++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++++
    >+++++
    >+++++
    >+++++
    >+++++
    >+++++
    >+++++
    >++
    >+++++
    >+
    >+
    >+
    >++++
    >++
    >++++
    >+++
    >+++
    >+
    >++
    >++
    >++++++
    >+++++
    >+++
    >++++
    >++
    >+
    >+++
    >++++++
    >+++++
    >+++
    >+++++++
    >++
    >++
    >++++++
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++++++
    >+++
    >+
    >++++
    >+++
    >+
    >+++++
    >+++
    >++++
    >+++
    >+++++
    >++
    >++++
    >+++
    >+++
    >+
    >+
    >+
    >+
    >+
    >+
    >+
    >+
    >++++
    >++
    >+
    >+
    >+
    >+
    >+
    >+
    >+
    >+
    >+++
    >++++++
    >+++++
    >++
    >++++++
    >++++++
    >+++++++
    >+++
    >+
    >+
    >+
    >+
    >++++
    >++
    >++++++
    >++++++
    >++++++
    >++++++
    >+++
    >++++++
    >+++++
    >++
    >++++++
    >++++++
    >++++++
    >++
    >++++
    >+++
    >+++++++
    >++
    >++++++
    >+++++
    >+++
    >++++
    >++++++
    >+++++
    >++
    >++
    >+++++
    >+
    >++++
    >++
    >+
    >+++++
    >++
    >++
    >++++
    >+++++++
    >++
    >+++++

    # return to first char
    [<] >

    # copy data
    [ # for each data
    [<+<+>>-] # copy to left two cells
    <[>+<-]> # copy back from left cell

    [>]>[>]+
    [<]<[<]>-
    [ # while greater than 0
    [>]> [>]< +
    [<]<[<]> -
    ]
    >
    ]

    <<->>
    >

    # Apply character map
    [
    # go to end of data plus 1; adds up to 43
    [>]> [>]
    >++++++++ # for range(8)
    [<+++++>-] # adds 5
    <+++ # adds 3
    [<]<[<]> # return to the character

    - # minus 1

    # if greater than 0
    [
    # total 19
    [>]> [>]<
    >++++ # for range(4)
    [<++++>-] # adds 4
    <+++ # adds 3 (19 totally)
    [<]<[<]>
    -

    [
    [>]> [>]< -- # 62 to 60
    [<]<[<]>
    - # minus 1

    [
    [>]> [>]<
    >+++++ # for range(5)
    [<++++++>-] # adds 6
    <+ # adds 1 (31 totally)
    [<]<[<]>
    -
    [
    [>]> [>]< ++ # adds 2
    [<]<[<]> # go back to char
    - # minus 1
    # 93 to 45
    [
    [>]> [>]<
    >+++++++ # for range(7)
    [<------->-] # minus 7
    <+ # adds 1 (minus 48 totally)
    [<]<[<]>
    -
    [
    [>]> [>]< + # adds 1
    [<]<[<]> # go back to char
    - # minus 1
    ]
    ]
    ]
    ]
    ]
    ]
    > # go to next char
    ]

    # Print first 3 ascii
    +++
    [
    # move the data to left cell
    > [<<+>>-] <
    # move to right cell
    [>+<-]
    <.>> # print it
    -
    ]

    <[<]

    # go to orig data
    -<

    +[<+]
    <[<]>

    # Print data (data format)
    [
    <<++++++++ # for range(8)
    [>++++++++<-] # plus 8
    >-- # 62
    . # ">"
    # 62 to 43
    <++++ # for range(4)
    [>----<-] # minus 4
    >--- # 43
    >
    [<.>-] # print "+"
    <[-]> # reset left cell to zero
    >
    ]

    +[>+] # go until 255
    >>
    [.>]
    ```

    aka.

    ```
    >>>>++>++>++>++++>+++>+++++>++>++++>++++>+++>+>+++>+>++>++>++++++>+++++>+++>++++>++>+>+++>++++++>+++++>++>++++>++>+++++>++>++++>++>+++++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>+++++>++>+++++>+++>+++>++++++>++>++>++>++++>++++>++>+++++>++>++++>++>+++++>++>+>+>+>+>+>+>+>+>++++>+++>+>+>+>+>+>++>++++++>+++++>+++>+>+>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++>+>+>+>+>++++>+++>+>+>+>+>++>++++++>+++++>+++>+>+>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++++++>++++++>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++>+>+>+>+>+>++++>+++>+>+>+>+>+>+>++>++++++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>+>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>++>+>+>+>+>+>+>+>++++>+++>++++++>++++++>++++++>++++++>++++++>++++++>++++++>++>++++++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>++++>++++>++>+++++>++>++++>++>+++++>+++>+>++++>+++>+++++>+++>++++>+++>+++++>++>++++++>+++++>+++++>+++++>+++++>+++++>+++++>++>+++++>+>+>+>++++>++>++++>+++>+++>+>++>++>++++++>+++++>+++>++++>++>+>+++>++++++>+++++>+++>+++++++>++>++>++++++>+++++>+++>++++>+++>+++++>++++++>+++>+>++++>+++>+>+++++>+++>++++>+++>+++++>++>++++>+++>+++>+>+>+>+>+>+>+>+>++++>++>+>+>+>+>+>+>+>+>+++>++++++>+++++>++>++++++>++++++>+++++++>+++>+>+>+>+>++++>++>++++++>++++++>++++++>++++++>+++>++++++>+++++>++>++++++>++++++>++++++>++>++++>+++>+++++++>++>++++++>+++++>+++>++++>++++++>+++++>++>++>+++++>+>++++>++>+>+++++>++>++>++++>+++++++>++>+++++[<]>[[<+<+>>-]<[>+<-]>[>]>[>]+[<]<[<]>-[[>]>[>]<+[<]<[<]>-]>]<<->>>[[>]>[>]>++++++++[<+++++>-]<+++[<]<[<]>-[[>]>[>]<>++++[<++++>-]<+++[<]<[<]>-[[>]>[>]<--[<]<[<]>-[[>]>[>]<>+++++[<++++++>-]<+[<]<[<]>-[[>]>[>]<++[<]<[<]>-[[>]>[>]<>+++++++[<------->-]<+[<]<[<]>-[[>]>[>]<+[<]<[<]>-]]]]]]>]+++[>[<<+>>-]<[>+<-]<.>>-]<[<]-<+[<+]<[<]>[<<++++++++[>++++++++<-]>--.<++++[>----<-]>--->[<.>-]<[-]>>]+[>+]>>[.>]
    ```
    blahgeek
        16
    blahgeek  
       2016-05-20 17:26:43 +08:00 via iPhone
    (好像太长了……求不 block ………
    blahgeek
        17
    blahgeek  
       2016-05-20 17:26:43 +08:00 via iPhone
    (好像太长了……求不 block ………
    oott123
        18
    oott123  
       2016-05-20 17:29:59 +08:00   ❤️ 1
    要不怎么说 PHP 是世界上最好的语言呢……
    <?php
    // 随便写啥
    highlight_file(__FILE__);
    // 也可以随便写啥

    逃(。
    blahgeek
        19
    blahgeek  
       2016-05-20 17:34:42 +08:00 via iPhone
    不过最短的应该是:

    #!/bin/cat
    est
        20
    est  
       2016-05-20 17:38:32 +08:00
    @blahgeek 有 python/js 的短么?

    >>> 1
    1
    cxbig
        21
    cxbig  
       2016-05-20 17:43:10 +08:00   ❤️ 1
    ```php
    <?php
    echo file_get_contents(__FILE__);
    ```
    Balthild
        22
    Balthild  
       2016-05-20 22:21:20 +08:00 via iPhone
    @blahgeek WTF …………
    Sunyanzi
        23
    Sunyanzi  
       2016-05-20 23:09:29 +08:00   ❤️ 2
    @oott123 @cxbig 其实 php 根本不用这么麻烦 ... 只要不出现 <?php 标签随便写啥都可以 ...
    cxbig
        24
    cxbig  
       2016-05-20 23:29:27 +08:00
    @Sunyanzi 在我看来那和 console 自动输出上一个命令的 return 值一样,不属于“代码打印代码”的范畴。
    Goheing
        25
    Goheing  
       2016-05-20 23:36:33 +08:00
    Perl 两行。。。
    @ARGV = $0;
    print for <>
    bazingaterry
        26
    bazingaterry  
       2016-05-21 00:55:53 +08:00
    我来抖机灵,空代码,运行的时候没有输出,算不算打印自身?
    UnisandK
        27
    UnisandK  
       2016-05-21 01:00:03 +08:00
    @bazingaterry 想到一起去了 doge 。。
    MCVector
        28
    MCVector  
       2016-05-21 01:22:47 +08:00
    ```
    #include <iostream>
    #include <fstream>
    int main()
    {
    std::ifstream f(__FILE__);
    std::cout<<f.rdbuf();
    f.close();
    return 0;
    }
    ```
    xiamx
        29
    xiamx  
       2016-05-21 09:46:16 +08:00
    #!/bin/cat
    xjl
        30
    xjl  
       2016-05-21 16:51:00 +08:00
    Golang:

    package main

    import "fmt"

    func main() {
    fmt.Printf("%s%c%s%c\n", s, 0x60, s, 0x60)
    }

    var s = `package main

    import "fmt"

    func main() {
    fmt.Printf("%s%c%s%c\n", s, 0x60, s, 0x60)
    }

    var s = `
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2607 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 118ms · UTC 15:55 · PVG 23:55 · LAX 07:55 · JFK 10:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.