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: ...

November 26, 2019 Β· 4 min Β· Sobamemil

C++ Programming Ch.9 Exercise 9 Solution

Problem: λ‹€μŒ κ·Έλ¦Όκ³Ό 같은 상속 ꡬ쑰λ₯Ό κ°–λŠ” 클래슀λ₯Ό μ„€κ³„ν•œλ‹€. λͺ¨λ“  ν”„λ¦°ν„°λŠ” λͺ¨λΈλͺ…(model), μ œμ‘°μ‚¬(manufacturer), 인쇄 맀수(printedCount), 인쇄 쒅이 μž”λŸ‰(availableCount)을 λ‚˜νƒ€λ‚΄λŠ” 정보λ₯Ό κ°€μ§„λ‹€. print(int pages) ν•¨μˆ˜μ™€ show() ν•¨μˆ˜λŠ” 가상 ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜λΌ. print(int pages)λŠ” pages 만큼 ν”„λ¦°νŠΈν•˜λŠ” ν•¨μˆ˜μ΄κ³ , show() ν•¨μˆ˜λŠ” ν˜„μž¬ ν”„λ¦°νŠΈμ˜ λͺ¨λΈ, μ œμ‘°μ‚¬, 인쇄 맀수, 인쇄 쒅이 μž”λŸ‰ 등을 좜λ ₯ν•˜λŠ” ν•¨μˆ˜μ΄λ‹€. μž‰ν¬μ ― ν”„λ¦°ν„°λŠ” μž‰ν¬ μž”λŸ‰(availableInk) 정보λ₯Ό μΆ”κ°€μ μœΌλ‘œ κ°€μ§€λ©°, λ ˆμ΄μ € ν”„λ¦°ν„°λŠ” ν† λ„ˆ μž”λŸ‰(availableToner) 정보λ₯Ό μΆ”κ°€μ μœΌλ‘œ κ°€μ§„λ‹€. μ΄λ“€μ˜ print(int pages) 멀버 ν•¨μˆ˜λŠ” ν”„λ¦°ν„° νƒ€μž…μ— 맞게 κ΅¬ν˜„ν•˜λΌ. 각 클래슀λ₯Ό 섀계 κ΅¬ν˜„ν•˜κ³  λ‹€μŒκ³Ό 같이 μ‹€ν–‰λ˜λ„λ‘ 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. InkJetPrinter 객체와 LaserPrinter 객체λ₯Ό 각각 ν•˜λ‚˜λ§Œ λ™μ μœΌλ‘œ μƒμ„±ν•˜μ—¬ μ‹œμž‘ν•œλ‹€. ...

November 26, 2019 Β· 3 min Β· Sobamemil

μ‹œμŠ€ν…œ ν”„λ‘œκ·Έλž˜λ° ν”„λ‘œμ νŠΈ #5

Problem: Input File: [sample.txt 0.00MB](https://blog.kakaocdn.net/dna/rQmDA/btqBh0wGGuo/AAAAAAAAAAAAAAAAAAAAAOJH_v7vkHkLQ6qLzsNnWwGtEpIuLXHhdgVHWZ1_bdfn/sample.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=g2umKymX%2Fy9G6jLj7w3iVVcqUKc%3D&attach=1&knm=tfile.txt) μ‹€ν–‰κ²°κ³Ό : μ†ŒμŠ€Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include<stdio.h> #include<string.h> #include<stdlib.h> int len=0; void print_len_word(char *bp){ int i; for(i=0; bp[i]!='\0'; i++) // bp λ°°μ—΄μ˜ 끝이 λ‚˜μ˜¬λ•ŒκΉŒμ§€ loop if(bp[i]=='\n') bp[i] = '\0'; // bp[i]κ°€ κ°œν–‰λ¬Έμžμ΄λ©΄ 그곳을 NULL둜 λ°”κΏˆ printf("%.2X: %s\n", len, bp); len += strlen(bp); } int main() { char buf[80]; FILE* fp; if(!(fp = fopen("test1.txt", "r"))) { printf("file not open\n"); exit(1); } while(fgets(buf, sizeof(buf), fp) != NULL) print_len_word(buf); fclose(fp); printf("%X", len); return 0; } Explanation: ...

November 23, 2019 Β· 1 min Β· Sobamemil

μ‹œμŠ€ν…œ ν”„λ‘œκ·Έλž˜λ° ν”„λ‘œμ νŠΈ #6

Problem: μž…λ ₯ 데이터 : [Command.txt 0.00MB](https://blog.kakaocdn.net/dna/bnbU30/btqBgeimBnY/AAAAAAAAAAAAAAAAAAAAAAPakDbTA4jf0WyG_DmDE76emfyLiazho4x1advtOToN/Command.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=2iAm290NW4CFkRgpHKAQHRqBvxw%3D&attach=1&knm=tfile.txt) Execution Result: 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 #include<stdio.h> #include<string.h> #include<stdlib.h> void PrintAssm(char* name_buf, int name_size); struct OPTAB{ char name[8]; int len; } Wordtab[] = { {"LDA", 3}, {"STA", 4}, {"ADD", 5}, {"TIX", 2}, {"CMP", 6} }; int main() { char name_buf[8]; int wordtab_size = sizeof(Wordtab)/12; // Wordtab에 μ •μ˜λœ 단어 갯수, 12 = size of char[8] + int FILE* fp; if((fp = fopen("Command.txt", "r"))==NULL) { printf("file not found...\n"); exit(1); } while(fgets(name_buf, sizeof(name_buf), fp) != NULL) { // fgets() ν•¨μˆ˜λŠ” scanfλ‚˜ gets()μ™€λŠ” 달리 뒀에 κ°œν–‰λ¬Έμžκ°€ λΆ™λŠ”λ‹€ if(name_buf[0] == '\n') // name_buf λ°°μ—΄ 맨 처음이 κ°œν–‰λ¬Έμžμ΄λ©΄ λ©”μ‹œμ§€ 좜λ ₯ 없이 λ¬΄μ‹œ continue; if(name_buf[strlen(name_buf) - 1] == '\n') // name_buf λ°°μ—΄ 맨 끝이 κ°œν–‰λ¬Έμžμ΄λ©΄ name_buf[strlen(name_buf) - 1] = '\0'; // naem_buf λ°°μ—΄ 맨 끝에 뢙은 κ°œν–‰λ¬Έμž 제거 PrintAssm(name_buf, wordtab_size); } fclose(fp); } void PrintAssm(char* name_buf, int wordtab_size){ static int k=1; // 쀄 번호λ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ³€μˆ˜ static int loc=0; // λ‹¨μ–΄μ˜ μ‹œμž‘ μœ„μΉ˜λ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ³€μˆ˜ int i, res=-1; for(i=0; i<wordtab_size; i++){ res = strcmp(name_buf, Wordtab[i].name); // strcmp : 두 λ¬Έμžμ—΄μ΄ κ°™μœΌλ©΄ 0 리턴 if(res==0){ break; } } if(i==wordtab_size){ printf(" Undefined word\n"); // Wordtab λ°°μ—΄ μ•ˆμ— μ •μ˜λ˜μ–΄ μžˆμ§€ μ•ŠλŠ” λͺ…λ Ήμ–΄λ©΄ 좜λ ₯ν•˜κ³  단어 λ¬΄μ‹œ } else{ if(k < 10) printf(" "); printf("%d, %.2X, %s, %.2d\n", k, loc, Wordtab[i].name, Wordtab[i].len); loc += Wordtab[i].len; // λ‹¨μ–΄μ˜ 해당길이λ₯Ό λ”ν•΄μ„œ λ‹€μŒ μ‹œμž‘ μœ„μΉ˜λ₯Ό μ•Œλ €μ€Œ k++; } } Explanation: ...

November 21, 2019 Β· 2 min Β· Sobamemil

C++ Programming Ch.9 Exercise 8 Solution

Problem: μ‚¬κ°ν˜•μ— λ‚΄μ ‘ν•˜λŠ” λ„ν˜•μ„ ν‘œν˜„ν•˜κΈ° μœ„ν•œ Shape ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Shape { protected: string name; // λ„ν˜•μ˜ 이름 int width, height; // λ„ν˜•μ΄ λ‚΄μ ‘ν•˜λŠ” μ‚¬κ°ν˜•μ˜ λ„ˆλΉ„μ™€ 높이 public: Shape(string n="", int w=0, int h=0) { name = n; width = w; height = h; } virtual double getArea() { return 0; } // dummy κ°’ 리턴 string getName() { return name; } // 이름 리턴 }; 문제 7에 μ£Όμ–΄μ§„ Shape 클래슀λ₯Ό 좔상 클래슀둜 λ§Œλ“€κ³  문제 7을 λ‹€μ‹œ μž‘μ„±ν•˜λΌ. ...

November 21, 2019 Β· 2 min Β· Sobamemil

C++ Programming Ch.9 Exercise 7 Solution

Problem: μ‚¬κ°ν˜•μ— λ‚΄μ ‘ν•˜λŠ” λ„ν˜•μ„ ν‘œν•œν•˜κΈ° μœ„ν•œ Shape ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Shape { protected: string name; // λ„ν˜•μ˜ 이름 int width, height; // λ„ν˜•μ΄ λ‚΄μ ‘ν•˜λŠ” μ‚¬κ°ν˜•μ˜ λ„ˆλΉ„μ™€ 높이 public: Shape(string n="", int w=0, int h=0) { name = n; width = w; height = h; } virtual double getArea() { return 0; } // dummy κ°’ 리턴 string getName() { return name; } // 이름 리턴 }; Write a 타원을 ν‘œν˜„ν•˜λŠ” Oval, μ‚¬κ°ν˜•μ„ ν‘œν˜„ν•˜λŠ” Rect, μ‚Όκ°ν˜•μ„ ν‘œν˜„ν•˜λŠ” Triangular class that inherits from the Shape class. main()을 μž‘μ„±ν•˜κ³  μ‹€ν–‰ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€. ...

November 21, 2019 Β· 2 min Β· Sobamemil

C++ Programming Ch.9 Exercise 6 Solution

Problem: λ‹€μŒ AbstractStack은 μ •μˆ˜ μŠ€νƒ ν΄λž˜μŠ€λ‘œμ„œ 좔상 ν΄λž˜μŠ€μ΄λ‹€. 1 2 3 4 5 6 7 class AbstrackStack { public: virtual bool push(int n) = 0; // μŠ€νƒμ— n을 ν‘Έμ‹œν•œλ‹€. μŠ€νƒμ΄ full이면 false 리턴 virtual bool pop(int& n) = 0; // μŠ€νƒμ—μ„œ νŒν•œ μ •μˆ˜λ₯Ό n에 μ €μž₯ν•˜κ³  μŠ€νƒμ΄ empty이면 false 리턴 virtual int size() = 0; }; 이λ₯Ό 상속받아 μ •μˆ˜λ₯Ό ν‘Έμ‹œ, νŒν•˜λŠ” IntStack 클래슀λ₯Ό λ§Œλ“€κ³  μ‚¬μš© 사둀λ₯Ό 보여라. Objective & Hints: ...

November 21, 2019 Β· 2 min Β· Sobamemil

C++ Programming Ch.9 Exercise 5 Solution

Problem: λ””μ§€ν„Έ νšŒλ‘œμ—μ„œ 기본적인 게이트둜 OR 게이트, AND 게이트, XOR 게이트 등이 μžˆλ‹€. 이듀은 각각 두 μž…λ ₯ μ‹ ν˜Έλ₯Ό λ°›μ•„ OR μ—°μ‚°, AND μ—°μ‚°, XOR 연산을 μˆ˜ν–‰ν•œ κ²°κ³Όλ₯Ό 좜λ ₯ν•œλ‹€. 이 κ²Œμ΄νŠΈλ“€μ„ 각각 ORGate, XORGate, ANDGate 클래슀둜 μž‘μ„±ν•˜κ³ μž ν•œλ‹€. ORGate, XORGate, ANDGate ν΄λž˜μŠ€κ°€ AbstractGatefλ₯Ό 상속받도둝 μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 class AbstractGate { // 좔상 클래슀 protected: bool x, y; public: void set(bool x, bool y) { this->x = x; this->y = y; } virtual bool operation()=0; }; ANDGate, ORGate, XORGateλ₯Ό ν™œμš©ν•˜λŠ” 사둀와 κ²°κ³ΌλŠ” λ‹€μŒκ³Ό κ°™λ‹€. ...

November 21, 2019 Β· 2 min Β· Sobamemil

C++ Programming Ch.9 Exercise 4 Solution

Problem: λ‹€μŒ 좔상 클래슀 LoopAdderκ°€ μžˆλ‹€. 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 class LoopAdder { // 좔상 클래슀 string name; // λ£¨ν”„μ˜ 이름 int x, y, sum; // xμ—μ„œ yκΉŒμ§€μ˜ 합은 sum void read(); // x, y 값을 μ½μ–΄λ“œλ¦¬λŠ” ν•¨μˆ˜ void write(); // sum을 좜λ ₯ν•˜λŠ” ν•¨μˆ˜ protected: LoopAdder(string name="") { // λ£¨ν”„μ˜ 이름을 λ°›λŠ”λ‹€. μ΄ˆκΉƒκ°’μ€ "" this->name = name; } int getX() { return x; } int getY() { return y; } virtual int calculate() = 0; // 순수 가상 ν•¨μˆ˜. 루프λ₯Ό 돌며 합을 κ΅¬ν•˜λŠ” ν•¨μˆ˜ public: void run(); // 연산을 μ§„ν–‰ν•˜λŠ” ν•¨μˆ˜ }; void LoopAdder::read() { // x, y μž…λ ₯ cout << name << ":" << endl; cout << "처음 μˆ˜μ—μ„œ λ‘λ²ˆμ§Έ μˆ˜κΉŒμ§€ λ”ν•œλ‹€. 두 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” >> "; cin >> x >> y; } void LoopAdder::write() { // κ²°κ³Ό sum 좜λ ₯ cout << x << "μ—μ„œ " << y << "κΉŒμ§€μ˜ ν•© = " << sum << " μž…λ‹ˆλ‹€" << endl; } void LoopAdder::run() { read(); // x, yλ₯Ό μ½λŠ”λ‹€ sum = calculate(); // 루프λ₯Ό λŒλ©΄μ„œ κ³„μ‚°ν•œλ‹€. write(); // κ²°κ³Ό sum을 좜λ ₯ν•œλ‹€. } Write a λ‹€μŒ main() ν•¨μˆ˜μ™€ Execution Result처럼 λ˜λ„λ‘ WhileLoopAdder, DoWhileLoopAdder class that inherits from the LoopAdder class. while λ¬Έ, do-while 문을 μ΄μš©ν•˜μ—¬ 합을 κ΅¬ν•˜λ„λ‘ calculate() ν•¨μˆ˜λ₯Ό 각각 μž‘μ„±ν•˜λ©΄ λœλ‹€. ...

November 21, 2019 Β· 3 min Β· Sobamemil

C++ Programming Ch.9 Exercise 3 Solution

Problem: λ‹€μŒ 좔상 클래슀 LoopAdderκ°€ μžˆλ‹€. 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 class LoopAdder { // 좔상 클래슀 string name; // λ£¨ν”„μ˜ 이름 int x, y, sum; // xμ—μ„œ yκΉŒμ§€μ˜ 합은 sum void read(); // x, y 값을 읽어 λ“€μ΄λŠ” ν•¨μˆ˜ void write(); // sum을 좜λ ₯ν•˜λŠ” ν•¨μˆ˜ protected: LoopAdder(string name="") { // λ£¨ν”„μ˜ 이름을 λ°›λŠ”λ‹€. μ΄ˆκΉƒκ°’μ€ "" this->name = name; } int getX() { return x; } int getY() { return y; } virtual int calculate() = 0; // 순수 가상 ν•¨μˆ˜. 루프λ₯Ό 돌며 합을 κ΅¬ν•˜λŠ” ν•¨μˆ˜ public: void run(); // 연산을 μ§„ν–‰ν•˜λŠ” ν•¨μˆ˜ }; void LoopAdder::read() { // x, y μž…λ ₯ cout << name << ":" << endl; cout << "처음 μˆ˜μ—μ„œ λ‘λ²ˆμ§Έ μˆ˜κΉŒμ§€ λ”ν•œλ‹€. 두 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” >> "; cin >> x >> y; } void LoopAdder::write() { // κ²°κ³Ό sum 좜λ ₯ cout << x << "μ—μ„œ " << y << "κΉŒμ§€μ˜ ν•© = " << sum << " μž…λ‹ˆλ‹€" << endl; } void LoopAdder::run() { read(); // x, yλ₯Ό μ½λŠ”λ‹€ sum = calculate(); // 루프λ₯Ό λŒλ©΄μ„œ κ³„μ‚°ν•œλ‹€. write(); // κ²°κ³Ό sum을 좜λ ₯ν•œλ‹€. } Write a λ‹€μŒ main() ν•¨μˆ˜μ™€ Execution Result처럼 λ˜λ„λ‘ ForLoopAdder class that inherits from the LoopAdder class. ForLoopAdder 클래슀의 calculate() ν•¨μˆ˜λŠ” for 문을 μ΄μš©ν•˜μ—¬ 합을 κ΅¬ν•œλ‹€. ...

November 21, 2019 Β· 3 min Β· Sobamemil