C++

[C++] 명품 C++ 프로그래밍 실습문제 9장 10번

Hs’s Coding vlog 2023. 1. 14. 15:41

[C++]명품 C++ 프로그래밍 실습문제 9장 10번

[C++]명품 C++ programming 실습문제 9장 10번

명품 C++ 프로그래밍실습문제/연습문제 /C++

//Circle.cpp
#include "Circle.h"

void Circle:: draw() {
	cout << "Circle" << endl;
}

//Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include "Shape.h"
using namespace std;

class Circle : public Shape {
protected:
	virtual void draw();

};
#endif

//GraphicEditor.cpp
#include "GraphicEditor.h"
#include "Line.h"
#include "Circle.h"
#include "Rectangle.h"
#include "UI.h"

void GraphicEditor::insertItem(int type) {
	Shape* p = NULL;
	switch (type) {
	case 1:
		p = new Line();
		break;
	case 2:
		p = new Circle();
		break;
	case 3:
		p = new Rectangle();
		break;
	default:
		break;
	}
	if (pStart == NULL) {
		pStart = p;
		pLast = p;
		return;
	}
	pLast->add(p);
	pLast = pLast->getNext();
}
void GraphicEditor::deleteItem(int index) {
	Shape* pre = pStart;
	Shape* tmp = pStart;
	if (pStart == NULL) {
		cout << "도형이 없습니다!" << endl;
		return;
	}
	for (int i = 1; i < index; i++) {
		pre = tmp;
		tmp = tmp->getNext();
	}
	if (tmp == pStart) {			//첫번째 도형을 삭제하는 경우
		pStart = tmp->getNext();
		delete tmp;
	}
	else {
		pre->add(tmp->getNext());
		delete tmp;
	}
}
void GraphicEditor::show() {
	Shape* tmp = pStart;
	int i = 1;
	while (tmp != NULL) {
		cout << i++ << ": ";
		tmp->paint();
		tmp = tmp->getNext();
	}
}
void GraphicEditor::run() {
	cout << "그래픽 에디터입니다." << endl;
	int menu, index, type;
	while (true) {
		menu = UI::getMenu();
		switch (menu) {
		case 1:
			type = UI::getShapeTypeToInsert();
			insertItem(type);
			break;
		case 2:
			index = UI::getShapeIndexToDelete();
			deleteItem(index);
			break;
		case 3:
			show();
			break;
		default:
			return;
		}
	}
}

//GraphicEditor.h
#ifndef GraphicEditor_H
#define GraphicEditor_H
#include <iostream>
#include "Shape.h"
using namespace std;

class GraphicEditor {
	Shape* pStart;
	Shape* pLast;
public:
	GraphicEditor() { pStart = pLast = NULL; }
	void insertItem(int type);
	void deleteItem(int index);
	void show();
	void run();
};

#endif

//Line.cpp
#include "Line.h"

void Line::draw() {
	cout << "Line" << endl;
}

//Line.h
#ifndef LINE_H
#define LINE_H
#include <iostream>
#include "Shape.h"
using namespace std;

class Line : public Shape {
protected:
	virtual void draw();

};

#endif


//Rectangle.cpp
#include "Rectangle.h"


void Rectangle::draw() {
		cout << "Rectangle" << endl;
}

//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
#include "Shape.h"
using namespace std;

class Rectangle : public Shape {
protected:
	virtual void draw();

};

#endif


//Shape.cpp
#include <iostream>
#include "Shape.h"
Shape::Shape() { next = NULL; }
Shape* Shape::add(Shape* p) {
	this->next = p;
	return p;
}
void Shape::paint() {
	draw();
}
Shape* Shape::getNext() { return next; }


//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
using namespace std;
class Shape {
	Shape* next;
protected:
	virtual void draw() = 0;
public:
	Shape();
	virtual ~Shape() {}
	Shape* add(Shape* p);
	void paint();
	Shape* getNext();
};

#endif

//UI.cpp
#include "UI.h"

int UI::getMenu() {
	int key;
	cout << "삽입:1, 삭제:2, 모두보기:3, 종료:4 >>";
	cin >> key;
	return key;
}
int UI::getShapeTypeToInsert() {
	int key;
	cout << "선:1, 원:2, 사각형:3 >>";
	cin >> key;
	return key;
}
int UI::getShapeIndexToDelete() {
	int key;
	cout << "삭제하고자 하는 도형의 인덱스 >>";
	cin >> key;
	return key;
}


//UI.h
#ifndef UI_H
#define UI_H
#include <iostream>
#include "Shape.h"
using namespace std;

class UI {
public:
	static int getMenu();
	static int getShapeTypeToInsert();
	static int getShapeIndexToDelete();

};
#endif