본문 바로가기

미래(2015-2016)/자습

자판기



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
    2015.09.02
    내용: 자판기 프로그램 작성하기
            돈을 넣으세요 : 1000원
            1. 블랙커피 150원    2. 믹스커피 200원    3. 고급커피 300원
            4. 사탕 100원    5. 초컬릿 200원    6. 과자 300원
            원하는 음료를 고르세요 : 3
            원하는 스넥을 고르세요 : 6
            거스름돈 : 100원 4개...
            +) 무한반복, 돈이 적으면 자동으로 거스름돈 반환 (혹은 q/Q입력), 돈이 적으면 돈을 더 넣어달라고 함
*/
 
#include <stdio.h>
 
 
int main()
{
    int coin, temp, change, select;
    int w10, w50, w100, w500, w1000;
    char selectMenu[200= "";
    char *whatSelect;
 
    printf("******************************************************************\n\n");
    printf("    ☆    ★            ☆    ★\n\n");
    printf("            맛있는 간식 자판기        ☆★  \n\n");
    printf("  ★         ★☆        ★          ☆\n\n");
    printf("******************************************************************\n\n\n");
 
 
 
    while (1)
    {
        printf(" 돈을 넣어 주세요 →  ");
        scanf("%d"&coin);
        temp = coin;
        fflush(stdin);
 
        if (coin < 100)
        {
            printf("\n\n");
            printf("              === 자판기 판매목록 === \n\n");
            printf("      블랙커피 150원       믹스커피 200원       고급커피 300원    \n");
            printf("      사탕     100원       초콜릿   200원       과자     300원        \n\n");
            printf("\n\n");
            printf(" 돈이 부족합니다 다시 넣어 주세요\n\n");
            continue;
        }
        else
            break;
    }
 
    printf("\n\n");
    printf("              === 자판기 판매목록 === \n\n");
    printf("   1. 블랙커피 150원    2. 믹스커피 200원    3. 고급커피 300원    \n");
    printf("   4. 사탕     100원    5. 초콜릿   200원    6. 과자     300원        \n\n");
 
    while (1)
    {
        printf(" 메뉴를 골라주세요 돈을 반환하려면 0을 눌러주세요    →  ");
        scanf("%d"&select);
        fflush(stdin);
        printf("\n");
 
        if (select == 0)
            break;
 
        switch (select)
        {
        case 1:
            if (temp >= 150)
            {
                temp -= 150;
                whatSelect = "블랙커피 ";
                strcat(selectMenu, whatSelect);
                printf(" 현재 선택한 메뉴: %s \n", selectMenu);
            }
            else
                printf(" 잔액이 부족합니다. \n");
 
            break;
        case 2:
            if (temp >= 200)
            {
                temp -= 200;
                whatSelect = "믹스커피 ";
                strcat(selectMenu, whatSelect);
                printf(" 현재 선택한 메뉴: %s \n", selectMenu);
            }
            else
                printf(" 잔액이 부족합니다. \n");
 
            break;
        case 3:
            if (temp >= 300)
            {
                temp -= 300;
                whatSelect = "고급커피 ";
                strcat(selectMenu, whatSelect);
                printf(" 현재 선택한 메뉴: %s \n", selectMenu);
            }
            else
                printf(" 잔액이 부족합니다. \n");
 
            break;
        case 4:
            if (temp >= 100)
            {
                temp -= 100;
                whatSelect = "사탕 ";
                strcat(selectMenu, whatSelect);
                printf(" 현재 선택한 메뉴: %s \n", selectMenu);
            }
            else
                printf(" 잔액이 부족합니다. \n");
 
            break;
        case 5:
            if (temp >= 200)
            {
                temp -= 200;
                whatSelect = "초콜릿 ";
                strcat(selectMenu, whatSelect);
                printf(" 현재 선택한 메뉴: %s \n", selectMenu);
            }
            else
                printf(" 잔액이 부족합니다. \n");
 
            break;
        case 6:
            if (temp >= 300)
            {
                temp -= 300;
                whatSelect = "과자 ";
                strcat(selectMenu, whatSelect);
                printf(" 현재 선택한 메뉴: %s \n", selectMenu);
            }
            else
                printf(" 잔액이 부족합니다. \n");
            
            break;
        default:
            printf(" 잘못입력하셨습니다. 다시 입력해주세요.\n");
        }
 
        if (temp < 100)
            break;
    }
 
            change = temp;
 
            w1000 = temp / 1000;
            temp %= 1000;
            w500 = temp / 500;
            temp %= 500;
            w100 = temp / 100;
            temp %= 100;
            w50 = temp / 50;
            temp %= 50;
            w10 = temp / 10;
 
            printf("******************************************************************\n\n");
            printf(" 총 선택한 메뉴: %s \n", selectMenu);
            printf(" 총 금액: %d원\n\n", coin - change);
            
            if (change > 0)
            {
                printf(" 거스름돈은 %d원으로 ", change);
                w1000 == 0 ? printf("") : printf("1000원 %d장 ", w1000);
                w500 == 0 ? printf("") : printf("500원 %d개 ", w500);
                w100 == 0 ? printf("") : printf("100원 %d개 ", w100);
                w50 == 0 ? printf("") : printf("50원 %d개 ", w50);
                w10 == 0 ? printf("") : printf("10원 %d개 ", w10);
                printf("가 나왔습니다.\n\n");
            }
            else
                printf(" 거스름돈은 0원입니다. \n\n");
 
            printf("******************************************************************\n\n");
 
    return 0;
}
 
cs


c150902_slotMachine.c


'미래(2015-2016) > 자습' 카테고리의 다른 글

전기요금 계산하기  (0) 2015.09.07
계산기  (0) 2015.09.03
배열을 정렬하여 병합하기  (0) 2015.09.01
포인터를 이용한 배열복사  (0) 2015.09.01
정수 두 개와 연산자 받아 계산하기  (0) 2015.08.28