1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
class Accumulator {
    int value;
public:
    Accumulator(int value) { this->value = value; } // 매개 변수 value로 멤버 value를 초기화한다.
    Accumulator& add(int n) { value += n; return (*this); } // value에 n을 더해 값을 누적한다.
    int get() { return value; } // 누적된 값 value를 리턴한다.
};
 
int main() {
    Accumulator acc(10);
    acc.add(5).add(6).add(7);
    cout << acc.get() << endl;
}
cs


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
#include <iostream>
using namespace std;
 
class MyIntStack {
    int *p;
    int size;
    int tos;
public:
    MyIntStack();
    MyIntStack(int size) { this->size = size; p = new int[size]; tos = 0; }
    MyIntStack(MyIntStack& s) { this->= new int[s.size]; size = s.size; tos = s.tos; for (int i = 0; i < tos; i++) { p[i] = s.p[i]; } }
    ~MyIntStack() { delete[]p; }
    bool push(int n); // 정수 n을 스택에 푸시한다.
            // 스택이 꽉 차 있으면 false를, 아니면 true 리턴
    bool pop(int &n); // 스택의 탑에 있는 값을 n에 팝한다.
            // 만일 스택이 비어 있으면 false를, 아니면 true 리턴
};
 
bool MyIntStack::push(int n) {
    if (tos != size) {
        p[tos] = n;
        tos++;
        return true;
    }
    else return false;
}
 
bool MyIntStack::pop(int &n) {
    if (tos != 0) {
        tos--;
        n = p[tos];
        return true;
    }
    else return false;
}
 
int main() {
    MyIntStack a(10);
    a.push(10);
    a.push(20);
    MyIntStack b = a;
    b.push(30);
 
    int n;
    a.pop(n);
    cout << "스택 a에서 팝한 값 " << n << endl;
    b.pop(n);
    cout << "스택 b에서 팝한 값 " << n << endl;
}
cs


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
#include <iostream>
using namespace std;
 
class MyIntStack {
    int p[10];
    int tos;
public:
    MyIntStack() { tos = 0; }
    bool push(int n);
    bool pop(int &n);
};
 
bool MyIntStack::push(int n) {
    if (tos != 10) {
        p[n] = n;
        tos++;
        return true;
    }
    else {
        return false;
    }
}
 
bool MyIntStack::pop(int &n) {
    if (tos != 0) {
        tos--;
        n = p[tos];
        return true;
    }
    else return false;
}
 
int main() {
    MyIntStack a;
    for (int i = 0; i < 11; i++) {
        if (a.push(i)) cout << i << ' ';
        else cout << endl << i + 1 << " 번째 stack full" << endl;
    }
    int n;
    for (int i = 0; i < 11; i++) {
        if (a.pop(n)) cout << n << ' ';
        else cout << endl << i + 1 << " 번째 stack empty";
    }
    cout << endl;
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
 
char& find(char a[], char c, bool& success) {
    int i, size = strlen(a);
    for (i = 0; i < size; i++) {
        a[i] == c ? success = true : success = false;
        break;
    }
    return a[i];
}
 
int main() {
    char s[] = "Mike";
    bool b = false;
    char& loc = find(s, 'M', b);
    if (b == false) {
        cout << "M을 발견할 수 없다" << endl;
        return 0;
    }
    loc = 'm';
    cout << s << endl;
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
 
class Circle {
    int radius;
public:
    Circle(int r) { radius = r; }
    int getRadius() { return radius; }
    void setRadius(int r) { radius = r; }
    void show() { cout << "반지름이 " << radius << "인 원" << endl; }
};
 
void increaseBy(Circle &a, Circle &b) {
    int r = a.getRadius() + b.getRadius();
    a.setRadius(r);
}
 
int main() {
    Circle x(10), y(5);
    increaseBy(x, y);
    x.show();
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
 
bool bigger(int a, int b, int &big) {
    if (a == b) return true;
    else {
        b > a ? big = b : big = a;
        return false;
    }
}
 
int main(){
    int a, b, max;
    cout << "두 개의 정수를 입력하시오>> ";
    cin >> a >> b;
    if (bigger(a, b, max)) cout << "두 정수가 같습니다." << endl;
    else cout << "두 정수중 더 큰 것은 " << max << "입니다." << endl;
};
cs


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 = 1) { this->radius = radius; }
    void swap(Circle &a, Circle &b);
    int getRadius() { return radius; }
};
 
void Circle::swap(Circle &a, Circle &b) {
    int tmp;
    tmp = a.radius;
    a.radius = b.radius;
    b.radius = tmp;
}
 
int main() {
    Circle a(10), b(20);
    cout << "a원의 반지름 : " << a.getRadius() << ", b원의 반지름 : " << b.getRadius() << endl;
    swap(a, b);
    cout << "a원의 반지름 : " << a.getRadius() << ", b원의 반지름 : " << b.getRadius() << endl;
}
cs


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
#include <iostream>
#include <string>
using namespace std;
 
class Morse {
    string alphabet[26];
    string digit[10];
    string slash, question, comma, period, plus, equal;
public:
    Morse();
    void text2Morse(string text, string& morse);
    bool morse2Text(string morse, string& text);
};
 
Morse::Morse() {
    string alphabet[26= { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
                            "---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
    string digit[10= { "-----",".----","..---","...--","....-",".....","-....","--...","---..","----." };
    for (int i = 0; i < 26; i++) {
        this->alphabet[i] = alphabet[i];
    }
    for (int i = 0; i < 10; i++) {
        this->digit[i] = digit[i];
    }
    slash = "-..-."; question = "..--.."; comma = "--..--"; period = ".-.-.-"; plus = ".-.-."; equal = "-...-";
}
 
void Morse::text2Morse(string text, string& morse) {
    int size = text.length();
    morse = "";
    for (int i = 0; i < size; i++) {
        char c = text[i];
        if (c == '/')        morse = morse + slash + " ";
        else if (c == '?')    morse = morse + question + " ";
        else if (c == ',')    morse = morse + comma + " ";
        else if (c == '.')    morse = morse + period + " ";
        else if (c == '+')    morse = morse + plus + " ";
        else if (c == '=')    morse = morse + equal + " ";
        else if (isdigit(c)) morse = morse + digit[c - 48+ " ";
        else if (isalpha(c)) {
            if (isupper(c)) {
                morse = morse + alphabet[c - 65+ " ";
            }
            else if (islower(c)) {
                morse = morse + alphabet[c - 97+ " ";
            }
        }
        else if (c == ' ')    morse += "   ";
    }
}
 
bool Morse::morse2Text(string morse, string &text) {
    string bufMorse;
    text2Morse(text, bufMorse);
    if (bufMorse == morse) return true;
    else return false;
}
 
 
int main() {
    string text, morse;
    Morse m;
    cout << "아래에 영문 텍스트를 입력하세요. 모스 부호로 바꿉니다." << endl;
    getline(cin, text);
    m.text2Morse(text, morse);
    cout << morse << endl << endl;
    cout << "모스 부호를 다시 영문 텍스트로 바꿉니다." << endl;
    if (m.morse2Text(morse, text)) cout << text << endl;
    else cout << "오류 발생!" << endl;
}
cs


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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
 
class Player {
    string name;
public:
    void setName(string name) { this->name = name; }
    string getName() { return name; }
};
 
class GamblingGame {
    Player *p;
    int size;
public:
    GamblingGame();
    ~GamblingGame() { delete[]p; }
    void playgame();
};
 
GamblingGame::GamblingGame() {
    cout << "플레이어 숫자를 입력하세요>>";
    cin >> size;
    cout << endl;
    this->size = size;
    p = new Player[size];
    srand((unsigned)time(0));
}
 
void GamblingGame::playgame() {
    cout << "***** 갬블링 게임을 시작합니다. *****" << endl;
    string name;
    int index = 0;
    int number[3];
    for (int i = 0; i < size; i++) {
        cout << i + 1 << "번째 선수 이름>>";
        cin >> name;
        p[i].setName(name);
    }
    cin.ignore();    // 버퍼 비우기
    while (1) {
        if (index == size) index = 0;
        cout << p[index].getName() << ":<Enter>";
        getline(cin, name);
        cout << "\t\t";
        for (int i = 0; i < 3; i++) {
            number[i] = rand() % 3;
            cout << number[i] << "\t";
        }
        if (number[0== number[1&& number[1== number[2]) {
            cout << p[index].getName() << "님 승리!!" << endl;
            break;
        }
        else {
            cout << "아쉽군요!" << endl;
            index++;
        }
    }
}
 
int main() {
    GamblingGame user;
    user.playgame();
}
cs

플레이어 수를 2명이 아닌 사용자에게 입력받도록 변형

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
 
class Player {
    string name;
public:
    void setName(string name) { this->name = name; }
    string getName() { return name; }
};
 
class GamblingGame {
    Player *p;
public:
    GamblingGame() { p = new Player[2]; srand((unsigned)time(0)); }
    ~GamblingGame() { delete[]p; }
    void playgame();
};
 
void GamblingGame::playgame() {
    cout << "***** 갬블링 게임을 시작합니다. *****" << endl;
    string name;
    int index = 0;
    int number[3];
    cout << "첫번째 선수 이름>>";
    cin >> name;
    p[0].setName(name);
    cout << "두번째 선수 이름>>";
    cin >> name;
    p[1].setName(name);
    cin.ignore();    // 버퍼 비우기
    while (1) {
        if (index == 2) index = 0;
        cout << p[index].getName() << ":<Enter>";
        getline(cin, name);
        cout << "\t\t";
        for (int i = 0; i < 3; i++) {
            number[i] = rand() % 3;
            cout << number[i] << "\t";
        }
        if (number[0== number[1&& number[1== number[2]) {
            cout << p[index].getName() << "님 승리!!" << endl;
            break;
        }
        else {
            cout << "아쉽군요!" << endl;
            index++;
        }
    }
}
 
int main() {
    GamblingGame user;
    user.playgame();
}
cs


+ Recent posts