Quine (BASIC)

From LiteratePrograms

(Redirected from Quine (QBASIC))
Jump to: navigation, search
Other implementations: BASIC | Clojure | dc | Erlang | Forth | JavaScript / URI | Lisp | Oz | Perl | Python | Smalltalk

A "Quine" is a program that prints its own source code when it is run. See Wikipedia's article on Quines for more information, and David Madore's Page for a highly readable introduction to the art of constructing Quines.

Two line quine

<<QuineA.bas>>=
10 LET Q$ = "20 PRINT CHR$(49) + CHR$(48) + CHR$(32) + CHR$(76) + CHR$(69) + CHR$(84) + CHR$(32) + CHR$(81) + CHR$(36) + CHR$(32) + CHR$(61) + CHR$(32) + CHR$(34) + Q$ + CHR$(34): PRINT Q$"
20 PRINT CHR$(49) + CHR$(48) + CHR$(32) + CHR$(76) + CHR$(69) + CHR$(84) + CHR$(32) + CHR$(81) + CHR$(36) + CHR$(32) + CHR$(61) + CHR$(32) + CHR$(34) + Q$ + CHR$(34): PRINT Q$

Single line quines

The first of these is similar to the above but leaves out the optional LET keyword.

<<QuineB.bas>>=
10 Q$ = "PRINT CHR$(49) + CHR$(48) + CHR$(32) + CHR$(81) + CHR$(36) + CHR$(32) + CHR$(61) + CHR$(32) + CHR$(34) + Q$ + CHR$(34) + Q$ + CHR$(58) + CHR$(32) + Q$": PRINT CHR$(49) + CHR$(48) + CHR$(32) + CHR$(81) + CHR$(36) + CHR$(32) + CHR$(61) + CHR$(32) + CHR$(34) + Q$ + CHR$(34) + CHR$(58) + CHR$(32) + Q$

The second uses the standard approach of creating a quine function and then using it.

<<QuineC.bas>>=
10 DEF FNQ$ (T$) = T$ + CHR$(40) + CHR$(34) + T$ + CHR$(34) + CHR$(41): PRINT FNQ$("10 DEF FNQ$ (T$) = T$ + CHR$(40) + CHR$(34) + T$ + CHR$(34) + CHR$(41): PRINT FNQ$")

The first two of these follow the standard quining approach of printing out the quoted version of a printing section followed by the printing section itself. Note that you will have to remove the REMarks which the Download Code link adds to the above programs in order to have them work as advertised.

Download code
Views