PrintSelf
I wrote the program for those who is
interesting in curious things and programming puzzles. It's a classic problem -
to write a program, which is printing a source code of its own and using only
output functions
|
program PrintSelf;
{$APPTYPE CONSOLE}
const
PrgText: array [1..21] of string = (
'program PrintSelf;',
'{$APPTYPE CONSOLE}',
'const',
'PrgText: array [1..21] of string = (',
');',
'var I: Integer;',
'begin',
'for I := 1 to 4 do begin',
'writeln(PrgText[I]);',
'end;',
'',
'for I := 1 to 20 do begin',
'writeln(chr(39)+PrgText[I]+chr(39)+chr(44));',
'end;',
'writeln(chr(39)+PrgText[21]+chr(39));',
'',
'for I := 5 to 21 do begin',
'writeln(PrgText[I]);',
'end;',
'end.',
''
);
var I: Integer;
begin
for I := 1 to 4 do begin
writeln(PrgText[I]);
end;
for I := 1 to 20 do begin
writeln(chr(39)+PrgText[I]+chr(39)+chr(44));
end;
writeln(chr(39)+PrgText[21]+chr(39));
for I := 5 to 21 do begin
writeln(PrgText[I]);
end;
end.
|
The shortest version.
The shortest version of the program, which I've ever seen, was sent to me by
Andrew P.Rybin . The general idea is: to declare a string
with program text and to use the substring function to output the program text.
|
const S='const S=;begin
Write(Copy(S,1,8),#39,S,#39,Copy(S,9,99));end.'; begin Write(Copy,S,1,8),#39,S,#39,Copy(S,9,99));end.
To compile the program use the command-line compiler. For
example:
dcc32 self.dpr -cc
|
|