繁体   English   中英

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

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

(完整的问题在底部列出)

我有一项作业,要求我将文字写入后记文件,该文件允许我使用递归绘制““语”曲线。 但是,我的教授给我们的测试驱动程序(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
}

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:

#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.cpp中每个Gosper类对象的文本写到一个脚本文件中。 目前,任何尝试这样做都会导致指定的output.ps中的上一个文本块被覆盖。 目前,我只能编写一个Gosper类对象所需的文本。 为了测试程序是否正常运行,我不得不注释掉Gosperdriver.cpp中的每个Gosper对象声明,但要声明一个。

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


有关继承的附加问题:现在,每个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来使班级自动照顾文件。

暂无
暂无

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

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