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

#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 n = "", int w = 0, int h = 0) :Shape(n, w, h) { ; }

    double getArea() {

        return width * height*3.14;

    }

};

class Rect :public Shape {

public:

    Rect(string n = "", int w = 0, int h = 0) :Shape(n, w, h) { ; }

    double getArea() {

        return width * height;

    }

};

class Triangular :public Shape {

public:

    Triangular(string n = "", int w = 0, int h = 0) :Shape(n, 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];

}

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 7번  (0) 2019.06.14
명품 c++ 9장 실습문제 6번  (0) 2019.06.14
명품 c++ 9장 실습문제 5번  (0) 2019.06.14
명품 c++ 9장 실습문제 3,4 번  (0) 2019.06.14
명품 c++ 9장 실습문제 1,2 번  (0) 2019.06.14

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

#include <iostream>

#include <string>

using namespace std;

 

class Shape {

protected:

    string name;

    int width, height;

public:

    Shape(string n = ""int w = 0int h = 0) {

        name = n;

        width = w;

        height = h;

    }

    virtual double getArea() { return 0; }

    string getName() { return name; }

};

 

class Oval :public Shape {

public:

    Oval(string n = ""int w = 0int h = 0) :Shape(n, w, h) { ; }

    double getArea() {

        return width * height*3.14;

    }

};

 

class Rect :public Shape {

public:

    Rect(string n = ""int w = 0int h = 0) :Shape(n, w, h) { ; }

    double getArea() {

        return width * height;

    }

};

 

class Triangular :public Shape {

public:

    Triangular(string n = ""int w = 0int h = 0) :Shape(n, w, h) { ; }

    double getArea() {

        return width * height/2;

    }

};

 

int main() {

    Shape *p[3];

    p[0= new Oval("빈대떡"1020);

    p[1= new Rect("찰떡"3040);

    p[2= new Triangular("토스트"3040);

    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];

}

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 7번  (0) 2019.06.14
명품 c++ 9장 실습문제 6번  (0) 2019.06.14
명품 c++ 9장 실습문제 5번  (0) 2019.06.14
명품 c++ 9장 실습문제 3,4 번  (0) 2019.06.14
명품 c++ 9장 실습문제 1,2 번  (0) 2019.06.14

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

#include <iostream>

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 {

    int *stack;    // 배열 포인터 변수 생성

    int stack_size;

    int top = -1; // 스텍 위치

public:

    IntStack(int size) {    // IntStack 생성자로 n 만큼 동적할당

        stack_size = size;

        stack = new int[size];

    }

    int size() {        // 스텍의 정수 갯수

        return top+2;

    }

    bool push(int n){

        top++;

        stack[top] = n;

        if (top + 1 == stack_size) return false;

        else return true;

    }

    bool pop(int &n) {

        n = stack[top];

        top--;

        if (top + 1 == stack_size) return false;

        else return true;

    }

};

int main() {

    int pop_value;

    IntStack stack(3);                    // stack 크기 3

    cout << stack.push(3)<<endl;        // stack 에 3 삽입 top = 0

    cout << stack.push(5) << endl;        // stack 에 5 삽입 top = 1

    cout << stack.push(4) << endl;        // stack 에 4 삽입 top = 2

    

    cout << stack.pop(pop_value)<<endl;        // 첫번째 pop  4 나옴

    cout << pop_value << endl;

    cout << stack.pop(pop_value) << endl;        // 첫번째 pop     5 나옴

    cout << pop_value << endl;

    cout << stack.pop(pop_value) << endl;        // 첫번째 pop    3 나옴

    cout << pop_value << endl;

}

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 7번  (0) 2019.06.14
명품 c++ 9장 실습문제 7번  (0) 2019.06.14
명품 c++ 9장 실습문제 5번  (0) 2019.06.14
명품 c++ 9장 실습문제 3,4 번  (0) 2019.06.14
명품 c++ 9장 실습문제 1,2 번  (0) 2019.06.14

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

#include <iostream>

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() {

        if (x == 1 && y == 1return true;

        else return false;

    }

};

 

class ORGate :public AbstractGate {

public:

    bool operation() {

        if (x == 1 || y == 1return true;

        else return false;

    }

};

 

class XORGate :public AbstractGate {

public:

    bool operation() {

        if (x != y) return true;

        else return false;

    }

};

 

int main() {

    ANDGate andGate;

    ORGate orGate;

    XORGate xorGate;

 

 

    andGate.set(truefalse);

    orGate.set(truefalse);

    xorGate.set(truefalse);

    cout.setf(ios::boolalpha);

    

    cout << andGate.operation() << endl;

    cout << orGate.operation() << endl;

    cout << xorGate.operation() << endl;

}

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 7번  (0) 2019.06.14
명품 c++ 9장 실습문제 6번  (0) 2019.06.14
명품 c++ 9장 실습문제 3,4 번  (0) 2019.06.14
명품 c++ 9장 실습문제 1,2 번  (0) 2019.06.14
명품 c++ 8장 실습문제 7번  (0) 2019.06.10

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

-- 실습 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) { ; }

protected:

    int calculate() {

        int sum_ = 0;

        for (int i = this->getX();i <= this->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) { ; }

protected:

    int calculate() {

        int counter = getX();

        int sum_ = 0;

        while (counter <= getY()) {

            sum_ += counter;

            counter++;

        }

        return sum_;

    }

};

 

class DoWhileLoopAdder :public LoopAdder {

public:

    DoWhileLoopAdder(string name = "") :LoopAdder(name) { ; }

protected:

    int calculate() {

        int counter = getX();

        int sum_ = 0;

        do {

            sum_ += counter;

            counter++;

        } while (counter <= getY());

        return sum_;

    }

 

};

 

int main() {

    WhileLoopAdder whileLoop("while Loop");

    DoWhileLoopAdder doWhileLoop("Do while Loop");

 

    whileLoop.run();

    doWhileLoop.run();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 6번  (0) 2019.06.14
명품 c++ 9장 실습문제 5번  (0) 2019.06.14
명품 c++ 9장 실습문제 1,2 번  (0) 2019.06.14
명품 c++ 8장 실습문제 7번  (0) 2019.06.10
명품 c++ 8장 실습문제 6번  (0) 2019.06.10

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

--실습 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:

 

    double convert(double src) {

 

        return src / ratio;

 

    }

 

    string getSourceString() {

 

        return "원";

 

    }

 

    string getDestString() {

 

        return "달러";

 

    }

 

public:

 

    WonToDollar(double ratio) : Converter(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) :Converter(ratio) { ; }

 

protected:

 

    double convert(double src) {

 

        return src / ratio;

 

    }

 

    string getSourceString() {

 

        return "Km";

 

    }

 

    string getDestString() {

 

        return "Mile";

 

    }

 

};

 

int main() {

 

    KmToMile toMile(1.609344);

 

    toMile.run();

 

}

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 5번  (0) 2019.06.14
명품 c++ 9장 실습문제 3,4 번  (0) 2019.06.14
명품 c++ 8장 실습문제 7번  (0) 2019.06.10
명품 c++ 8장 실습문제 6번  (0) 2019.06.10
명품 c++ 8장 실습문제 5번  (0) 2019.06.10

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

#include <iostream>

using namespace std;

 

class BaseMemory {

    char *mem;

protected:

    BaseMemory(int size) { mem = new char[size]; }

    void setMemory(int mem_num, char val) { this->mem[mem_num] = val; }

public:

    char read(int mem_index) {

        return this->mem[mem_index];

    }

};

class ROM :public BaseMemory {

public:

    ROM(int sizechar *x, int mem_index) :BaseMemory(size) {

        for (int i = 0;i < mem_index;i++) {

            this->setMemory(i, x[i]);

        }

    }

};

class RAM :public BaseMemory {

public:

    RAM(int size) : BaseMemory(size) {;}

    void write(int mem_index, char val);

};

void RAM::write(int mem_index, char val) {

    this->setMemory(mem_index, val);

}

int main() {

    char x[5= { 'h','e','l','l','o' };

    ROM boisROM(1024 * 10, x, 5);

    RAM mainMemory(1024 * 1024);

 

    for (int i = 0;i < 5;i++) {

        mainMemory.write(i, boisROM.read(i));

    }

    for (int i = 0;i < 5;i++) {

        cout << mainMemory.read(i);

    }

}

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 3,4 번  (0) 2019.06.14
명품 c++ 9장 실습문제 1,2 번  (0) 2019.06.14
명품 c++ 8장 실습문제 6번  (0) 2019.06.10
명품 c++ 8장 실습문제 5번  (0) 2019.06.10
명품 c++ 7장 실습문제 9번  (0) 2019.06.06

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

#include <iostream>

#include <string>

using namespace std;

 

class BaseArray {

    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 top;

public:

    MyStack(int capacity = 100) {

        this->top = -1;

    }

    void push(int val);

    int pop();

    int length();

    int capacity();

};

void MyStack::push(int val) {

    this->top++;

    this->put(this->top, val);

}

int MyStack::pop() {

    this->top--;

    return this->get(top + 1);

}

int MyStack::length() {

    return this->top+1;

}

int MyStack::capacity() {

    return this->getCapacity();

}

 

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;

}

 

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 9장 실습문제 1,2 번  (0) 2019.06.14
명품 c++ 8장 실습문제 7번  (0) 2019.06.10
명품 c++ 8장 실습문제 5번  (0) 2019.06.10
명품 c++ 7장 실습문제 9번  (0) 2019.06.06
명품 c++ 7장 실습문제 8번  (0) 2019.06.06

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

#include <iostream>

#include <string>

using namespace std;

 

class BaseArray {

    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;

    int front;

    int queue_counter;

public:

    MyQueue(int capacity = 100) : BaseArray(capacity) { this->rear = -1;this->front = -1;this->queue_counter = 0; }

    void enqueue(int val);

    int dequeue();

    int length();

    int capacity();

};

void MyQueue::enqueue(int val) {

    this->queue_counter++;

    this->rear++;

    this->put(this->rear, val);

}

int MyQueue::dequeue() {

    this->queue_counter--;

    this->front++;

    return this->get(this->front);

}

int MyQueue::length() {

    return this->queue_counter;

}

int MyQueue::capacity() {

    return this->getCapacity();

}

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;

}

 

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 8장 실습문제 7번  (0) 2019.06.10
명품 c++ 8장 실습문제 6번  (0) 2019.06.10
명품 c++ 7장 실습문제 9번  (0) 2019.06.06
명품 c++ 7장 실습문제 8번  (0) 2019.06.06
명품 c++ 7장 실습문제 6번  (0) 2019.06.06

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include <iostream>

using namespace std;

 

class Circle {

    int radius;

public:

    Circle(int radius = 0) {

        this->radius = radius;

    }

    void show() { cout << "radius = " << radius << "인 원" << endl; }

    friend Circle operator+(int op, Circle op2);

};

Circle operator+(int op, Circle op2) {

    Circle tmp;

    tmp.radius = op + op2.radius;

    return tmp;

}

 

int main() {

    Circle a(5), b(4);

    b = 1 + a;

    a.show();

    b.show();

}

Colored by Color Scripter

cs

'C++' 카테고리의 다른 글

명품 c++ 8장 실습문제 6번  (0) 2019.06.10
명품 c++ 8장 실습문제 5번  (0) 2019.06.10
명품 c++ 7장 실습문제 8번  (0) 2019.06.06
명품 c++ 7장 실습문제 6번  (0) 2019.06.06
명품 c++ 7장 실습문제 5번  (0) 2019.06.06

+ Recent posts