실습문제
1번
#include <iostream>
#include <string>
using namespace std;
class Converter {
protected:
double ratio;
virtual double convert(double src) = 0;
virtual string getSourceString() = 0;
virtual string getDestString() = 0;
public:
Converter(double ratio) { this->ratio = ratio; }
void run() {
double src;
cout << getSourceString() << "을 " << getDestString() << "로 바꿉니다. ";
cout << getSourceString() << "을 입력하세요>> ";
cin >> src;
cout << "변환 결과: " << convert(src) << getDestString() << endl;
}
};
class WonToDollar : public Converter {
public:
WonToDollar(double money) : Converter(money) {}
double convert(double src) { return src / ratio; }
string getSourceString(){ return "원"; }
string getDestString() { return "달러"; }
};
int main() {
WonToDollar wd(1010);
wd.run();
}
2번
#include <iostream>
#include <string>
using namespace std;
class Converter {
protected:
double ratio;
virtual double convert(double src) = 0;
virtual string getSourceString() = 0;
virtual string getDestString() = 0;
public:
Converter(double ratio) { this->ratio = ratio; }
void run() {
double src;
cout << getSourceString() << "을 " << getDestString() << "로 바꿉니다. ";
cout << getSourceString() << "을 입력하세요>> ";
cin >> src;
cout << "변환 결과: " << convert(src) << getDestString() << endl;
}
};
class KmToMile : public Converter {
public:
KmToMile (double Km) : Converter(Km) {}
string getSourceString() {
return "Km";
}
string getDestString() {
return "Mile";
}
double convert(double src) {
return src / ratio;
}
};
int main() {
KmToMile toMile(1.609344);
toMile.run();
}
3번
#include <iostream>
#include <string>
using namespace std;
class LoopAdder {
string name;
int x, y, sum;
void read();
void write();
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() {
cout << name << ":" << endl;
cout << "처음 수에서 두번째 수까지 더합니다. 두 수를 입력하세요 >> ";
cin >> x >> y;
}
void LoopAdder::write() {
cout << x << "에서 " << y << "까지의 합 = " << sum << "입니다." << endl;
}
void LoopAdder::run() {
read();
sum = calculate();
write();
}
class ForLoopAdder : public LoopAdder {
public:
ForLoopAdder(string name = "") : LoopAdder(name){}
int calculate() {
int sum = 0;
for (int i = getX(); i <= getY(); i++) {
sum += i;
}
return sum;
}
};
int main() {
ForLoopAdder forLoop("For Loop");
forLoop.run();
}
4번
#include <iostream>
#include <string>
using namespace std;
class LoopAdder {
string name;
int x, y, sum;
void read();
void write();
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() {
cout << name << ":" << endl;
cout << "처음 수에서 두번째 수까지 더합니다. 두 수를 입력하세요 >> ";
cin >> x >> y;
}
void LoopAdder::write() {
cout << x << "에서 " << y << "까지의 합 = " << sum << "입니다." << endl;
}
void LoopAdder::run() {
read();
sum = calculate();
write();
}
class WhileLoopAdder : public LoopAdder {
public:
WhileLoopAdder(string name = "") : LoopAdder(name){}
int calculate() {
int sum = 0, index = getX();
while (index <= getY()) {
sum += index;
index++;
}
return sum;
}
};
class DoWhileLoopAdder : public LoopAdder {
public:
DoWhileLoopAdder(string name = "") : LoopAdder(name) {}
int calculate() {
int sum = 0, index = getX();
do {
sum += index;
index++;
} while (index <= getY());
return sum;
}
};
int main() {
WhileLoopAdder whileLoop("While Loop");
DoWhileLoopAdder dowhileLoop("Do while Loop");
whileLoop.run();
dowhileLoop.run();
}
5번
#include <iostream>
#include <string>
using namespace std;
class AbstractGate {
protected:
bool x, y;
public:
void set(bool x, bool y) { this->x = x; this->y = y; }
virtual bool operation() = 0;
};
class ANDGate : public AbstractGate {
public:
bool operation() {
return x && y;
}
};
class ORGate : public AbstractGate {
public:
bool operation() {
return x || y;
}
};
class XORGate : public AbstractGate {
public:
bool operation() {
return x ^ y;
}
};
int main() {
ANDGate andGate;
ORGate orGate;
XORGate xorGate;
andGate.set(true, false);
orGate.set(true, false);
xorGate.set(true, false);
cout.setf(ios::boolalpha);
cout << andGate.operation() << endl;
cout << orGate.operation() << endl;
cout << xorGate.operation() << endl;
}
6번
#include <iostream>
#include <string>
using namespace std;
class AbstractStack {
public:
virtual bool push(int n) = 0;
virtual bool pop(int& n) = 0;
virtual int size() = 0;
};
class IntStack : public AbstractStack {
public:
int arr[6] = { 0 };
int index = -1;
bool push(int n) {
if (index >= -1 && index < 5) {
++index;
arr[index] = n;
return true;
}
return false;
}
bool pop(int& n) {
if (index > 0 && index < 5) {
index--;
n = arr[index];
return true;
}
return false;
}
int size() {
return index;
}
void show() {
for (int i = 0; i <= index; i++) {
cout << arr[i] << ' ';
}
cout << endl;
}
};
int main() {
IntStack intStack;
intStack.push(1);
intStack.push(2);
intStack.push(3);
intStack.show();
int n;
intStack.pop(n);
intStack.show();
}
7번
#include <iostream>
#include <string>
using namespace std;
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; }
string getname() { return name; }
};
class Oval : public Shape {
public:
Oval(string name = "", int w = 0, int h = 0) : Shape(name, w, h){ }
double getArea() {
return width * height * 3.141592;
}
};
class Rect : public Shape {
public:
Rect(string name = "", int w = 0, int h = 0) : Shape(name, w, h) { }
double getArea() {
return width * height;
}
};
class Triangular : public Shape {
public:
Triangular(string name = "", int w = 0, int h = 0) : Shape(name, w, h) { }
double getArea() {
return width * height / 2;
}
};
int main() {
Shape* p[3];
p[0] = new Oval("빈대떡", 10, 20);
p[1] = new Rect("찰떡", 30, 40);
p[2] = new Triangular("토스트", 30, 40);
for (int i = 0; i < 3; i++) {
cout << p[i]->getname() << " 넓이는 " << p[i]->getArea() << endl;
}
for (int i = 0; i < 3; i++) delete p[i];
}
8번
#include <iostream>
#include <string>
using namespace std;
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() = 0;
string getname() { return name; }
};
class Oval : public Shape {
public:
Oval(string name = "", int w = 0, int h = 0) : Shape(name, w, h){ }
double getArea() {
return width * height * 3.141592;
}
};
class Rect : public Shape {
public:
Rect(string name = "", int w = 0, int h = 0) : Shape(name, w, h) { }
double getArea() {
return width * height;
}
};
class Triangular : public Shape {
public:
Triangular(string name = "", int w = 0, int h = 0) : Shape(name, w, h) { }
double getArea() {
return width * height / 2;
}
};
int main() {
Shape* p[3];
p[0] = new Oval("빈대떡", 10, 20);
p[1] = new Rect("찰떡", 30, 40);
p[2] = new Triangular("토스트", 30, 40);
for (int i = 0; i < 3; i++) {
cout << p[i]->getname() << " 넓이는 " << p[i]->getArea() << endl;
}
for (int i = 0; i < 3; i++) delete p[i];
}
9번
#include <iostream>
#include <string>
using namespace std;
class Printer {
string model, manufacturer;
int printedCount, availableCount;
public:
Printer(string m = "", string mf = "", int aC = 0) {
model = m;
manufacturer = mf;
printedCount = 0;
availableCount = aC;
}
virtual void print(int pages) = 0;
virtual void show() = 0;
string getModel() {
return model;
}
string getManufacturer() {
return manufacturer;
}
int getPrintedCount() {
return printedCount;
}
int getAvailableCount() {
return availableCount;
}
void setPrintedCount(int n) {
printedCount = n;
}
void setAvailableCount(int n) {
availableCount = n;
}
};
class Inkjet : public Printer {
int ink;
public:
Inkjet(string m = "", string mf = "", int aC = 0, int ink = 0) : Printer(m, mf, aC) {
this->ink = ink;
}
void print(int pages) {
if (getAvailableCount() > pages) {
if (ink > pages) {
setPrintedCount(getPrintedCount() + pages);
setAvailableCount(getAvailableCount() - pages);
ink -= pages;
}
else {
cout << "잉크가 부족하여 프린트할 수 없습니다." << endl;
}
}
else {
cout << "용지가 부족하여 프린트할 수 없습니다." << endl;
}
}
void show() {
cout << getModel() << ", " << getManufacturer() << ", 남은 종이 " << getAvailableCount() << "장, 남은 잉크 " << ink << endl;
}
};
class Laser : public Printer {
int toner;
public:
Laser(string m = "", string mf = "", int aC = 0, int toner = 0) : Printer(m, mf , aC) {
this->toner = toner;
}
void print(int pages) {
if (getAvailableCount() > pages) {
if (toner > pages) {
setPrintedCount(getPrintedCount() + pages);
setAvailableCount(getAvailableCount() - pages);
toner -= pages;
}
else {
cout << "잉크가 부족하여 프린트할 수 없습니다." << endl;
}
}
else {
cout << "용지가 부족하여 프린트할 수 없습니다." << endl;
}
}
void show() {
cout << getModel() << ", " << getManufacturer() << ", 남은 종이 " << getAvailableCount() << "장, 남은 토너 " << toner << endl;
}
};
int main() {
Inkjet* ink = new Inkjet("Officejet V40", "HP", 5, 10);
Laser* laser = new Laser("SCX-6x45", "삼성전자", 3, 20);
cout << "현재 작동중인 2대의 프린터는 아래와 같다" << endl;
cout << "잉크젯 : ";
ink->show();
cout << "레이저 : ";
laser->show();
int printer, paper;
char ch;
while (true) {
cout << endl << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>";
cin >> printer >> paper;
switch (printer) {
case 1: ink->print(paper); break;
case 2: laser->print(paper); break;
default: break;
}
ink->show();
laser->show();
cout << "게속 프린트 하시겠습니까(y/n)>>";
cin >> ch;
if (ch == 'n') break;
else continue;
}
return 0;
}
10번
#include <iostream>
#include <string>
using namespace std;
class Shape {
Shape* next;
protected:
virtual void draw() = 0;
public:
Shape() { next = NULL; }
virtual ~Shape() {}
Shape* add(Shape* p) {
this->next = p;
return p;
}
void paint() {
draw();
}
Shape* getNext() { return next; }
};
class Line : public Shape {
protected:
virtual void draw() {
cout << "Line" << endl;
}
};
class Circle : public Shape {
protected:
virtual void draw() {
cout << "Circle" << endl;
}
};
class Rect : public Shape {
protected:
virtual void draw() {
cout << "Rectangle" << endl;
}
};
class UI {
public:
static int getMenu() {
int key;
cout << "삽입:1, 삭제:2, 모두보기:3, 종료:4 >>";
cin >> key;
return key;
}
static int getShapeTypeToInsert() {
int key;
cout << "선:1, 원:2, 사각형:3 >>";
cin >> key;
return key;
}
static int getShapeIndexToDelete() {
int key;
cout << "삭제하고자 하는 도형의 인덱스 >>";
cin >> key;
return key;
}
};
class GraphicEditor {
Shape* pStart;
Shape* pLast;
int cnt;
public:
GraphicEditor() { pStart = NULL; cnt = 0; }
void create(int num) {
if (num == 1) {
if (cnt == 0) {
pStart = new Line();
pLast = pStart;
}
else {
pLast = pLast->add(new Line());
}
}
else if (num == 2) {
if (cnt == 0) {
pStart = new Circle();
pLast = pStart;
}
else {
pLast = pLast->add(new Circle());
}
}
else if (num == 3) {
if (cnt == 0) {
pStart = new Rect();
pLast = pStart;
}
else {
pLast = pLast->add(new Rect());
};
}
cnt++;
cout << cnt;
}
void deleteShape(int index) {
Shape* pre = pStart;
Shape* tmp = pStart;
if (pStart == NULL) {
cout << "도형이 없습니다" << endl;
}
else {
for (int i = 1; i < index; i++) {
pre = tmp;
tmp = tmp->getNext();
}
if (tmp == pStart) {
pStart = tmp->getNext();
delete tmp;
}
else {
pre->add(tmp->getNext());
delete tmp;
}
}
}
void show() {
Shape* tmp = pStart;
for (int i = 0; i < cnt; i++) {
cout << i << ": ";
tmp->paint();
tmp = tmp->getNext();
}
}
void run() {
cout << "그래픽 에디터입니다." << endl;
int menu, index, type;
while (true) {
menu = UI::getMenu();
switch (menu) {
case 1:
type = UI::getShapeTypeToInsert();
create(type);
break;
case 2:
index = UI::getShapeIndexToDelete();
deleteShape(index);
break;
case 3:
show();
break;
default:
return;
}
}
}
};
int main() {
GraphicEditor graphicEditor;
graphicEditor.run();
}
'대학교 수업 > C++ 프로그래밍' 카테고리의 다른 글
[C++] 명품 C++ 프로그래밍 10장 (0) | 2022.06.09 |
---|---|
[C++] 명품 C++ 프로그래밍 9장 OpenChallenge (0) | 2022.06.09 |
[C++] 명품 C++ 프로그래밍 8장 OpenChallenge (0) | 2022.06.09 |
[C++] 명품 C++ 프로그래밍 7장 OpenChallenge (0) | 2022.06.09 |
[C++] 명품 C++ 프로그래밍 8장 (0) | 2022.06.08 |