-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_self.cpp
More file actions
64 lines (62 loc) · 2.1 KB
/
print_self.cpp
File metadata and controls
64 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <stdio.h>
#define Q(x) #x
// This is a C++ program to print its own source code based on
// the recursion theorem in Sipser's book _Theory_Of_Computation_.
// This program was compiled and tested on LINUX 6.0 with egcs-2.91.66.
// Permission is granted to copy, modify, and distribute provided
// this comment is maintained at the top. Emin Martinian, 10/2000.
const char*const MakeProgramToPrintString(const char*const s) {
char quote[2];
char newline[2];
sprintf(quote,Q(%c),34);
sprintf(newline,Q(%c),10);
quote[1] = 0;
newline[1] = 0;
std::string prog(Q( ));
prog = prog + Q(int main() {) + newline;
prog = prog + Q(string tape;) + newline;
prog = prog + Q(tape = tape +) + quote + s + quote + Q(;) + newline;
prog = prog + Q(DoPartB(tape.c_str());) + newline;
prog = prog + Q(return 0;) + newline + Q(}) + newline;
return prog.c_str();
}
void DoPartB(const char*const s) {
std::cout << s << MakeProgramToPrintString(s);
}
int main() {
std::string tape;
tape = tape +"#include <iostream.h>¥
#include <string>¥
#include <stdio.h>¥
#define Q(x) #x¥
// This is a C++ program to print its own source code based on¥
// the recursion theorem in Sipser's book _Theory_Of_Computation_.¥
// This program was compiled and tested on LINUX 6.0 with egcs-2.91.66.¥
// Permission is granted to copy, modify, and distribute provided¥
// this comment is maintained at the top. Emin Martinian, 10/2000.¥
const char*const MakeProgramToPrintString(const char*const s) {¥
char quote[2];¥
char newline[2];¥
sprintf(quote,Q(%c),34);¥
sprintf(newline,Q(%c),10);¥
quote[1] = 0;¥
newline[1] = 0;¥
string prog(Q( ));¥
prog = prog + Q(int main() {) + newline;¥
prog = prog + Q(string tape;) + newline;¥
prog = prog + Q(tape = tape +) + quote + s + quote + Q(;) + newline;¥
prog = prog + Q(DoPartB(tape.c_str());) + newline;¥
prog = prog + Q(return 0;) + newline + Q(}) + newline;¥
return prog.c_str();¥
}¥
¥
void DoPartB(const char*const s) {¥
std::cout << s << MakeProgramToPrintString(s);¥
}¥
¥
";
DoPartB(tape.c_str());
return 0;
}