명품 C++ programming 실습 문제 2장 1번

문제 : cout과 << 연산자를 이용하여, 1에서 100까지 정수를 다음과 같이 한 줄에 10개씩 출력하라. 각 정수는 탭으로 분리하여 출력하라. 실행 결과 : 목적 및 힌트 : cout 활용, 화면 출력 코드 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main(){ int i; for(i=1;i<101;i++){ cout << i << "\t"; if(i%10==0) // 10개마다 개행문자 출력 cout << endl; } return 0; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : 간단한 그래픽 편집기를 콘솔 바탕으로 만들어보자. 그래픽 편집기의 기능은 "삽입", "삭제", "모두보기", "종료" 의 4가지이고, 실행 과정은 다음과 같다. 목적 및 힌트 : 추상 클래스, 상속 종합 응용 Shape과 이를 상속받은 Circle, Line,Rect 클래스는 [그림9-13]을 이용하고 필요한 클래스와 main() 함수를 작성하라. 전체 프로그램은 대략 아래와 같이 구성된다. 코드 : 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; } 설명 : ...

2019년 11월 26일 · 4 분 · Sobamemil

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

문제 : 다음 그림과 같은 상속 구조를 갖는 클래스를 설계한다. 모든 프린터는 모델명(model), 제조사(manufacturer), 인쇄 매수(printedCount), 인쇄 종이 잔량(availableCount)을 나타내는 정보를 가진다. print(int pages) 함수와 show() 함수는 가상 함수로 구현하라. print(int pages)는 pages 만큼 프린트하는 함수이고, show() 함수는 현재 프린트의 모델, 제조사, 인쇄 매수, 인쇄 종이 잔량 등을 출력하는 함수이다. 잉크젯 프린터는 잉크 잔량(availableInk) 정보를 추가적으로 가지며, 레이저 프린터는 토너 잔량(availableToner) 정보를 추가적으로 가진다. 이들의 print(int pages) 멤버 함수는 프린터 타입에 맞게 구현하라. 각 클래스를 설계 구현하고 다음과 같이 실행되도록 전체 프로그램을 완성하라. InkJetPrinter 객체와 LaserPrinter 객체를 각각 하나만 동적으로 생성하여 시작한다. ...

2019년 11월 26일 · 3 분 · Sobamemil

명품 C++ programming 실습 문제 9장 8번

문제 : 사각형에 내접하는 도형을 표현하기 위한 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을 다시 작성하라. ...

2019년 11월 21일 · 3 분 · Sobamemil

명품 C++ programming 실습 문제 9장 7번

문제 : 사각형에 내접하는 도형을 표한하기 위한 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; } // 이름 리턴 }; Shape 클래스를 상속받아 타원을 표현하는 Oval, 사각형을 표현하는 Rect, 삼각형을 표현하는 Triangular 클래스를 작성하라. main()을 작성하고 실행하면 다음과 같다. ...

2019년 11월 21일 · 2 분 · Sobamemil

명품 C++ programming 실습 문제 9장 6번

문제 : 다음 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 클래스를 만들고 사용 사례를 보여라. 목적 및 힌트 : ...

2019년 11월 21일 · 2 분 · Sobamemil

명품 C++ programming 실습 문제 9장 3번

문제 : 다음 추상 클래스 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을 출력한다. } LoopAdder 클래스를 상속받아 다음 main() 함수와 실행 결과처럼 되도록 ForLoopAdder 클래스를 작성하라. ForLoopAdder 클래스의 calculate() 함수는 for 문을 이용하여 합을 구한다. ...

2019년 11월 21일 · 3 분 · Sobamemil

명품 C++ programming 실습 문제 9장 1번

문제: 다음은 단위를 변환하는 추상 클래스 Converter이다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include using namespace std; class Converter { protected: double ratio; virtual double convert(double src)=0; // src를 다른 단위로 변환한다. virtual string getSourceString()=0; // src 단위 명칭 virtual string getDestString()=0; // dest 단위 명칭 public: Converter(double ratio) { this->ratio = ratio; } void run(){ double src; cout << getSourceString() << "을 " << getDestString() << "로 바꿉니다. "; cout << getSourceString() << "을 입력하세요>> "; cin >> src; cout << "변환 결과 : " << convert(src) << getDestString() << endl; } }; Converter 클래스를 상속받아 달러를 원화로 환산하는 WonToDollar 클래스를 작성하라. main() 함수와 실행 결과는 다음과 같다. ...

2019년 11월 20일 · 2 분 · Sobamemil