2013-10-26 1 views
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 : 여기

는 (그림을 제어하는 ​​I는 불필요한 코드를 잘라 것)의 순으로 내 Gosper.h, Gosper.cpp, Turtle.h 및 Turtle.cpp 파일입니다 :

// 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 : 나는이 일에 GosperDriver.cpp의 모든 Gosper 클래스 객체의 텍스트를 작성하려는

:

#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 

자, 이제 당신이 내 코드를 본 적이 있는지, 여기에 내 문제입니다 포스트 스크립트 파일. 지금 당장이 작업을 수행하려고하면 지정된 output.ps의 텍스트 블록을 덮어 쓰게됩니다. 지금은 ONE Gosper 클래스 객체에 필요한 텍스트 만 쓸 수 있습니다. Gosperdriver.cpp의 모든 Gosper 개체 선언을 주석 처리해야했지만 프로그램이 제대로 작동하는지 테스트해야했습니다.

요약하면 GosperDriver.cpp의 모든 Gosper 개체에 대해 output.ps에 필요한 텍스트를 작성해야하지만 한 번에 하나씩 작성하도록하기 때문에 작동하지 않습니다. 나는 무엇을해야합니까? 상속에 대한


보너스 질문 : 지금, 각 Gosper 그리기에 대한 내 "시작 지점"X = 0, Y = Gosper 객체 선언에서 보듯이 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를 사용하여 클래스가 파일을 자동으로 처리하도록합니다.