自我复制程序 [英] Self-reproducing program

查看:293
本文介绍了自我复制程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


编写一个自我重现程序。这样的程序是没有输入的程序,运行时,在标准输出流中写入自己的源文本的副本。


我的解决方案:

  using std :: string; 
使用std :: cout;
使用std :: endl;
使用std :: ifstream;
使用std :: getline;

void selfReproduce16_1()
{
ifstream thisFile(C:\\Users\\Kevin\\\Documents\\NetBeansProjects\\\ \\ Accelerated_C ++ _ Exercises\\\Chapter_16.cpp,ifstream :: in);

string curline;

bool foundHeader = false;

while(getline(thisFile,curLine))
{
if(!curLine.compare(void selfReproduce16_1())|| foundHeader)
{
foundHeader = true;
cout<<< curline<< ENDL;
}

}

}

这只打印出解决方案的源文本(此函数)。这是他们想到的解决方案吗?



我想要一个动态解决方案,不需要硬编码源文件的位置。但是,我不知道在运行时自动获取源文件的位置的方法。



与此相关的另一点是包含包含文件,(当遇到函数调用时),自动获取该函数的源文件的位置对我来说,这将是一个真正的自我复制程序。



这是否可能在C ++中?如果是这样,那么怎么样?

解决方案

打印自己的程序称为 Quine



我认为你的解决方案不被认为是有效的:不允许读取文件(也不得到任何其他类型的输入)。可以写一个Quine C ++程序, here 你可以在几个例子中找到很多的quine实现语言。


I am questioning my solution to the last exercise in Accelerated C++:

Write a self-reproducing program. Such a program is one that does no input, and that, when run, writes a copy of its own source text on the standard output stream.

My solution:

using std::string;
using std::cout;
using std::endl;
using std::ifstream;
using std::getline;

void selfReproduce16_1()
{
    ifstream thisFile("C:\\Users\\Kevin\\Documents\\NetBeansProjects\\Accelerated_C++_Exercises\\Chapter_16.cpp", ifstream::in);

    string curLine;

    bool foundHeader = false;

    while(getline(thisFile, curLine))
    {
        if(!curLine.compare("void selfReproduce16_1()") || foundHeader)
        {
            foundHeader = true;
            cout << curLine << endl;
        }

    }

}

This only prints out the source text of the solution (this function). Is this the solution that they had in mind?

I would like a dynamic solution that does not require hard-coding the location of the source file. However, I am not aware of a way to get the location of a source file automatically during runtime.

Another point related to that is the inclusion of "included" files, and (when encountering a function call), automatically obtaining the location of the source file that the function is stored in. To me, this would be a true "self-reproducing" program.

Is this possible in C++? If so, how?

解决方案

A program that prints itself is called Quine.

I think your solution wouldn't be considered valid: quines usually aren't allowed to read files (nor to get any other kind of input). It's possible to writ a Quine C++ program, here you could find many quine implementations in several languages.

这篇关于自我复制程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆