실습문제
1번
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
Circle(int radius = 0) { 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() {
NamedCircle waffle(3, "waffle");
waffle.show();
}
2번
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
Circle(int radius = 0) { 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 = 0, string name = "") : Circle(radius) {
this->name = name;
}
void show() {
cout << "반지름이 " << getRadius() << "인 " << name << endl;
}
void setName(string name) { this->name = name; }
string getName() { return name; }
};
int main() {
NamedCircle pizza[5];
cout << "5 개의 정수 반지름과 원의 이름을 입력하세요." << endl;
for (int i = 0; i < 5; i++) {
int tempNum;
string tempStr;
cout << i + 1 << ">> ";
cin >> tempNum >> tempStr;
pizza[i].setRadius(tempNum);
pizza[i].setName(tempStr);
}
int maxRadius = -1, maxIdx;
for (int i = 0; i < 5; i++) {
if (maxRadius < pizza[i].getRadius()) {
maxRadius = pizza[i].getRadius();
maxIdx = i;
}
}
cout << "가장 면적이 큰 피자는 " << pizza[maxIdx].getName();
}
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) {
move(x, y);
}
void setColor(string color) {
this->color = color;
}
void show() {
cout << color << "색으로 (" << getX() << ", " << getY()
<< ")에 위치한 점입니다.";
}
};
int main() {
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) {
move(x, y);
}
void setColor(string color) {
this->color = color;
}
void show() {
cout << color << "색으로 (" << getX() << ", " << getY()
<< ")에 위치한 점입니다." << endl;
}
};
int main() {
ColorPoint zeroPoint;
zeroPoint.show();
ColorPoint cp(5, 5, "RED");
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}
5번
#include <iostream>
#include <string>
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 index;
int tmpIndex;
public:
MyQueue(int capacity) : BaseArray(capacity) { index = 0; }
void enqueue(int n) {
put(index, n);
index++;
}
int length() {
return index;
}
int capacity() {
return getCapacity();
}
int dequeue() {
index--;
return get(tmpIndex++);
}
};
int main() {
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>
#include <string>
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 {
int index;
public:
MyStack(int capacity) : BaseArray(capacity) { index = 0; }
void push(int n) {
put(index, n);
index++;
}
int length() {
return index;
}
int capacity() {
return getCapacity();
}
int pop() {
return get(--index);
}
};
int main() {
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>
#include <string>
using namespace std;
class BaseMemory {
char* mem;
protected:
BaseMemory(int size) { mem = new char[size]; }
void setArr(char* arr, int length) {
for (int i = 0; i < length; i++) {
mem[i] = arr[i];
}
}
char getWord(int index) {
return mem[index];
}
void setWord(int index, char ch) {
mem[index] = ch;
}
};
class ROM : public BaseMemory {
public:
ROM(int size, char* arr, int length) : BaseMemory(size) {
setArr(arr, length);
}
char read(int index) {
return getWord(index);
}
};
class RAM : public BaseMemory {
public:
RAM(int size) : BaseMemory(size) {}
char read(int index) {
return getWord(index);
}
void write(int index, char ch) {
setWord(index, ch);
}
};
int main() {
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);
return 0;
}
8번
#include <iostream>
#include <string>
using namespace std;
class Printer {
string model, manufacturer;
int printedCount, availableCount;
public:
Printer(string model, string manufacturer, int printedCount, int availableCount) {
this->model = model;
this->manufacturer = manufacturer;
this->printedCount = printedCount;
this->availableCount = availableCount;
}
bool print(int pages) {
if (availableCount >= pages) {
printedCount += pages;
availableCount -= pages;
return true;
}
return false;
}
void show() {
cout << model << ", " << manufacturer << ", 남은 종이 " << availableCount << "장, ";
}
};
class LaserPrinter : public Printer {
int toner;
public:
LaserPrinter(string model, string manufacturer, int availableCount, int availableToner, int printedCount = 0) :
Printer(model, manufacturer, printedCount, availableCount) {
toner = availableToner;
}
bool printLaser(int pages) {
if (print(pages)) {
toner -= pages;
}
return print(pages);
}
void showLaser() {
show();
cout << "남은 토너 " << toner << endl;
}
};
class InkjetPrinter : public Printer {
int ink;
public:
InkjetPrinter(string model = "", string manufacturer = "", int availableCount = 0, int availableInk = 0, int printedCount = 0) :
Printer(model, manufacturer, printedCount, availableCount) {
ink = availableInk;
}
bool printInk(int pages) {
if (print(pages)) {
ink -= pages;
}
return print(pages);
}
void showInk() {
show();
cout << "남은 잉크 " << ink << endl;
}
};
int main() {
InkjetPrinter Iprint("Officejet V40", "HP", 5, 10);
LaserPrinter Lprint("SCX-6x45", "삼성전자", 3, 20);
cout << "현재 작동중인 2대의 프린터는 아래와 같다." << endl;
cout << "잉크젯 : "; Iprint.showInk();
cout << "레이저 : "; Lprint.showLaser();
while (true) {
cout << endl;
cout << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>";
int temp1, temp2 = 0, isVaild = 0;
cin >> temp1 >> temp2;
if (temp1 == 1) {
if (Iprint.printInk(temp2)) {
isVaild = 1;
}
}
else if (temp1 == 2) {
if (Lprint.printLaser(temp2)) {
isVaild = 1;
}
}
else {
break;
}
if (isVaild == 1) {
cout << "프린트 하였습니다." << endl;
}
else {
cout << "용지가 부족하여 프린트 할 수 없습니다." << endl;
}
Iprint.showInk();
Lprint.showLaser();
char ch;
cout << "계속 프린트 하시겠습니까? (y/n) >>";;
cin >> ch;
if (ch == 'n') break;
else continue;
}
}
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 arrSize, time;
Seat* seat;
public:
Schedule(int time = 0, int arrSize = 8) {
this->time = time;
this->arrSize = arrSize;
seat = new Seat[arrSize];
}
void setTime(int time) {
this->time = time;
}
int getTime() { return time; }
void reserve(int seatNum, string name) {
seat[seatNum - 1].setName(name);
}
bool cancel(int seatNum, string name) {
if (seat[seatNum - 1].getName() != name) {
cout << "일치하지 않는 승객명" << endl;
return false;
}
seat[seatNum].setName("---");
return true;
}
void show() {
for (int i = 0; i < arrSize; i++) {
cout << seat[i].getName() << "\t";
}
cout << endl;
}
};
class AirlineBook {
int arrSize;
Schedule* schedule;
public:
AirlineBook(int arrSize = 3) {
this->arrSize = arrSize;
schedule = new Schedule[arrSize];
schedule[0].setTime(7);
schedule[1].setTime(12);
schedule[2].setTime(17);
}
void showSchedule(int arrNum) {
cout << schedule[arrNum - 1].getTime() << "시\t", schedule[arrNum - 1].show();
}
void ReserveSchedule(int time,int num, string name) {
schedule[time - 1].reserve(num, name);
}
void showAllSchedule() {
for (int i = 0; i < 3; i++) {
cout << schedule[i].getTime() << "시\t", schedule[i].show();
}
}
};
class Console {
AirlineBook hansung;
public:
void run() {
cout << "***** 한성 항공에 오신 것을 환영합니다 *****" << endl;
while (true) {
int temp;
printMenu();
cin >> temp;
if (temp == 1) {
resMenu();
}
else if (temp == 2) {
canMenu();
}
else if (temp == 3) {
showMenu();
}
else {
break;
}
}
}
void printMenu() {
cout << "예약:1, 취소:2, 보기:3, 끝내기:4>> ";
}
void printRes() {
cout << "07시:1. 12시:2. 17시:3>> ";
}
void resMenu() {
int tempRes, tempSeat;
string tempName;
printRes();
cin >> tempRes;
hansung.showSchedule(tempRes);
cout << "좌석 번호>> ";
cin >> tempSeat;
cout << "이름 입력 >> ";
cin >> tempName;
hansung.ReserveSchedule(tempRes, tempSeat, tempName);
}
void canMenu() {
int tempRes, tempSeat;
string tempName;
printRes();
cin >> tempRes;
hansung.showSchedule(tempRes);
cout << "좌석 번호>> ";
cin >> tempSeat;
cout << "이름 입력 >> ";
cin >> tempName;
hansung.ReserveSchedule(tempRes, tempSeat, "---");
}
void showMenu() {
hansung.showAllSchedule();
}
};
int main() {
Console s;
s.run();
}
'대학교 수업 > C++ 프로그래밍' 카테고리의 다른 글
[C++] 명품 C++ 프로그래밍 8장 OpenChallenge (0) | 2022.06.09 |
---|---|
[C++] 명품 C++ 프로그래밍 7장 OpenChallenge (0) | 2022.06.09 |
[C++] 명품 C++ 프로그래밍 7장 (0) | 2022.06.08 |
[C++] 명품 C++ 프로그래밍 6장 (0) | 2022.06.07 |
[C++] 명품 c++ 프로그래밍 5장 (0) | 2022.04.20 |