1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
int main(){
    int array[][3= { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
    int* ptr = array;
    int (*pptr)[3= array;
 
    printf("%d\n"*ptr);
    printf("%d\n"*(ptr + 5));
    printf("%d\n", (*pptr)[2]);
    printf("%d\n"*(pptr[3]));
    printf("%d\n",**(pptr + 1));
    printf("%d\n", array[0]);
    printf("%d\n"*(array[0]+2));
}
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
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
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <time.h>
 
void intro(); // 스플래시 
void com_mkrand(); // 컴퓨터 난수 생성,저장
int human_input(); // 플레이어 숫자 입력, 난수와 비교
int Avoid_duplication(); // 중복 숫자 입력 체크
int Avoid_range(); // 인풋 범위 체크
void count_reset(); // 카운트 리셋
 
int output[4], input[4], strike = 0, ball = 0, out = 0, count = 0// 리턴을 주는것보다 전역함수로 제어하는게 효율적이라고 판단
 
int main() {
    int exit;
    srand((unsigned)time(NULL)); // 매번 다른 난수 발생 위해 시드값 부여
    intro(); // 스플래시
 
    com_mkrand(); // 난수 발생
 
    exit = human_input(); // 플레이어 숫자 입력
    if (exit == 0// -1 입력시 종료
        return 0;
 
    std::cout << "\n                  " << count << "\a회 만에 당신이 이겼습니다! \n" << "                  정답 - ";
 
    for (int i = 0; i < 4; i++)
        std::cout << output[i] << " ";
    std::cout << "\n\n";
}
 
void com_mkrand() {
    output[0= rand() % 10// 첫번째 자리0부터 9까지 난수 생성
    while (1) {
        output[1= rand() % 10// 첫번째 자리와 중복되지 않는 난수 발생할때 까지 반복
        if (output[1!= output[0])
            break;
    }
    while (1) { // 왼쪽에 위치한 숫자들과 중복되지 않는 난수 발생할때 까지 반복
        output[2= rand() % 10;
        if (output[2!= output[0] && output[2!= output[1])
            break;
    }
    while (1) { // 왼쪽에 위치한 숫자들과 중복되지 않는 난수 발생할때 까지 반복
        output[3= rand() % 10;
        if (output[3!= output[0] && output[3!= output[1] && output[3!= output[2])
            break;
    }
    std::cout.width(20);
    for (int i = 0; i < 4; i++)
        std::cout << output[i] << " ";
    std::cout << "\n";
}
 
int human_input() {
    int num; // 중복숫자입력 체크용
    while (strike < 4) {
        count++// 게임 횟수 체크
        count_reset();
        std::cout << count << "회 플레이어 숫자 입력 (스페이스 바로 자릿수 입력 -1 입력시 종료):";
        std::cin >> input[0];
        if (input[0== -1// 종료 플래그
            return 0;
        std::cin >> input[1>> input[2>> input[3];
 
        num = Avoid_duplication(); // 중복, 범위 검사
 
        if (num == 1) {
        case1:
            std::cout << count << "회 플레이어 10보다 작은 중복 되지 않는 숫자 입력 (스페이스 바로 자릿수 입력 -1 입력시 종료):";
            std::cin >> input[0];
            if (input[0== -1// 종료 플래그
                return 0;
            std::cin >> input[1>> input[2>> input[3];
            num = Avoid_duplication(); // 중복, 범위 재검사
            if (num == 1// 검사 통과시 num 0반환, 못할 시 1반환. num 1반환 시 다시 입력
                goto case1;
        }
 
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (input[i] == output[i]) {
                    strike++// 스트라이크 카운트
                    break;
                }
                if (input[i] == output[j]) {
                    ball++// 볼 카운트
                    break;
                }
            }
        }
        out = 4 - strike - ball; // 아웃 카운트
        std::cout << "-----------------------------------------------------------------\n";
        std::cout.width(19);
        std::cout << strike << " 스트라이크! " << ball << " 볼! " << out << " 아웃!\n";
        std::cout << "-----------------------------------------------------------------\n";
    }
}
 
int Avoid_duplication() { // 중복 숫자 검사 함수
    int num;
    count_reset();
    for (int i = 0, j = 1; i < 3; i++) { // 중복 숫자 검사
        j = i + 1;
        for (; j < 4; j++) {
            if (input[i] == input[j]) {
                ball++;
            }
        }
    }
 
    if (ball > 0) { // 중복 시 1 반환
        count_reset();
        return 1;
    }
    else { // 중복이 아닐시
        count_reset();
        ball = Avoid_range(); // 10보다 큰 수가 있는지 확인 후
        if (ball > 0)
            return 1// 있을 시 1 반환
        else
            return 0// 없을 시 0 반환
    }
}
 
void count_reset() {
    strike = 0; ball = 0; out = 0// 카운트 리셋 함수
}
 
int Avoid_range() { // 범위 검사 함수
    count_reset();
    for (int i = 0; i < 4; i++) { // 사용자 input이 10보다 큰 수가 있는지 검사
        if (input[i] >= 10)
            ball++// 있을 시 ball 카운트
    }
    if (ball > 0) { // 10보다 큰 수 있을 시 1 반환
        count_reset();
        return 1;
    }
    else { // 없으면  0반환
        count_reset();
        return 0;
    }
}
 
void intro() { // 스플래시
    std::cout << "┌──────────────────────────────────┐\n";
    std::cout << "│                           숫자야구                                 │\n";
    std::cout << "│        윈도우 프로그래밍           김종완 교수님                   │\n";
    std::cout << "│                                                                    │\n";
    std::cout << "│                             developed by - 고상욱(20130889)        │\n";
    std::cout << "└──────────────────────────────────┘\n";
    std::cout.width(24);
    std::cout << "Wait";
    std::cout << ".";
    std::cout << ".";
    std::cout << ".\n";
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main() {
    char input[100], temp=0;
    while(1) {
        cout << "입력:";
        cin >> input;
 
        for (int i = 0; input[i] != NULL; i++) {
            temp = (temp < input[i]) ? input[i] : temp;
        }
 
        cout << temp<<"\n";
    }
}
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
#include <iostream>
using namespace std;
 
int main() {
    int a=0;
    char num[100], temp[4];
 
    cout << "문자열 입력:";
    cin >> num;
 
    for (int i = 0; num[i]!=0; i++) {
        a++;
    }
    a -= 1;
    cout << num[a]<<"\n";
 
    for (int i = 0; a > 0; a--,i++) {
        if (a - i == 1 || a==i)
            break;
        * temp = num[a];
        num[a] = num[i];
        num[i] = * temp;
    }
    cout << num;
}
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
#include <iostream>
#include <time.h>
#include <Windows.h>
 
void main(){
    int a[50]={0},input,i=0,j=49, count=0;
    srand(time(NULL));
    while(1) {
        input=rand() % 9 + 1;
        if (input%2 == 1) {
            a[i]=input;//홀수
            for(int i=0; i<=49; i++)
                std::cout<<a[i];
            i++;
            std::cout<<"\n";
        }
        else {
            a[j]=input;//짝수
            for(int i=0; i<=49; i++)
                std::cout<<a[i];
            j--;
            std::cout<<"\n";
        }
        count++;
        if (count==49) {
            for(int i=0; i<=49; i++
                a[i]=0;
            i=0,j=49;
            count=0;
        }
        Sleep(50);
    }    
}
cs


윈도우 프로그래밍 실습


난수를 발생시켜 홀수면 왼쪽, 짝수면 오른쪽에 저장한다


난수 50개가 전부 차면 0으로 초기화 한다

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
#include <iostream>
void main(){
    int a[10]={0},input,i=0,j=9, count=1;
 
    while(1) {
        std::cout<<count<<"입력: ";
        std::cin>>input;
        if (input == 0)
            break;
        if (input%2 == 1) {
            a[i]=input;//홀수
            for(int i=0; i<=9; i++)
                std::cout<<a[i];
            i++;
            std::cout<<"\n";
        }
        else {
            a[j]=input;//짝수
            for(int i=0; i<=9; i++)
                std::cout<<a[i];
            j--;
            std::cout<<"\n";
        }
        count++;
        if (a[5!= 0)
            break;
    }
    for(int i=0; i<=9; i++)
        std::cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
cs

윈도우 프로그래밍 실습


사용자에게 입력받아 홀수면 왼쪽, 짝수면 오른쪽에 저장한다


난수 50개가 전부 차면 0으로 초기화 한다


사용자로부터 두개의 숫자를 입력받아 구구단을 출력하는 프로그램을 만들어 보자. 


예를 들어 사용자가 3과 5를 입력하면, 3단, 4단, 5단을 출력해야 한다.


단 사용자의 입력 순서가 자유로워야 한다. 3과 5를 입력하던 5와 3을 입력하던 같은 결과를 출력해야 한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
 
int main() {
    int input1, input2, temp = 0;
 
    std::cout << "두 개의 숫자 입력: ";
    std::cin >> input1 >> input2;
 
    if (input1 > input2) {
        input2 = temp;
        input2 = input1;
        input1 = temp;
    }
 
    for (; input1 <= input2; input1++) {
        for (int i = 1; i <= 9; i++) {
            std::cout << input1 << " X " << i << " = " << input1*<< "\n";
        }
        std::cout << "\n\n";
    }
}
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
#include <iostream>
#include <time.h> //time 사용을 위한 헤더파일
 
int main(int argc, const char * argv[]) {
    int human, computer, option, totalgame = 0, hw = 0, cw = 0, hl = 0, cl = 0, hh = 0, cc = 0;
    srand(time(NULL)); //매번 다른 난수 발생을 위해 시드값 사용 *time을 이용해 시간을 시드값으로 사용
    do { // 게임을 한번 시작 후 종료 트리거 실행을 위해 do while문 사용
        option = 0// 2,147,483,648 이상 입력시 무한루프에 빠지는데 0 초기화값을 주면 루프돌지 않고 종료됨
        std::cout << "계속 할꺼면 1 입력 (0 입력시 종료)";
        std::cin >> option; // 0 입력시 종료하고 그 외 숫자, 문자 입력시 게임 시작
        human = rand() % 6 + 1// 인간 1부터 6까지 난수 발생 후 저장
        computer = rand() % 6 + 1// 컴퓨터 1부터 6까지 난수 발생 후 저장
        if (human>computer) {
            std::cout << human << ":" << computer << " 이세돌 승\n";
            hw += 1, cl += 1// hw - 인간 승, cl - 컴퓨터 배패
        }
        else if (computer>human) {
            std::cout << human << ":" << computer << " 알파고 승\n";
            cw += 1, hl += 1// cw - 컴퓨터 승, hl - 인간 패배
        }
        else {
            std::cout << human << ":" << computer << " 무승부\n";
            hh += 1, cc += 1// hh, cc - 무승부
        }
        totalgame += 1// 총 게임수 계산
    } while (option != 0); // 0 입력시 종료
    std::cout.precision(3); // 승률 소수점 세자리 고정 
    std::cout << "\n이세돌 승:" << hw << " 패:" << hl << " 무:" << hh << " 승률:" << (double)hw / (hl + hh) << "\n";
    std::cout << "\n알파고 승:" << cw << " 패:" << cl << " 무:" << cc << " 승률:" << (double)cw / (cl + cc) << "\n";
    std::cout << "\n총 " << totalgame << "판함\n\n";
    return 0;
}
cs


열심히 주석을 달아놨으니 이해가 안되시면 참고하세요...


주사위 난수출력을 위해 rand() 함수를 사용하는데 매번 다른 난수 출력을 위해선 srand() 함수를 추가해야 합니다.


만약 srand() 함수를 사용하지 않는다면 프로그램 종료 후 재실행시 매번 같은 난수가 출력됩니다.

+ Recent posts