C/C++ program that prints its own source code as its output

后端 未结 8 2267
天命终不由人
天命终不由人 2020-11-29 08:25

Wikipedia says it\'s called a quine and someone gave the code below:

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


        
相关标签:
8条回答
  • 2020-11-29 09:27

    Quine (Basic self-relicating code in c++`// Self replicating basic code

    [http://www.nyx.net/~gthompso/quine.htm#links] [https://pastebin.com/2UkGbRPF#links]

    // Self replicating basic code

    #include <iostream>     //1 line   
    #include <string>       //2 line    
    using namespace std;        //3 line    
                    //4 line    
    int main(int argc, char* argv[])    //5th line  
    {
            char q = 34;            //7th line  
            string l[] = {      //8th line  ---- code will pause here and will resume later in 3rd for loop
     " ",
     "#include <iostream>       //1 line   ",
     "#include <string>     //2 line    ",
     "using namespace std;      //3 line    ",
     "              //4 line    ",
     "int main(int argc, char* argv[])  //5th line  ",
     "{",
     "        char q = 34;          //7th line  ",
     "        string l[] = {        //8th line  ",
     "        };                //9th resume printing end part of code  ",      //3rd loop starts printing from here
     "        for(int i = 0; i < 9; i++)        //10th first half code ",
     "                cout << l[i] << endl;     //11th line",
     "        for(int i = 0; i < 18; i++)   //12th whole code ",
     "                cout << l[0] + q + l[i] + q + ',' << endl;    13th line",
     "        for(int i = 9; i < 18; i++)   //14th last part of code",
     "                cout << l[i] << endl;     //15th line",
     "        return 0;         //16th line",
     "}             //17th line",
            };                                          //9th resume printing end part of code  
            for(int i = 0; i < 9; i++)      //10th first half code 
                    cout << l[i] << endl;       //11th line
            for(int i = 0; i < 18; i++) //12th whole code 
                    cout << l[0] + q + l[i] + q + ',' << endl;  13th line
            for(int i = 9; i < 18; i++) //14th last part of code
                    cout << l[i] << endl;       //15th line
            return 0;           //16th line
    }               //17th line
    
    0 讨论(0)
  • 2020-11-29 09:30

    A quine has some depth roots in fixed point semantics related to programming languages and to executions in general. They have some importance related to theoretical computer science but in practice they have no purpose.

    They are a sort of challenge or tricks.

    The literal requirement is just you said, literal: you have a program, its execution produces itself as the output. Nothing more nor less, that's why it's considered a fixed point: the execution of the program through the language semantics has itself as its ouput.

    So if you express the computation as a function you'll have that

    f(program, environment) = program
    

    In the case of a quine the environment is considered empty (you don't have anything as input neither precomputed before)

    0 讨论(0)
提交回复
热议问题