Here is an interesting self-replicating program (program that prints its own source code) in C++. It is interesting because unlike most quines in c/c++ it does not rely on pattern substitution of the printf or sprintf functions and it can be arbitrarily long. It instead accumulates a list of code lines and prints them all at the end in a loop. The only restriction is that no line may contain the quotation mark; using character constants and (char)34 works around that. (Copy the source out to get


#include<iostream>
using namespace std; int main(){int i=0; string ss[50]; cout<<'#'<<'i'<<'n'<<'c'<<'l'<<'u'<<'d'<<'e'<<'<'<<'i'<<'o'<<'s'<<'t'<<'r'<<'e'<<'a'<<'m'<<'>'<<endl;
ss[i++]="using namespace std; int main(){int i=0; string ss[50]; cout<<'#'<<'i'<<'n'<<'c'<<'l'<<'u'<<'d'<<'e'<<'<'<<'i'<<'o'<<'s'<<'t'<<'r'<<'e'<<'a'<<'m'<<'>'<<endl;";
for (int j=0; j<5; j++) {
ss[i++]="for (int j=0; j<5; j++) {";
cout << ss[j] << endl;
ss[i++]="cout << ss[j] << endl;";
cout << 's'<<'s'<<'['<<'i'<<'+'<<'+'<<']'<<'=' << (char)34 << ss[j] << (char)34 << ';' << endl;
ss[i++]="cout << 's'<<'s'<<'['<<'i'<<'+'<<'+'<<']'<<'=' << (char)34 << ss[j] << (char)34 << ';' << endl;";
if(j==4) cout << '}' << '}'<<endl;
ss[i++]="if(j==4) cout << '}' << '}'<<endl;";
}}
 


Comments




Leave a Reply