自我再现程序 [英] Self-reproducing program

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

问题描述

我质疑我在Accelerated C ++中的最后一个练习的解决方案:

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.

我的解决方案:

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.

这是可能在C ++?如果是,如何?

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

推荐答案

打印出来的程序称为 Quine

我认为你的解决方案不会被视为有效:quines通常不允许读取文件(也不能获得任何其他类型的输入)。可以写一个Quine C ++程序,这里你可以找到很多种语言的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天全站免登陆