728x90
개인적으로 C++을 공부할 때 작성해놓았던 코드들을 찾았다.
혼자 책을 사서 독학하던 시절에, 그리고 학부생 시절에 복습하면서 했던 문제들이라서
어떤 문제들은 깔끔히 잘 정리되어 있고, 어떤 문제들은 허접한 버그가 있을 수도 있다.
확인은 해뒀지만, 확인하지 못하거나 고려해야 할 버그, 오탈자 등은 댓글을 남겨주시라.
그간 공부한 것들을 정리하는 블로그이기 때문에 올려놓는다.
지금까지는 실습문제 문제 내용까지 적었지만, 귀찮기도 하고, 굳이 입력할 필요성은 느끼지 않아서 적지 않았다. 차후 게시물을 대대적으로 수정할 때면 추가될지도 모른다.
실습문제 1.
#include <iostream> #include <string> using namespace std; class Circle { int radius; public: Circle(int radius) { this->radius = radius; } int getRadius() { return radius; } void setRadius(int radius) { this->radius = radius; } double getArea() { return 3.14*radius*radius; } }; class NamedCircle : public Circle { string name; public: NamedCircle(int radius, string name) : Circle(radius) { this->name = name; } void show() { cout << "¹ÝÁö¸§ÀÌ " << getRadius() << "ÀÎ " << name << endl; } }; int main(void) { NamedCircle waffle(3, "waffle"); waffle.show(); }
실습문제 2.
#include실습문제 3.
#include <iostream> #include <string> using namespace std; class Point { int x, y; public: Point(int x, int y) { this->x = x, this->y = y; } int getX() { return x; } int getY() { return y; } protected: void move(int x, int y) { this->x = x, this->y = y; } }; class ColorPoint : public Point { string color; public: ColorPoint(int x, int y, string color) : Point(x, y) { this->color = color; } void setPoint(int x, int y) { this->move(x, y); } void setColor(string color) { this->color = color; } void show() { cout << color << "색으로 (" << getX() << ',' << getY() << ")에 위치한 점입니다.\n"; } }; int main(void) { ColorPoint cp(5, 5, "RED"); cp.setPoint(10, 20); cp.setColor("BLUE"); cp.show(); }
실습문제 4.
#include <iostream> #include <string> using namespace std; class Point { int x, y; public: Point(int x, int y) { this->x = x, this->y = y; } int getX() { return x; } int getY() { return y; } protected: void move(int x, int y) { this->x = x, this->y = y; } }; class ColorPoint : public Point { string color; public: ColorPoint(int x = 0, int y = 0, string color = "BLACK") : Point(x, y) { this->color = color; } void setPoint(int x, int y) { this->move(x, y); } void setColor(string color) { this->color = color; } void show() { cout << color << "색으로 (" << getX() << ',' << getY() << ")에 위치한 점입니다.\n"; } }; int main(void) { ColorPoint zeroPoint; zeroPoint.show(); ColorPoint cp(5, 5); cp.setPoint(10, 20); cp.setColor("BLUE"); cp.show(); }
실습문제 5.
#include <iostream> using namespace std; class BaseArray { private: int capacity; int* mem; protected: BaseArray(int capacity = 100) { this->capacity = capacity, mem = new int[capacity]; } ~BaseArray() { delete[] mem; } void put(int index, int val) { mem[index] = val; } int get(int index) { return mem[index]; } int getCapacity() { return capacity; } }; class MyQueue : public BaseArray { int rear, front; public: MyQueue(int capacity) : BaseArray(capacity) { rear = 0, front = -1; }; void enqueue(int n) { put(rear, n), rear++; } int dequeue() { int tmp = get(++front); return tmp; } int capacity() { return getCapacity(); } int length() { return rear - front - 1; } }; int main(void) { MyQueue mQ(100); int n; cout << "큐에 삽입할 5개의 정수를 입력하라>> "; for (int i = 0; i < 5; i++) { cin >> n; mQ.enqueue(n); } cout << "큐의 용량:" << mQ.capacity() << ", 큐의 크기:" << mQ.length() << endl; cout << "큐의 원소를 순서대로 제거하여 출력한다>> "; while (mQ.length() != 0) { cout << mQ.dequeue() << ' '; } cout << endl << "큐의 현재 크기 : " << mQ.length() << endl; }
실습문제 6.
#include <iostream> using namespace std; class BaseArray { private: int capacity; int* mem; protected: BaseArray(int capacity = 100) { this->capacity = capacity, mem = new int[capacity]; } ~BaseArray() { delete[] mem; } void put(int index, int val) { mem[index] = val; } int get(int index) { return mem[index]; } int getCapacity() { return capacity; } }; class MyStack : public BaseArray { private: int top; public: MyStack(int capacity) : BaseArray(capacity) { top = -1; } void push(int n) { put(++top, n); } int pop() { return get(top--); } int length() { return top + 1; } int capacity() { return getCapacity(); } }; int main(void) { MyStack mStack(100); int n; cout << "스택에 삽입할 5개의 정수를 입력하라>> "; for (int i = 0; i < 5; i++) { cin >> n; mStack.push(n); } cout << "스택용량:" << mStack.capacity() << ", 스택크기:" << mStack.length() << endl; cout << "스택의 모든 원소를 팝하여 출력한다>> "; while (mStack.length() != 0) { cout << mStack.pop() << ' '; } cout << endl << "스택의 현재 크기 : " << mStack.length() << endl; }
실습문제 7.
#include <iostream> using namespace std; class BaseMemory { int memIdx; char* mem; protected: BaseMemory(int size) { mem = new char[size], memIdx = 0; } void writeMemory(int idx, char c) { mem[idx] = c; } char readMemory(int idx) { return mem[idx]; } }; class ROM : public BaseMemory { public: ROM(int size, char* x, int arrSize) : BaseMemory(size) { for (int i = 0; i < arrSize; i++) writeMemory(i, x[i]); } char read(int idx) { return readMemory(idx); } }; class RAM : public BaseMemory { public: RAM(int size) : BaseMemory(size) {} void write(int idx, char c) { writeMemory(idx, c); } char read(int idx) { return readMemory(idx); } }; int main(void) { char x[5] = { 'h','e','l','l','o' }; ROM biosROM(1024 * 10, x, 5); RAM mainMemory(1024 * 1024); for (int i = 0; i < 5; i++) mainMemory.write(i, biosROM.read(i)); for (int i = 0; i < 5; i++) cout << mainMemory.read(i); }
실습문제 8.
#include <iostream> #include <string> using namespace std; class Printer { string model, manufacturer; int printedCount, availableCount; protected: Printer(string model, string manufacturer, int printedCount, int availableCount) { this->model = model; this->manufacturer = manufacturer; this->printedCount = printedCount; this->availableCount = availableCount; } void print(int pages) { printedCount += pages, availableCount -= pages; } string getModel() { return model; } string getManufacturer() { return manufacturer; } int getPrinted() { return printedCount; } int getAvailable() { return availableCount; } }; class Inkjet : public Printer { int availableInk; public: Inkjet(string model, string manufacturer, int availableCount, int availableInk) : Printer(model, manufacturer, 0, availableCount) { this->availableInk = availableInk; } void printInkjet(int pages) { availableInk -= pages; print(pages); } int getInk() { return availableInk; } void showStatus() { cout << getModel() << " ," << getManufacturer() << " ,남은 종이 " << getAvailable() << "장 ,남은 잉크 " << getInk() << endl; } void cannotPrint(bool& cannot, int pages) { if ((getAvailable() - pages) < 0 && (getInk() - pages) < 0) cannot = true, cout << "용지와 잉크가 부족하여 프린트할 수 없습니다." << endl; else if ((getAvailable() - pages) < 0) cannot = true, cout << "용지가 부족하여 프린트할 수 없습니다." << endl; else if ((getInk() - pages) < 0) cannot = true, cout << "잉크가 부족하여 프린트할 수 없습니다.." << endl; else cannot = false; } }; class Laser : public Printer { int availableToner; public: Laser(string model, string manufacturer, int availableCount, int availableToner) : Printer(model, manufacturer, 0, availableCount) { this->availableToner = availableToner; } void printLaser(int pages) { availableToner -= pages; print(pages); } int getToner() { return availableToner; } void showStatus() { cout << getModel() << " ," << getManufacturer() << " ,남은 종이 " << getAvailable() << "장 ,남은 토너 " << getToner() << endl; } void cannotPrint(bool& cannot, int pages) { if ((getAvailable() - pages) < 0 && (getToner() - pages) < 0) cannot = true, cout << "용지와 토너가 부족하여 프린트할 수 없습니다." << endl; else if ((getAvailable() - pages) < 0) cannot = true, cout << "용지가 부족하여 프린트할 수 없습니다." << endl; else if ((getToner() - pages) < 0) cannot = true, cout << "토너가 부족하여 프린트할 수 없습니다." << endl; else cannot = false; } }; int main(void) { Inkjet* ink = new Inkjet("Officejet V40", "HP", 5, 10); Laser* laser = new Laser("SCX-6x45", "삼성전자", 3, 20); cout << "현재 작동중인 2대의 프린터는 아래와 같다." << endl; cout << "잉크젯 : "; ink->showStatus(); cout << "레이저 : "; laser->showStatus(); cout << endl; while (1) { char op; int printer, pages; cout << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>"; cin >> printer >> pages; if (printer == 1) { bool cannot; ink->cannotPrint(cannot, pages); if (!cannot) { ink->printInkjet(pages); cout << "프린트하였습니다." << endl; ink->showStatus(); laser->showStatus(); } cout << "계속 프린트 하시겠습니까?(y/n)>>"; cin >> op; if (op == 'n') break; } else if (printer == 2) { bool cannot; laser->cannotPrint(cannot, pages); if (!cannot) { laser->printLaser(pages); cout << "프린트하였습니다." << endl; ink->showStatus(); laser->showStatus(); } cout << "계속 프린트 하시겠습니까?(y/n)>>"; cin >> op; if (op == 'n') break; } else { cout << "프린터를 잘못 선택했습니다." << endl; } cout << endl; } }
실습문제 9.
#include <iostream> #include <string> using namespace std; class Seat { string name; public: Seat(string name = "") { this->name = name; } void setName(string name) { this->name = name; } string getName() { return name; } }; class Schedule { int seatSize; int time; Seat* list; public: Schedule() {} Schedule(int time, int seatSize = 8) { this->time = time; this->seatSize = seatSize; list = new Seat[seatSize]; for (int i = 0; i < seatSize; i++) list[i].setName("---"); } void setSchedule(int time, int seatSize = 8) { this->time = time; this->seatSize = seatSize; list = new Seat[seatSize]; for (int i = 0; i < seatSize; i++) list[i].setName("---"); } void showSeat() { for (int i = 0; i < seatSize; i++) cout << list[i].getName() << '\t'; cout << endl; } bool reserveSeat(int num, string name) { if (num > seatSize || num <= 0) { cout << "번호가 잘못되었습니다." << endl; return false; } else if (list[num - 1].getName() != "---") { cout << "이미 예약된 좌석입니다." << endl; return false; } else { list[num - 1].setName(name); return true; } } bool cancelSeat(int num, string name) { if (num > seatSize || num <= 0) { cout << "번호가 잘못되었습니다." << endl; return false; } else if (list[num - 1].getName() == "---") { cout << "이미 비어 있는 좌석입니다." << endl; return false; } else if (list[num - 1].getName() != name) { cout << "승객명이 일치하지 않습니다." << endl; return false; } else { list[num - 1].setName("---"); return true; } } int getTime() { return time; } }; class AirlineBook { string name; int scheduleSize; Schedule* list; public: AirlineBook(string name = "한성항공", int scheduleSize = 3) { this->name = name; this->scheduleSize = scheduleSize; list = new Schedule[scheduleSize]; list[0].setSchedule(7); list[1].setSchedule(12); list[2].setSchedule(17); } void showAllTime() { for (int i = 0; i < scheduleSize; i++) { if (i < scheduleSize - 1) cout << list[i].getTime() << "시:" << i + 1 << ", "; else cout << list[i].getTime() << "시:" << i + 1 << " >>"; } } void showSchedule(int time) { cout << list[time - 1].getTime() << "시:", list[time - 1].showSeat(); } void showAllSchedule() { for (int i = 0; i < scheduleSize; i++) cout << list[i].getTime() << "시:", list[i].showSeat(); cout << endl; } Schedule getSchedule(int time) { if (time > scheduleSize || time < 1) { cout << "해당하는 비행편이 없습니다." << endl; return NULL; } else { return list[time - 1]; } } string getName() { return name; } }; class Console { public: static void intro(AirlineBook* air) { cout << "*** " << air->getName() << "에 오신 것을 환영합니다. ***" << endl << endl; } static int Menu(AirlineBook* air) { cout << "예약:1, 취소:2, 보기:3, 끝내기:4>>"; int m; cin >> m; return m; } static int selectTime(AirlineBook* air) { air->showAllTime(); int t; cin >> t; return t; } static void showTime(AirlineBook* air, int time) { air->showSchedule(time); } static void showAllTime(AirlineBook* air) { air->showAllSchedule(); } static void reserve(AirlineBook* air, int time) { int num; string name; cout << "좌석 번호 >>"; cin >> num; cout << "이름 입력 >>"; cin >> name; air->getSchedule(time).reserveSeat(num, name); cout << endl; } static void cancel(AirlineBook* air, int time) { int num; string name; cout << "좌석 번호 >>"; cin >> num; cout << "이름 입력 >>"; cin >> name; air->getSchedule(time).cancelSeat(num, name); cout << endl; } }; int main(void) { AirlineBook* air = new AirlineBook(); Console::intro(air); while (1) { int menu = Console::Menu(air); if (menu == 4) break; if (menu == 1) { int time = Console::selectTime(air); Console::showTime(air, time); Console::reserve(air, time); } else if (menu == 2) { int time = Console::selectTime(air); Console::showTime(air, time); Console::cancel(air, time); } else { Console::showAllTime(air); } } }
728x90
'Programming Language > C,C++' 카테고리의 다른 글
C++] STL(Standard Template Library)이 무엇인가? (1) | 2017.11.22 |
---|---|
C++] 명품 C++ 프로그래밍 9장 실습문제 (3) | 2017.11.09 |
C++] 명품 C++ 프로그래밍 7장 실습문제 (0) | 2017.11.07 |
C++] 명품 C++ 프로그래밍 6장 실습문제 (0) | 2017.10.28 |
C++] 명품 C++ 프로그래밍 5장 실습문제 (2) | 2017.10.27 |