2013-10-26 21 views
0

(全問題在底部列出)寫作文使用若干類PostScript文件對象

我有一個需要我寫文本到PostScript文件,允許我用畫「高斯帕」曲線賦值遞歸。然而,測試驅動程序(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到一個文本後記文件。現在,任何試圖這樣做都會導致指定output.ps中的前一個文本塊被覆蓋。目前,我只能編寫一個Gosper類對象所需的文本。我不得不將Gosperdriver.cpp中的每個Gosper對象聲明註釋掉,但爲了測試我的程序是否正常工作,還需要註釋一個。

總之,我需要爲GosperDriver.cpp中的每個Gosper對象輸出output.ps所需的文本,但它不起作用,因爲它只能讓我一次編寫一個。我該怎麼辦?關於繼承


獎金的問題:現在,對於每一個高斯帕圖我的「起點」保持被設定在x = 0和y = 0。正如所看到的由高斯帕對象聲明,沒有參數的含有0爲x或y。有些東西沒了。發生了什麼?

在此先感謝任何能夠回答上述問題中的一個或兩個的人! :)

回答

0

您可以使用

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

打開追加模式的文件。意味着舊內容不會被覆蓋。 然而,您將需要添加一些內容來檢測是否已寫入標頭 out << "%!PS-Adobe-2.0" << endl;。 (我假設你只需要每個文件一次)

爲了避免打開和關閉文件所有的時間,你也可以創建一個單獨的類來打開文件,初始化它(寫入頭文件)然後使用這個類來寫所有的內容,然後關閉文件。

對於獎勵積分,請使用RAII讓班級自動處理文件。