使用多个类对象将文本写入后脚本文件

Writing text to a postscript file using several class objects

本文关键字:脚本 文件 文本 对象      更新时间:2023-10-16

(完整问题列在底部)

有一个作业,要求我将文本写入后记文件,该文件允许我使用递归绘制"Gosper"曲线。但是,我的教授给我们的测试驱动程序(GosperDriver.cpp)类似于以下内容:

#include "Gosper.h"
#include <iostream>
using namespace std;
int main( )
{   
// test right hexagonal Gosper curve at level 4
Gosper gosper1( 100, 100, 0 );
gosper1.rightCurve( 4, 4 ); 
// test left hexagonal Gosper curver at level 4
Gosper gosper2( 500, 100, 0 );
gosper2.leftCurve( 4, 4 );
// test right hexagonal Gosper curve at level 3
Gosper gosper3( 100, 400, 0 );
gosper3.rightCurve( 3, 6 );
// test left hexagonal Gosper curver at level 3
Gosper gosper4( 500, 400, 0 );
gosper4.leftCurve( 3, 6 );
// test right hexagonal Gosper curve at level 2
Gosper gosper5( 100, 600, 0 );
gosper5.rightCurve( 2, 8 );
// test left hexagonal Gosper curver at level 2
Gosper gosper6( 500, 600, 0 );
gosper6.leftCurve( 2, 8 );
// test right hexagonal Gosper curve at level 1
Gosper gosper7( 100, 700, 0 );
gosper7.rightCurve( 1, 10 );
// test left hexagonal Gosper curver at level 1
Gosper gosper8( 500, 700, 0 );
gosper8.leftCurve( 1, 10 );
}

Gosper.h包括Turtle.h,它包含对项目至关重要的"draw"函数。

以下是我的Gosper.h,Gosper.cpp,Turtle.h和Turtle.cpp文件,按顺序排列(我将删除不必要的代码,它控制绘图):

Gosper.h:

// Sierpinski Class 
#ifndef GOSPER_H
#define GOSPER_H
#include "Turtle.h"
#include <iostream>
#include <fstream>

using namespace std;
class Gosper : Turtle 
{
public:
    Gosper(float initX=0.0, float initY=0.0, float initA=0.0);
    void leftCurve( int l, float d );  // Draw level l left curve with dist d
    void rightCurve( int l, float d ); // Draw level l right curve with dist d
};
#endif

高斯珀.cpp:

#include <iostream>
#include <string>
#include "Gosper.h"
// Initialization and such.
Gosper::Gosper(float initX, float initY, float initA)
{
}

void Gosper::leftCurve(int level, float d)
{
    // Code that uses draw() function of Turtle.h and Turtle.cpp
}

void Gosper::rightCurve(int level, float d)
{
    // Same as above
}

#ifndef TURTLE_H
#define TURTLE_H
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
const float PI = 3.1459265;
class Turtle {
public:
    Turtle(float initX = 0.0, float initY = 0.0, float initA = 0.0);
    ~Turtle();
    void draw( float d );   // draw line by distance d
    void move( float d );   // simply move by distance d
    void turn( float a );   // turn by angle a
private:
    ofstream out;
    float angle;    // current angle
};

.cpp:

#include "Turtle.h"
#include <iostream>
#include <fstream>
Turtle::Turtle(float initX, float initY, float initA)
{
out.open("output.ps");
out << "%!PS-Adobe-2.0" << endl;
out << initX << "t" << initY << "tmoveto" << endl;
angle = initA;
}
Turtle::~Turtle() 
{
out << "stroke" << endl;
out << "showpage" << endl;
}
void Turtle::turn(float a)
{
angle += a;
}
void Turtle::draw(float d)
{
float dX, dY;
dX = d * cos(PI * angle / 180);
dY = d * sin(PI * angle / 180);
out << dX << "t" << dY << "trlineto" << endl;
}
void Turtle::move(float d)
{
float dX, dY;
dX = d * cos(PI * angle / 180);
dY = d * sin(PI * angle / 180);
out << dX << "t" << dY << "trmoveto" << endl;
}
#endif

好的,现在你已经看到了我的代码,这是我的问题:

我想将GosperDriver中的每个Gosper类对象的文本写入一个postscript文件中.cpp。就像现在一样,任何这样做的尝试都将导致覆盖指定 output.ps 中的前一个文本块。目前,我只能编写一个Gosper类对象所需的文本。我不得不注释掉 Gosperdriver 中的每个 Gosper 对象声明.cpp但只有一个,以便测试我的程序是否正常工作。

简而言之,我需要编写必要的文本来 output.ps GosperDriver中的每个Gosper对象.cpp,但它不起作用,因为它一次只允许我编写一个。我该怎么办?


关于继承的奖励问题:现在,我对每张 Gosper 绘图的"起点"一直设置为 x = 0 和 y = 0。从 Gosper 对象声明中可以看出,对于 x 或 y,没有一个参数包含 0。有些事情变得不稳定。发生了什么事情?

提前感谢任何可以回答其中一个或两个问题的人! :)

您可以使用

out.open("output.ps", std::fstream::in | std::fstream::out | std::fstream::app);

以追加模式打开文件。这意味着旧内容不会被覆盖。但是,您需要添加一些内容来检测标头是否 out << "%!PS-Adobe-2.0" << endl;已经写好了。(我假设每个文件只需要一次。

为了避免一直打开和关闭文件,

您还可以创建一个单独的类来打开文件,初始化它(写入标头),然后使用此类写入所有内容并在之后关闭文件。

对于奖励积分,请使用 RAII 使班级自动处理文件。