简体   繁体   English

使用几个类对象将文本写入postscript文件

[英]Writing text to a postscript file using several class objects

(Full question is listed at the bottom) (完整的问题在底部列出)

I have an assignment that requires me to write text to a postscript file that allows me to draw "Gosper" curves using recursion. 我有一项作业,要求我将文字写入后记文件,该文件允许我使用递归绘制““语”曲线。 However, the test driver (GosperDriver.cpp) my professor has given us resembles the following: 但是,我的教授给我们的测试驱动程序(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 includes Turtle.h, which contains the "draw" functions which are vital to the project. Gosper.h包含Turtle.h,其中包含对项目至关重要的“绘制”功能。

Here are my Gosper.h, Gosper.cpp, Turtle.h, and Turtle.cpp files, in that order (I'll cut out the unnecessary code, which controls drawing): 这是我的Gosper.h,Gosper.cpp,Turtle.h和Turtle.cpp文件(按顺序排列)(我将剪切出不必要的代码,以控制绘图):

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

Turtle.h: Turtle.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

};

Turtle.cpp: Turtle.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

Okay, so now that you've seen my code, here's my problem: 好的,既然您已经看到了我的代码,这就是我的问题:

I want to write the text for every Gosper class object in GosperDriver.cpp into one postscript file. 我想将GosperDriver.cpp中每个Gosper类对象的文本写到一个脚本文件中。 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. 目前,任何尝试这样做都会导致指定的output.ps中的上一个文本块被覆盖。 At the moment, I can only write the text necessary for ONE Gosper class object. 目前,我只能编写一个Gosper类对象所需的文本。 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. 为了测试程序是否正常运行,我不得不注释掉Gosperdriver.cpp中的每个Gosper对象声明,但要声明一个。

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. 简而言之,我需要为GosperDriver.cpp中的每个Gosper对象编写对output.ps必需的文本,但是它不起作用,因为它一次只能写一个。 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. 有关继承的附加问题:现在,每个Gosper绘图的“起点”始终设置为x = 0和y =0。从Gosper对象声明可以看出,x或y的参数都不包含0。 Something's gone wonky. 事情变得不妙了。 What's happening? 发生了什么?

Thanks in advance to anyone who can answer one or both of these questions! 在此先感谢所有可以回答其中一个或两个问题的人! :) :)

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; 但是,您将需要添加一些东西来检测标题是否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. 对于奖励积分,请使用RAII来使班级自动照顾文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM