C++ Programming Ch.9 Exercise 10 Solution
Problem: κ°λ¨ν κ·Έλν½ νΈμ§κΈ°λ₯Ό μ½μ λ°νμΌλ‘ λ§λ€μ΄λ³΄μ. κ·Έλν½ νΈμ§κΈ°μ κΈ°λ₯μ "μ½μ ", "μμ ", "λͺ¨λ보기", "μ’ λ£" μ 4κ°μ§μ΄κ³ , μ€ν κ³Όμ μ λ€μκ³Ό κ°λ€. Objective & Hints: μΆμ ν΄λμ€, μμ μ’ ν© μμ© Shapeκ³Ό μ΄λ₯Ό μμλ°μ Circle, Line,Rect ν΄λμ€λ [κ·Έλ¦Ό9-13]μ μ΄μ©νκ³ νμν ν΄λμ€μ main() ν¨μλ₯Ό μμ±νλΌ. μ 체 νλ‘κ·Έλ¨μ λλ΅ μλμ κ°μ΄ ꡬμ±λλ€. Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 #include #include<stdlib.h> using namespace std; class UI { public: static int show_menu() { int num; cout << "μ½μ :1, μμ :2, λͺ¨λ보기:3, μ’ λ£:4 >>" ; cin >> num; return num; } static int input_shape() { int num; cout << "μ :1, μ:2, μ¬κ°ν:3 >> "; cin >> num; return num; } static int del_index() { int num; cout << "μμ νκ³ μ νλ λνμ μΈλ±μ€ >> "; cin >> num; return num; } }; class Shape { // Node Shape* next; protected: virtual void draw() { cout << "--Shape--" << endl; } public: Shape() { next = NULL; } virtual ~Shape() {} Shape* add(Shape* p) { this->next = p; return p; } Shape* getNext() { return next; } void paint() { draw(); } void setNext(Shape *p) { this->next = p->next; } }; class Line : public Shape { public: virtual void draw() { cout << "Line" << endl; } }; class Circle : public Shape { public: virtual void draw() { cout << "Circle" << endl; } }; class Rect : public Shape { public: virtual void draw() { cout << "Rectangle" << endl; } }; class GraphicEditor { // List int node_size; Shape* pStart; Shape* pLast; public: GraphicEditor() { pStart = NULL; node_size = 0; } int run() { cout << "κ·Έλν½ μλν°μ λλ€.\n"; while(true){ int num; num = UI::show_menu(); switch (num){ case 1:{ num = UI::input_shape(); input_new(num); break; } case 2:{ if(pStart == NULL){ cout << "List Empty\n"; break; } num = UI::del_index(); del(num); break; } case 3:{ show(); break; } case 4:{ exit(0); } default : cout << "λ©λ΄λ₯Ό μλͺ» μ ννμ ¨μ΅λλ€.\n"; } } } void input_new(int n) { switch (n){ case 1: { if(node_size == 0) { pStart = new Line(); pLast = pStart; } else pLast = pLast->add(new Line()); node_size++; break; } case 2: { if(node_size == 0){ pStart = new Circle(); pLast = pStart; } else pLast = pLast->add(new Circle()); node_size++; break; } case 3: { if(node_size == 0){ pStart = new Rect(); pLast = pStart; } else pLast = pLast->add(new Rect()); node_size++; break; } default : cout << "λ©λ΄λ₯Ό μλͺ» μ ννμ ¨μ΅λλ€.\n"; } } bool del(int n) { int k=0; Shape* target_node = pStart; Shape* priv_node; if(n == 0){ pStart = pStart->getNext(); delete target_node; } else{ while( (target_node != NULL) && (k < n)){ priv_node = target_node; target_node = target_node->getNext(); k++; } if(target_node == NULL){ cout << "μλ λ Έλμ λλ€.\n"; return false; } else { priv_node->setNext(target_node); delete target_node; } } node_size--; } void show() { Shape* p = pStart; int i=0; if(p == NULL) cout << "List Empty\n"; else while(p != NULL){ cout << i << ": "; p->paint(); p = p->getNext(); i++; } } }; int main() { GraphicEditor* g_editor = new GraphicEditor; g_editor->run(); delete g_editor; } Explanation: ...