Can a program output a copy of itself

2019-02-05 19:27发布

I think this might be a classic question but I am not aware of an answer. Can a program output a copy of itself, and, if so, is there a short program that does this?

I do not accept the "empty program" as an answer, and I do not accept programs that have access to there own source code. Rather, I am thinking something like this:

int main(int argc, char** argv){ printf("int main(argc, char** argv){ printf...

but I do not know how to continue...

11条回答
手持菜刀,她持情操
2楼-- · 2019-02-05 20:20

I assume you allow interpreted languages. (At some level, all languages are interpreted.) Somebody writes the interpreter, and if you are writing it, you can add to it any built-in functions you like, such as a (lispy) function (foo) that does nothing except print "(foo)".

Or you can add a more complex macro-type function (printMeAndMyArgs ...).

So the trick is in how you define the problem.

查看更多
Melony?
3楼-- · 2019-02-05 20:26

The basic idea of most quines is:

  1. You write code that takes a string literal s and prints it, while replacing occurrences (or the occurrence) of a special substring foo in s by the value of s itself.

  2. You take the entire source code of the program so far and use it as the definition for s. but you exclude the definition of s from the string, instead replacing it by foo.

Well, that's the general idea. The rest is string formatting details, really.

查看更多
等我变得足够好
4楼-- · 2019-02-05 20:26

Michael Sipser’s “Introduction to the Theory of Computation” explains in one of the chapters how to construct a quine. I have recently written a Java program based on that idea and posted it at : http://bornagainprogrammer.net/2009/11/07/hello-world-from-the-tm-self/

I'd suggest you get hold of that book and try implementing the program yourself in your favorite language. There are lot of other fun theorems in that book.

-kiran

查看更多
看我几分像从前
5楼-- · 2019-02-05 20:26

It is possible in Java, but with some constraints.

I have written a simple code in java which prints itself. You can use literals of C/C++ to use the same program. You can add anything you want inside this program, it will print itself completely.

Conditions

  1. Java file should not be inside any package

  2. Folder structure should not contain any folders with spaces in its name

  3. Compilation target should be default or same folder where java file resides

    import java.io.FileInputStream;
    import java.net.URL;
    
    
    public class PrintYourself {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            URL location = PrintYourself.class.getProtectionDomain().getCodeSource().getLocation();
            String path=location.getFile();
            path=path.replace("/bin", "/src");
            System.out.println(path);
    
            try{
                FileInputStream st=new FileInputStream(path+"PrintYourself.java");
                int i=0;
                while((i=st.read())!=-1){
                    System.out.print((char)i);
                }
                st.close();
            }
            catch(Exception e){
                System.out.println(e);
            }
    
        }
    }
    
查看更多
Lonely孤独者°
6楼-- · 2019-02-05 20:31

Yes. Here's a C program that does it that I wrote about 20 years ago.

http://womencht.reocities.com/Athens/8994/repeat.html

查看更多
登录 后发表回答