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 {
protected:
virtual double convert(double src) override;
virtual string getSourceString() override;
virtual string getDestString() override;
public:
WonToDollar(int ratio);
};
WonToDollar::WonToDollar(double ratio) : Converter(ratio) {};
string WonToDollar::getSourceString() {
return "원";
}
string WonToDollar::getDestString() {
return "달러";
}
double WonToDollar::convert(double src) {
return src / ratio;
}
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 ratio);
protected:
virtual double convert(double src) override;
virtual string getSourceString() override;
virtual string getDestString() override;
};
KmToMile::KmToMile(double ratio) :Converter(ratio) {};
double KmToMile::convert(double src) {
return src / ratio;
}
string KmToMile::getSourceString() {
return "Km";
}
string KmToMile::getDestString() {
return "Mile";
}
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);
protected:
virtual int calculate() override;
};
ForLoopAdder::ForLoopAdder(string name) : LoopAdder(name) {};
int ForLoopAdder::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);
protected:
virtual int calculate() override;
};
class DoWhileLoopAdder : public LoopAdder {
public:
DoWhileLoopAdder(string name);
protected:
virtual int calculate() override;
};
WhileLoopAdder::WhileLoopAdder(string name) : LoopAdder(name) {};
int WhileLoopAdder::calculate() {
int sum = 0;
int i = getX();
while (i <= getY()) {
sum += i;
i++;
}
return sum;
}
DoWhileLoopAdder::DoWhileLoopAdder(string name) : LoopAdder(name) {};
int DoWhileLoopAdder::calculate() {
int sum = 0;
int i = getX();
do {
sum += i;
i++;
} while (i <= getY());
return sum;
}
int main() {
WhileLoopAdder whileLoop("While Loop");
DoWhileLoopAdder doWhileLoop("Do while Loop");
whileLoop.run();
doWhileLoop.run();
}
5. 디지털 회로에서 기본적인 게이트로 OR 게이트,AND 게이트,X0R 게이트 등이 있다. 이들은 각각 두 입력 신호를 받아 0R 연산, AND 연산, X0R 연산을 수행한 결괴를 출 력한다. 이 게이트들을 각각 ORGate, XORGate, ANDGate 클래스로 작성하고자 한다. ORGate, XORGate, ANDGate 클래스가 A b s tra c tG a te를 상속받도록 작성하라.

#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 ORGate: public AbstractGate {
public:
bool operation();
};
class XORGate: public AbstractGate {
public:
bool operation();
};
class ANDGate: public AbstractGate {
public:
bool operation();;
};
bool ORGate::operation() {
return x | y;
}
bool XORGate::operation() {
return x ^ y;
}
bool ANDGate::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;
return 0;
}
6.
#include <iostream>
#include <string>
using namespace std;
class AbstractStack {
public:
virtual bool push(int n) = 0; // 스택에 n 을푸시한다.스택이 f u l l 이면 fa ls e 리턴
virtual bool pop(int& n) = 0; // 스택에서 팝한 정수를 n에 저장하고 스택이 empty이면 fa lse 리턴
virtual int size() = 0;// 현재스택에 저장된 정수의 개수 리턴
};
class IntStack :public AbstractStack {
int tos;
int capacity;
int* p;
public:
IntStack(int size);
~IntStack();
bool push(int n);
bool pop(int& n);
int size();
};
IntStack::IntStack(int capacity) {
this->capacity = capacity;
tos = 0;
p = new int[capacity];
if (!p) {
cout << "heap 메모리 영역 부족 -> 동적 할당 실패" << endl;
exit(1);
}
}
IntStack::~IntStack() {
delete[] p;
}
bool IntStack::push(int n) {
if (tos >= capacity)
return false;
else {
p[tos++] = n;
return true;
}
}
bool IntStack::pop(int& n) {
if (tos == 0) return false;
tos--;
n = p[tos];
return true;
}
int IntStack::size() {
return tos;
}
int main() {
IntStack a(5);
for (int i = 0; i < 10; i++) { // 처음 5 개를 성공적으로 push되고 다음 5 개는 스택 full로 push 실패
if (a.push(i)) cout << "push 성공" << endl;
else cout << "스택 full" << endl;
}
int n;
for (int i = 0; i < 10; i++) { // 처음 5 개를 성공적으로 pop되고 다음 5 개는 스택 empty로 pop 실패
if (a.pop(n)) cout << "pop 성공 " << n << endl;
else cout << "스택 empty" << endl;
}
}
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() = 0;
string getName() { return name; }
};
class Oval : public Shape {
public:
Oval(string name = "", int width = 0, int height = 0);
protected:
double getArea() override;
};
Oval::Oval(string name, int width, int height) :Shape(name, width, height) {};
double Oval::getArea() {
return width * height * 3.14;
}
class Rect : public Shape {
public:
Rect(string name = "", int width = 0, int height = 0);
protected:
double getArea() override;
};
Rect::Rect(string name, int width, int height) :Shape(name, width, height) {};
double Rect::getArea() {
return width * height;
}
class Triangular : public Shape {
public:
Triangular(string name = "", int width = 0, int height = 0);
protected:
double getArea() override;
};
Triangular::Triangular(string name, int width, int height) :Shape(name, width, height) {};
double Triangular::getArea() {
return width * height / 2.0;
}
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];
}'C++' 카테고리의 다른 글
| [명품 C++ Programming] 11장 실습 (0) | 2025.06.16 |
|---|---|
| [명품 C++ Programming] 10장 실습 문제 (0) | 2025.05.30 |
| [명품 C++ Programming] 8장 실습 문제 (0) | 2025.05.24 |
| [명품 C++ Programming] 7장 실습 문제 (0) | 2025.05.24 |
| [명품 C++ Programming] 6장 실습 문제 (0) | 2025.04.28 |