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

后端 未结 8 2272
天命终不由人
天命终不由人 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      //1 line   
    #include        //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        //1 line   ",
     "#include      //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
    

提交回复
热议问题