A simple way to write a program that *computes* and outputs its own source code.
The actually program itself is not posted but the entire implementation idea is explained - hopefully this encourages people to write the program on their own.
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Quine - A program that computes & prints its own source code
1. Write a program that prints itself By Kiran Bhogadi http://en.wikipedia.org/wiki/Drawing_Hands
2. One simple rule Don’ts : The program can not read its own source code file and output it. Do’s : The program is set up to compute its own source code and output it.
4. A preliminary approach In your program : A and B could be two different functions/methods Or A and B could each be just a line of code Assume that execution starts in A and ends with B START END A B
5. A First Implementation Idea A and B are two methods The method A() has a copy of method B()’s code (as a literal string). A() prints this string and then calls B() A “<B>”
6. A First Implementation Idea (contd.) Tempting to design method B() to contain the code of method A as a literal string too! But that won’t work! Circular Dependency
7. A better approach Method A has a string literal copy of Method B code and it simply prints it. Method B will compute the string the represents Method A’s code A “<B>”
8. Method A’s code – contains Method B as a string literal Let’s say the method A is written like this : Method A () { Print(“<Code_for_Method_B>”) }
9. Can we compute the code for Method A … … given the code in the “box” i.e., “<Code_for_Method_B> ? YES!!
10. Computing Method A’s code given <Code_for_Method_B> as input The code (method) that computes Method A’s code : Input_String S For e.g. “<Code_for_Method_B>” Returns : “Method A () {” + NEWLINE + “Print(” + Input_String S + “)” + NEWLINE + “}” Note the returned string represents Method A’s code! Method A () { Print(“<Code_for_Method_B>”) }
11. So how does the code for Method B look like? See the previous slide!
12. Putting it All together Method A () { Print(<Code_for_Method_B>); Call B(…); } <Code_for_Method_B>is a string literal representing Method B’s code : Method B(Input String S) { String returnValue = “Method A () {” + NEWLINE + “Print(” + Input_String S + “);” + NEWLINE + “Call B(…);” + NEWLINE + “}”; Print returnValue; }
13. What happens when the program runs ? Execution starts with Method A() A() outputs the code (“literal”) for Method B A() calls B() B() computes the code for Method A and outputs it Method A()Method B() Step 2: calls Step 3 : outputs computed value Step 1 : outputs literal B A
14. Some Technicalities / Questions If the original program file contained the methods A and B in this order: then the output from the program contained the methods in reverse order But this can be fixed easily! A B B A