C ++ quine [英] C++ quine
问题描述
我很无聊,所以我只是在100%ansi c ++中编写了一个quine(程序将打印出它自己的
源代码),而不使用任何c-library
函数(例如printf),并且不对字符进行任何假设
设置。
有何评论?错误?关于如何缩短它的想法?
Emlyn
#include< string>
#include< iostream>
使用命名空间std;
int main(){
string s =" s",s0 =" \\\
",s1 = " \"",s2 =" \\",s3 =" n",s4 =" ;;" ;;
string s5 =" string s",s6 =",s",s7 =" =",s8 = s7 + s1,s9 =" +";
string s10 =" #include< string>",s11 =" #include< iostream> " ;;
string s12 =" using namespace std;",s13 =" int main(){" ;;
string s14 =" return 0;",s15 ="}" ;;
string s20 =" COUT<< S10<< S0<< S11<< S0<< S12<< S0<< S13<< S0;英寸;
串S21 = QUOT;
COUT<< S5<< S8<< S<< S1和S2;< S6<&℃,LT;< S8<< S2<< S3<< S1和S2; < S6<&。1 LT; < s8<< s2<< s1<< s1<<< s6;" ;;
string s22 ="
cout<<< 2< < S8<< S2<< S2<< S1和S2;< S6<&3; LT;< S8<< S3<< S1和S2;< S6<< 4℃; < s8<< s4<< s1<< s4<< s0"" ;;
string s23 ="
cout<< s5<小于5<< S8<< S5<< S1和S2;< S6<&6; LT;< S8<< S6<< S1和S2;< S6<&7; LT; < s8<<< s7<< s1;" ;;
string s24 ="
cout<< s6<< 8<< s7< < S<&7; LT;< S9<< S<&。1 LT;< S6<&其中R 9 LT;< S8<< S9<< S1和S2;< S4<< S0;" ;;
string s25 =" COUT<< S5<< 10<< S8<< S10<< S1和S2;< S6<< 11<< S8<< S11<< S1和S2;< S4<< S0 ;" ;;
string s26 =" COUT<< S5<&其中R 12 LT;< S8<< S12<< S1和S2;< S6<&; 13 LT;< S8<< S13<< S1和S2;< S4<< S0 ;" ;;
string s27 =" COUT<< S5<< 14<< S8<< S14<< S1和S2;< S6<< 15℃;< S8<< S15<< S1和S2;< S4<< S0 ;英寸;
串S28 = QUOT;
COUT<< S5<< 20℃;< S8<< S20<< S1和S2;< S4< ;< S0<< S5<< 21<< S8<< S21<< S1和S2;< S4<< S0<< S5<< 22℃;< S8<< <无线电通信/>
s22<< s1<< s4<< s0;" ;;
string s29 ="
cout<< s5< < 23℃;< S8<< S23<< S1和S2;< S4<< S0<< S5<< 24<< S8<< S24<< S1和S2;< S4<< ; S0<< S5<< 25℃;< S8<<
S25<< S1和S2;< S4<< S0;英寸;
串S30 = QUOT;
COUT<< S5<< 26<< S8<< S26<< S1和S2;< S4<< S0<< S5<< 27< ;< S8<< S27<< S1和S2;< S4<< S0<< S5<< 28和60;< S8<<
S28<< S1和S2; < s4<<< s0;" ;;
string S31 = QUOT;
COUT<< S5<< 29<< S8<< S29<< S1和S2;< S4<< S0<< S5<< 30℃; < S8<< S30<< S1和S2;< S4<< S0<< S5<< 31<< S8<<
S31<< S1和S2;< ; s4<<<< s0;" ;;
string s32 ="
cout<< s5<< 32<< s8<< s32<< S1和S2;< S4<< S0<< S5<&γ-33 LT;< S8<< S33<< S1和S2;< S4<< S0<< S5<< 34 - ;< s8<<
s34<< s1<< s4<< s0;" ;;
string s33 ="
COUT<< S5<< 35℃;< S8<< S35<< S1和S2;< S4<< S0<< S5<&R 36取代LT;< S8<< S36<< S1和S2 ;< s4<<<< s0;" ;;
string s34 ="
cout<< s20<< s0<< s21<<< s0< ;< S22<< S0<< S23<< S0<< S24<< S0< < S25<< S0<< S26<< S0<< S27<< S0
将< S28<< S0;英寸;
串S35 = QUOT;
COUT<< S29<< S0<< S30<< S0<< S31<< S0<< S32<< S0<< ; S33<< S0< < s34<<< s0<< s35<< s0<< s36<< s0
;" ;;
string s36 =" COUT<< S14<< S0<< S15<< S0;英寸;
COUT<< S10<< S0<< S11<< S0<< S12< ;< S0<< S13<< S0;
COUT<< S5<< S8<< S<< S1和S2;< S6<&℃,LT;< S8< ;< S2<< S3<< S1和S2;< S6<&。1 LT; < S8<< S2<< S1和S2;< S1和S2;< S6;
COUT<< 2';< S8<< S2<< S2<< S1和S2; < S6<&3; LT;< S8<< S3<< S1和S2;< S6<< 4℃; < S8<< S4<< S1和S2;< S4<< S0;
COUT<< S5<< 5';< S8<< S5<< S1和S2; < S6<&6; LT;< S8<< S6<< S1和S2;< S6<&7; LT; < S8<< S7<< S1;
COUT<< S6<< 8 LT;< S7<< S<&7; LT;< S9<< S< &。1 LT;< S6<&其中R 9 LT;< S8<< S9<< S1和S2;< S4<< S0;
COUT<< S5<< 10< ;< S8<< S10<< S1和S2;< S6<< 11<< S8<< S11<< S1和S2;< S4<< S0;
COUT<< S5<&其中R 12 LT;< S8<< S12<< S1和S2;< S6<&; 13 LT;< S8<< S13<< S1和S2;< S4<< S0 ;
COUT<< S5<< 14<< S8<< S14<< S1和S2;< S6<< 15℃;< S8<< S15<< S1和S2 ;< S4<< S0;
COUT<< S5<< 20℃;< S8<< S20<< S1和S2;< S4<< S0<< S5<< 21<< S8<< S21<< S1和S2;< S4<< S0<< S5<< 22℃;< S8<<
S22<< S1和S2;< S4<< S0;
COUT<< S5<< 23℃;< S8<< S23<< S1和S2 ;< S4<< S0<< S5<< 24<< S8<< S24 << S1和S2;< S4<< S0<< S5<< 25℃;< S8<<
S25<< S1和S2;< S4<< S0;
COUT<< S5<< 26<< S8<< S26<< S1和S2;< S4<< S0<< S5<< 27< < S8<< S27<< S1和S2;< S4<< S0<< S5<< 28和60;< S8<<
S28<< S1和S2;< ; S4<< S0;
COUT<< S5<< 29<< S8<< S29<< S1和S2;< S4<< S0<< ; S5<< 30℃;< S8<< S30<< S1和S2;< S4<< S0<< S5<< 31<< S8<<
S31<< S1和S2;< S4<< S0;
COUT<< S5<&γ-32 LT;< S8<< S32<< S1和S2;< S4<< S0<< S5<&γ-33 LT;< S8<< S33<< S1和S2;< S4<< S0<< S5<< 34 - ;< S8<< <登记/>
S34<< S1和S2;< S4<< S0;
COUT<< S5<< 35℃;< S8<< S35<< S1和S2 ;< s4<<< s0<< s5<<<<<< s<<<< s<吨;< S1和S2;< S4<< S0;
COUT<< S20<< S0<< S21<< S0<< S22<< S0<< S23<< S0<< S24<< S0< < s25<<< s0<< s26<< s0<< s27<< s0
<< s28<< s0;
COUT<< S29<< S0<< S30<< S0<< S31<< S0<< S32<< S0<< S33<< S0< < S34<< S0<< S35<< S0<< S36<< S0
;
COUT<< S14<< S0< < s15<< s0;
返回0;
}
I was bored, so I just wrote a quine (program that will print out it''s own
source code when run) in 100% ansi c++, without using any c-library
functions (such as printf), and not making any assumptions on the character
set.
Any comments? bugs? ideas on how to shorten it?
Emlyn
#include <string>
#include <iostream>
using namespace std;
int main() {
string s = "s", s0 = "\n", s1 = "\"", s2 = "\\", s3 = "n", s4 = ";";
string s5 = " string s", s6 = ", s", s7 = " = ", s8 = s7+s1, s9 = "+";
string s10 = "#include <string>", s11 = "#include <iostream>";
string s12 = "using namespace std;", s13 = "int main() {";
string s14 = " return 0;", s15 = "}";
string s20 = " cout<<s10<<s0<<s11<<s0<<s12<<s0<<s13<<s0;";
string s21 = "
cout<<s5<<s8<<s<<s1<<s6<<0<<s8<<s2<<s3<<s1<<s6<<1< <s8<<s2<<s1<<s1<<s6;";
string s22 = "
cout<<2<<s8<<s2<<s2<<s1<<s6<<3<<s8<<s3<<s1<<s6<<4< <s8<<s4<<s1<<s4<<s0;";
string s23 = "
cout<<s5<<5<<s8<<s5<<s1<<s6<<6<<s8<<s6<<s1<<s6<<7< <s8<<s7<<s1;";
string s24 = "
cout<<s6<<8<<s7<<s<<7<<s9<<s<<1<<s6<<9<<s8<<s9<<s1 <<s4<<s0;";
string s25 = " cout<<s5<<10<<s8<<s10<<s1<<s6<<11<<s8<<s11<<s1<<s4 <<s0;";
string s26 = " cout<<s5<<12<<s8<<s12<<s1<<s6<<13<<s8<<s13<<s1<<s4 <<s0;";
string s27 = " cout<<s5<<14<<s8<<s14<<s1<<s6<<15<<s8<<s15<<s1<<s4 <<s0;";
string s28 = "
cout<<s5<<20<<s8<<s20<<s1<<s4<<s0<<s5<<21<<s8<<s21 <<s1<<s4<<s0<<s5<<22<<s8<<
s22<<s1<<s4<<s0;";
string s29 = "
cout<<s5<<23<<s8<<s23<<s1<<s4<<s0<<s5<<24<<s8<<s24 <<s1<<s4<<s0<<s5<<25<<s8<<
s25<<s1<<s4<<s0;";
string s30 = "
cout<<s5<<26<<s8<<s26<<s1<<s4<<s0<<s5<<27<<s8<<s27 <<s1<<s4<<s0<<s5<<28<<s8<<
s28<<s1<<s4<<s0;";
string s31 = "
cout<<s5<<29<<s8<<s29<<s1<<s4<<s0<<s5<<30<<s8<<s30 <<s1<<s4<<s0<<s5<<31<<s8<<
s31<<s1<<s4<<s0;";
string s32 = "
cout<<s5<<32<<s8<<s32<<s1<<s4<<s0<<s5<<33<<s8<<s33 <<s1<<s4<<s0<<s5<<34<<s8<<
s34<<s1<<s4<<s0;";
string s33 = "
cout<<s5<<35<<s8<<s35<<s1<<s4<<s0<<s5<<36<<s8<<s36 <<s1<<s4<<s0;";
string s34 = "
cout<<s20<<s0<<s21<<s0<<s22<<s0<<s23<<s0<<s24<<s0< <s25<<s0<<s26<<s0<<s27<<s0
<<s28<<s0;";
string s35 = "
cout<<s29<<s0<<s30<<s0<<s31<<s0<<s32<<s0<<s33<<s0< <s34<<s0<<s35<<s0<<s36<<s0
;";
string s36 = " cout<<s14<<s0<<s15<<s0;";
cout<<s10<<s0<<s11<<s0<<s12<<s0<<s13<<s0;
cout<<s5<<s8<<s<<s1<<s6<<0<<s8<<s2<<s3<<s1<<s6<<1< <s8<<s2<<s1<<s1<<s6;
cout<<2<<s8<<s2<<s2<<s1<<s6<<3<<s8<<s3<<s1<<s6<<4< <s8<<s4<<s1<<s4<<s0;
cout<<s5<<5<<s8<<s5<<s1<<s6<<6<<s8<<s6<<s1<<s6<<7< <s8<<s7<<s1;
cout<<s6<<8<<s7<<s<<7<<s9<<s<<1<<s6<<9<<s8<<s9<<s1 <<s4<<s0;
cout<<s5<<10<<s8<<s10<<s1<<s6<<11<<s8<<s11<<s1<<s4 <<s0;
cout<<s5<<12<<s8<<s12<<s1<<s6<<13<<s8<<s13<<s1<<s4 <<s0;
cout<<s5<<14<<s8<<s14<<s1<<s6<<15<<s8<<s15<<s1<<s4 <<s0;
cout<<s5<<20<<s8<<s20<<s1<<s4<<s0<<s5<<21<<s8<<s21 <<s1<<s4<<s0<<s5<<22<<s8<<
s22<<s1<<s4<<s0;
cout<<s5<<23<<s8<<s23<<s1<<s4<<s0<<s5<<24<<s8<<s24 <<s1<<s4<<s0<<s5<<25<<s8<<
s25<<s1<<s4<<s0;
cout<<s5<<26<<s8<<s26<<s1<<s4<<s0<<s5<<27<<s8<<s27 <<s1<<s4<<s0<<s5<<28<<s8<<
s28<<s1<<s4<<s0;
cout<<s5<<29<<s8<<s29<<s1<<s4<<s0<<s5<<30<<s8<<s30 <<s1<<s4<<s0<<s5<<31<<s8<<
s31<<s1<<s4<<s0;
cout<<s5<<32<<s8<<s32<<s1<<s4<<s0<<s5<<33<<s8<<s33 <<s1<<s4<<s0<<s5<<34<<s8<<
s34<<s1<<s4<<s0;
cout<<s5<<35<<s8<<s35<<s1<<s4<<s0<<s5<<36<<s8<<s36 <<s1<<s4<<s0;
cout<<s20<<s0<<s21<<s0<<s22<<s0<<s23<<s0<<s24<<s0< <s25<<s0<<s26<<s0<<s27<<s0
<<s28<<s0;
cout<<s29<<s0<<s30<<s0<<s31<<s0<<s32<<s0<<s33<<s0< <s34<<s0<<s35<<s0<<s36<<s0
;
cout<<s14<<s0<<s15<<s0;
return 0;
}
推荐答案
Emlyn> ;我很无聊,所以我只是写了一个quine(程序将在100%ansi c ++中打印出来,然后运行它自己的源代码),没有
Emlyn>使用任何c库函数(如printf),而不是制作
Emlyn>关于字符集的任何假设。
Emlyn>任何意见?错误?关于如何缩短它的想法?
这是一个C89版本,用我觉得更容易理解的形式写成。感兴趣的读者可能喜欢将其翻译成C ++:
#include< stdio.h>
char * self [] = {
" #include< stdio.h>",
"",
" char * self [] = { ",
" \ n",
" \t0",
"};",
"",
" main()",
" {",
" ; \ tchar * p,** s;",
"",
" \ tfor(s = self; * s; s ++) {",
" \t\tif(** s ==''\\ n''){",
" \ tt \ tttttt;",
" \t \ t \ tt(t = self; * t; t ++){",
" \t\t\t\tputchar(''\\\\'');",
" \ t \\ \\t \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ * p; p ++){&q uot;,
" \t\t\t\t\tif(* p ==''\\ t''){",
" \t\t\t\t\t\tputchar(''\\\\'');",
\ t \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\如果(* p ==''\"''){",
" \t \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \t\tputchar(''\\\\'');",
" \t\t\t\t\\\ \\ tttcharchar(''\"'');",
" \t\t\t\t\t}否则if(* p ==' '\\ n''){",
" \t\t\t\t\t\tputchar(''\\\ \'');",
" \t\t\t\t\t\tputchar(''n'');",
" \t\t\t\t\t}否则如果(* p ==''\\\\''){",
" \t\t\t\t\t\tputchar (''\\\\'');",
" \t\t\t\t\t\tputchar(''\\' \\\\\'');",
" \t\t\t\t\t} else",
" \t\t\t\t\t\tputchar(* p);",
" \t\t\\ t}",
" \t\t\t\tputchar(''\"'');",
" ; \t\t\t\tputchar('','');",
" \t\t\t\tputchar(''\\ \\\ n'');",
" \t\t\t}",
" \t\ t } else {",
" \t\t\tfor(p = * s; * P; p ++){",
" \t\t\t\tputchar(* p);",
" \ t \\ \\ t \},
" \t\t\tputchar(''\\ n'');",
" \t\t}",
" \t}",
" \treturn 0;",
"}",
0
};
main()
{
char * p,** s;
for(s = self; * s; s ++){
if(** s ==''\ n''){
char ** t;
for(t = self; * t; t ++ ){
putchar(''\t'');
putchar(''"'');
for(p = * t; * p; p ++){
if(* p ==''\t''){
putchar(''\\' ');
putchar(''t'');
}否则if(* p ==''"''){
putchar(''\\'');
putchar(''"'');
}否则if(* p ==' '\ n''){
putchar(''\\'');
putchar(''n'');
}否则if(* p ==''\\''){
putchar(''\\'');
putchar (''\\'');
}其他
putchar(* p);
}
putchar(''"'');
putchar('','');
putchar(''\ n'');
}
}否则{
for(p = * s; * P; p ++){
putchar(* p);
}
putchar(''\ n'');
}
}
返回0;
}
-
Andrew Koenig, ar*@acm.org
Emlyn> I was bored, so I just wrote a quine (program that will print
Emlyn> out it''s own source code when run) in 100% ansi c++, without
Emlyn> using any c-library functions (such as printf), and not making
Emlyn> any assumptions on the character set.
Emlyn> Any comments? bugs? ideas on how to shorten it?
Here''s a C89 version, written in a form that I find easier to
understand. The interested reader may enjoy translating it to C++:
#include <stdio.h>
char *self[] = {
"#include <stdio.h>",
"",
"char *self[] = {",
"\n",
"\t0",
"};",
"",
"main()",
"{",
"\tchar *p, **s;",
"",
"\tfor (s = self; *s; s++) {",
"\t\tif (**s == ''\\n'') {",
"\t\t\tchar **t;",
"\t\t\tfor (t = self; *t; t++) {",
"\t\t\t\tputchar (''\\t'');",
"\t\t\t\tputchar (''\"'');",
"\t\t\t\tfor (p = *t; *p; p++) {",
"\t\t\t\t\tif (*p == ''\\t'') {",
"\t\t\t\t\t\tputchar (''\\\\'');",
"\t\t\t\t\t\tputchar (''t'');",
"\t\t\t\t\t} else if (*p == ''\"'') {",
"\t\t\t\t\t\tputchar (''\\\\'');",
"\t\t\t\t\t\tputchar (''\"'');",
"\t\t\t\t\t} else if (*p == ''\\n'') {",
"\t\t\t\t\t\tputchar (''\\\\'');",
"\t\t\t\t\t\tputchar (''n'');",
"\t\t\t\t\t} else if (*p == ''\\\\'') {",
"\t\t\t\t\t\tputchar (''\\\\'');",
"\t\t\t\t\t\tputchar (''\\\\'');",
"\t\t\t\t\t} else",
"\t\t\t\t\t\tputchar (*p);",
"\t\t\t\t}",
"\t\t\t\tputchar (''\"'');",
"\t\t\t\tputchar ('','');",
"\t\t\t\tputchar (''\\n'');",
"\t\t\t}",
"\t\t} else {",
"\t\t\tfor (p = *s; *p; p++) {",
"\t\t\t\tputchar (*p);",
"\t\t\t}",
"\t\t\tputchar (''\\n'');",
"\t\t}",
"\t}",
"\treturn 0;",
"}",
0
};
main()
{
char *p, **s;
for (s = self; *s; s++) {
if (**s == ''\n'') {
char **t;
for (t = self; *t; t++) {
putchar (''\t'');
putchar (''"'');
for (p = *t; *p; p++) {
if (*p == ''\t'') {
putchar (''\\'');
putchar (''t'');
} else if (*p == ''"'') {
putchar (''\\'');
putchar (''"'');
} else if (*p == ''\n'') {
putchar (''\\'');
putchar (''n'');
} else if (*p == ''\\'') {
putchar (''\\'');
putchar (''\\'');
} else
putchar (*p);
}
putchar (''"'');
putchar ('','');
putchar (''\n'');
}
} else {
for (p = *s; *p; p++) {
putchar (*p);
}
putchar (''\n'');
}
}
return 0;
}
--
Andrew Koenig, ar*@acm.org
Emlyn Corrin写道:
Emlyn Corrin wrote:
我很无聊,所以我只是在100%ansi c ++中编写了一个quine(程序将在运行时打印出它自己的源代码),而不使用任何c-library
函数(如printf),而不对字符集进行任何假设。
有何评论?错误?关于如何缩短它的想法?
Emlyn
I was bored, so I just wrote a quine (program that will print out it''s own
source code when run) in 100% ansi c++, without using any c-library
functions (such as printf), and not making any assumptions on the character
set.
Any comments? bugs? ideas on how to shorten it?
Emlyn
这是我的尝试:)我很确定这是标准的C ++,我还有
怀疑那些拥有更多C ++知识的人可以缩短它的时间。
me。如果这被你的新闻阅读器吃掉,请注意我没有
多行字面意思:)
唯一的小问题是我怀疑我做不了
b.insert(它,''\\''),因为这会使它无效(这对我来说意味着
之后插入物品会很明智*叹气*)
,所以我应该制作一个临时的减量副本,但我不能
打扰:)我也可以硬连接a.insert线和b.reserve
线,但是现在想让它们保持通用:)
#include< string>
#include< iostream>
using namespace std;
string a(" #include< string> \ n#include< iostream> \ nusing namespace
std \ nstring a(\" X \")int main(){\ nstring
b = a; \ nb.reserve(b.length()* 2); \ nstring :: iterator
it = b.begin(); \ nwhile(it!= b.end()){\ nif(* it ==''\ n''){* it =''n'';
b.insert(it,''\\ \\\'');} \ nit ++; \ n} \ nna.insert(find(ab egin(),a.end(),''X''),b.begin( ),b.end()); \\\
cout<< A<< endl; \ n} \ n");
int main(){
string b = a;
b.reserve( b.length()* 2);
string :: iterator it = b.begin();
while(it!= b.end()){
if(* it ==''\ n''){* it =''n''; b.insert(it,''\\'');}
it ++;
}
a.insert(find( a.begin(),a.end(),''X''),b.begin(),be nd());
cout<< a<< endl;
}
Here is my attempt :) I''m fairly certain is this standard C++, I also
suspect it can be made shorter by someone with more C++ knowledge than
me. If this gets eaten by your newsreader, note that I don''t have a
multi-line literal :)
The only minor point is that I suspect that I can''t do
b.insert(it,''\\''), as this will invalidate it (which suggests to me it
would have been sensible to have insert put things in afterwards *sigh*)
, so I should perhaps make a temporary decremented copy, but I can''t be
bothered :) Also I could hard-wire the a.insert line, and the b.reserve
line, but wanted to keep them general for now :)
#include<string>
#include<iostream>
using namespace std;
string a("#include<string>\n#include<iostream>\nusing namespace
std\nstring a(\"X\")int main(){\nstring
b=a;\nb.reserve(b.length()*2);\nstring::iterator
it=b.begin();\nwhile(it!=b.end()) {\nif(*it==''\n'') {*it=''n'';
b.insert(it,''\\\\'');}\nit++;\n}\na.insert(find(a.b egin(),a.end(),''X''),b.begin(),b.end());\ncout<<a<< endl;\n}\n");
int main(){
string b=a;
b.reserve(b.length()*2);
string::iterator it=b.begin();
while(it!=b.end()) {
if(*it==''\n'') {*it=''n''; b.insert(it,''\\'');}
it++;
}
a.insert(find(a.begin(),a.end(),''X''),b.begin(),b.e nd());
cout<<a<<endl;
}
我希望新闻阅读器不要太过混乱。
f的定义没有换行符。
Owner @ machine~ / projects / quine
I hope the newsreader doesn''t mess this up too much. There are no newlines in
the definition of f.
Owner@machine ~/projects/quine
这篇关于C ++ quine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!