0

(完整的问题列在底部)

我有一个任务要求我将文本写入后记文件,该文件允许我使用递归绘制“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,其中包含对项目至关重要的“绘图”功能。

这是我的 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

Gosper.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
}

海龟.h:

#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

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

I want to write the text for every Gosper class object in GosperDriver.cpp into one postscript file. As it is right now, any attempt to do that will result in the previous block of text in the designated output.ps to be overwritten. At the moment, I can only write the text necessary for ONE Gosper class object. I have had to comment out every Gosper object declaration in Gosperdriver.cpp but one, in order to test if my program is working correctly.

In short, I need to write the text necessary to output.ps for every Gosper object in GosperDriver.cpp, but it isn't working because it will only let me write for one at a time. What do I do?


Bonus question about inheritance: right now, my "starting point" for each Gosper drawing keeps being set at x = 0 and y = 0. As seen by the Gosper object declarations, none of the parameters contain 0 for x or y. Something's gone wonky. What's happening?

Thanks in advance to anyone who can answer one or both of these questions! :)

4

1 回答 1

0

You can use

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

to open the file in append mode. Meaning old content will not be overwritten. You will however need to add something to detect if the header out << "%!PS-Adobe-2.0" << endl; has already been written. (I assume you need that exactly once per file.)

To avoid opening and closing the file all the time you could also create a separate class that will open the file, initialize it (write the header) and then use this class to write all your contents and close the file afterwards.

For bonus points use RAII to make the class automatically take care of the file.

于 2013-10-27T00:35:27.727 回答