C++程序,其输出是两个C++程序

C++ program whose output is two C++ programs?

本文关键字:C++ 程序 两个 输出      更新时间:2023-10-16

我现在的任务是编写一个程序,该程序以一些文本规范为输入,用于将64位指令压缩为32位指令。根据该规范,我将构建两个可执行程序:一个编码器和一个解码器。

目前,我只是在编写一个解析器类来标记我设计的文本规范,但最终我必须将获得的信息转化为两个新程序。我知道我能做到这一点的唯一方法是使用ofstream打印到一个新的.cpp文件,然后使用system('g++ new_file.cpp -o new.x')创建可执行文件。然后也许system('rm new_file.cpp')来清理。

我四处寻找其他方法来做到这一点,但一无所获。如果你有什么建议,我将不胜感激。

感谢

p.s.我没有包含任何代码,因为这些代码无关紧要。为了简单起见,我的目标可能是编写一个输出为"Hello,World!"式可执行文件的程序。

让您的程序执行以下操作:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
    {
    ofstream out( "a.cpp" );
    out << "#include <iostream>nint main() { std::cout << "hello world\n"; } n";
    }
    system( "g++ a.cpp -o hello" );
}

刚刚测试了这个——它确实产生了一个编译的helloworld程序。

嗯,对于一些XML和XSLT来说,这听起来是一项不错的任务。使用XML定义您想要的内容,使用XSLT生成实际的C++代码。

编辑:对于那些被否决的人:

<?xml version="1.0" encoding="UTF-8" ?>
<proggy>
 <prompt input='uname' type='std::string'>
   <text>Name: </text>
   <output><text>Hello </text><field name="uname"/><text>!</text></output>
 </prompt>
</proggy>

将其转换为C++的XSL

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" indent="no"/>
  <xsl:variable name="co"><xsl:text>std::cout</xsl:text></xsl:variable>
  <xsl:variable name="ci"><xsl:text>std::cin</xsl:text></xsl:variable>
  <xsl:variable name="e"><xsl:text> &lt;&lt; std::endl;</xsl:text></xsl:variable>
  <xsl:variable name="so"><xsl:text> &lt;&lt; </xsl:text></xsl:variable>
  <xsl:variable name="si"><xsl:text> &gt;&gt; </xsl:text></xsl:variable>
  <xsl:variable name="nl"><xsl:text>&#xa;</xsl:text></xsl:variable>
  <!-- Root node -->
  <xsl:template match="/">
    <xsl:text>
#include &lt;iostream&gt;
int main(void)
{
    </xsl:text>
    <xsl:apply-templates select="//proggy"/>
    <xsl:text>
  return 0;
}
    </xsl:text>
  </xsl:template>
  <xsl:template match="proggy">
    <xsl:apply-templates select="prompt"/>
  </xsl:template>
  <xsl:template match="prompt">
    <xsl:value-of select="$co"/><xsl:value-of select="$so"/>
    <xsl:text>"</xsl:text><xsl:value-of select="text"/><xsl:text>"</xsl:text>
    <xsl:value-of select="$e"/><xsl:value-of select="$nl"/>
    <xsl:value-of select="@type"/><xsl:text> </xsl:text><xsl:value-of select="@input"/><xsl:text>;</xsl:text>
    <xsl:value-of select="$nl"/>
    <xsl:text>if (</xsl:text>
    <xsl:value-of select="$ci"/><xsl:value-of select="$si"/><xsl:value-of select="@input"/>
    <xsl:text>){</xsl:text>
    <xsl:value-of select="$nl"/>
    <xsl:apply-templates select="output"/>
    <xsl:value-of select="$nl"/>
    <xsl:text>}</xsl:text>
  </xsl:template>
  <xsl:template match="output">
    <xsl:value-of select="$co"/>
    <xsl:apply-templates select="./*" mode="so"/>
    <xsl:value-of select="$e"/>
  </xsl:template>
  <xsl:template match="text" mode="so">
    <xsl:value-of select="$so"/><xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text>
  </xsl:template>
  <xsl:template match="field" mode="so">
    <xsl:value-of select="$so"/><xsl:value-of select="@name"/>
  </xsl:template>
</xsl:stylesheet>

我挑战你写一个更小的C++程序来生成相同的C++代码!这是一个琐碎的例子。