C++程序,其输出是两个C++程序
C++ program whose output is two C++ programs?
我现在的任务是编写一个程序,该程序以一些文本规范为输入,用于将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> << std::endl;</xsl:text></xsl:variable>
<xsl:variable name="so"><xsl:text> << </xsl:text></xsl:variable>
<xsl:variable name="si"><xsl:text> >> </xsl:text></xsl:variable>
<xsl:variable name="nl"><xsl:text>
</xsl:text></xsl:variable>
<!-- Root node -->
<xsl:template match="/">
<xsl:text>
#include <iostream>
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++代码!这是一个琐碎的例子。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址