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
#include <iostream>
using namespace std;
 
struct Animal
{
    char name[20];
    char feature[20];
    Animal* yes;
    Animal* no;
};
 
void print(Animal* Data)
{
    char yesno;
    char yesno2;
 
    cout << Data->feature << "? \n";
    cin >> yesno;
 
    // 특징이 n이면 no에 바로 등록
    if (yesno == 'n')
    {
        if (Data->no == NULL)
        {
            Animal* NewData = new Animal;
 
            cout << "뭐야? \n";
            cin >> NewData->name;
 
            cout << "특징은? \n";
            cin >> NewData->feature;
 
            NewData->yes = NULL;
            NewData->no = NULL;
 
            Data->no = NewData;
        }
        else
        {
            print(Data->no);
        }
    }
    // 틀징이 y이면
    else
    {
        cout << Data->name << "? \n";
        cin >> yesno2;
        // 이름이 n인지 확인
        if (yesno2 == 'n')
        {
            if (Data->yes == NULL)
            {
                Animal* NewData = new Animal;
 
                cout << "뭐야? \n";
                cin >> NewData->name;
 
                cout << "특징은? \n";
                cin >> NewData->feature;
 
                NewData->yes = NULL;
                NewData->no = NULL;
 
                Data->yes = NewData;
            }
            else
                print(Data->yes);
        }
        // 이름이 맞으면 아무일도하지않음. main문에서 반복문 처음으로 돌아가서 print(head)실행
    }
}
 
void main()
{
    Animal* head = NULL;
    Animal* NewData = new Animal;
 
    cout << "뭐야? \n";
    cin >> NewData->name;
 
    cout << "특징은? \n";
    cin >> NewData->feature;
 
    NewData->yes = NULL;
    NewData->no = NULL;
 
    while (1)
    {
        // head가 널이면 새로운값 넣어줌
        if (head == NULL)
        {
            head = NewData;
        }
        // 아니면 print 함수 호출
        else
        {
            print(head);
            
        }
    }
}
cs

트리를 이용한 스무고개 예제이다....

어떻게 돌아가는지는 실행해보면 알 수 있다... 오래되서 기억이 안남

'프로그래밍 공부 > C++' 카테고리의 다른 글

클래스 사용예제  (0) 2018.03.21
객체지향, 클래스  (0) 2018.03.21
Tree (재귀함수 이용)  (0) 2018.03.21
Linked List (구조체 이용)  (0) 2018.03.21
구조체(struct)  (0) 2018.03.21
Posted by misty_
,