簡體   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