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

+ Recent posts