본문 바로가기
외대생의 코딩이야기

[개인학습자료] C언어 - 학생 성적 프로그램 ver.2 (c언어 구조체 사용)

by Jason.IM 2020. 3. 27.
728x90

학생 성적 프로그램


<C언어 - 학생성적 프로그램 ver.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
 
// 공용체를 이용한 신상정보
// 성적시스템
 
typedef struct student_grade {
    int grade; //등수
    int type; // 입력타입
    union // 검색조건
    {
        int school_num;
        char *name = NULL;
    }student;
    int point; // 점수
}infor;
 
void menu(void);
void input(infor a[], int num); // 입력시, 학번or이름으로 저장
void search(infor a[], int num); // 검색시, 타입에 의해 학번or이름으로 검색
void all_print(infor a[], int num); // 등수대로 나열
void del(infor a[], int num); // 삭제시, 타입에 의해 학번or이름으로 찾아서 삭제
void view(infor a[], int num); // 새화면
void swap(int a, int b);
 
void menu(void// 메뉴보기
{
    puts("----------------------------------------------------");
    puts("0.입력 1.검색 2.전체보기 3.삭제 4.새화면 그외:나가기");
    puts("----------------------------------------------------");
    puts("");
}
 
int main()
{
    infor x[30];
    int num = 0;
 
    void(*pf[5])(infor[], int= { input,search,all_print,del,view };
    int slelct;
 
    while (1)
    {
        menu();
 
        printf("메뉴를 참고하여 값을 입력하시오.\n");
        scanf("%d",&slelct);
 
        if (slelct < 0 || slelct>4)
            break;
 
        pf[slelct](x, num);
        if (slelct == 0)
            num++;
        if (slelct == 3)
            num--;
    }
 
 
 
    return 0;
}
 
void input(infor a[], int num) // 입력하기
{
    a[num].grade = num+1;
 
    do
    {
        printf("입력타입(1.학번/2.이름) : ");
        scanf("%d"&a[num].type);
 
        if (a[num].type == 1)
        {
            printf("학번을 입력하시오 : ");
            scanf("%d"&a[num].student.school_num);
        }
        else if (a[num].type == 2)
        {
            printf("이름을 입력하시오 : ");
            scanf("%s"&a[num].student.name);
        }
        else
        {
            puts("잘못된 수가 입력되었습니다.");
        }
    } while (a[num].type != 1 && a[num].type != 2);
 
    printf("점수를 입력하시오 : ");
    scanf("%d"&a[num].point);
 
    puts("");
 
}
 
void search(infor a[], int num)
{
    int tmp_point;
    char *tmp_name = NULL;
 
    int judge;
    int judge_num;
    char *judge_name = NULL;
    int i;
    int key = 0;
 
    for (i = 0; i < num; i++// 정렬하기
    {
        for (int j = 0; j < num-1; j++)
        {
            if (a[j].point < a[j + 1].point)
            {
                
                swap(a[j].type, a[j + 1].type);
 
                swap(a[j].student.school_num, a[j + 1].student.school_num);
 
                /*tmp_school_num = a[j].student.school_num;
                a[j].student.school_num = a[j + 1].student.school_num;
                a[j + 1].student.school_num = tmp_school_num;*/
 
                tmp_name = a[j].student.name;
                a[j].student.name = a[j + 1].student.name;
                a[j + 1].student.name = tmp_name;
 
                tmp_point = a[j].point;
                a[j].point = a[j + 1].point;
                a[j + 1].point = tmp_point;
            }
        }
    }
    
    do // 값 검색
    {
        printf("찾는 값의 입력타입은? : ");
        scanf("%d"&judge);
 
        if (judge == 1)
        {
            printf("학번을 입력하시오 : ");
            scanf("%d"&judge_num);
        }
        else if (judge == 2)
        {
            printf("이름을 입력하시오 : ");
            scanf("%s"&judge_name);
        }
        else
        {
            puts("잘못된 수가 입력되었습니다.");
        }
    } while (judge != 1 && judge != 2);
 
 
    if (judge == 1// 해당 값 출력
    {
        for (i = 0; i <= num; i++)
        {
            if (a[i].student.school_num == judge_num)
            {
                printf("%d등 - 학번 : %d / 점수 : %d \n", a[i].grade, a[i].student.school_num, a[i].point);
                key++;
            }
        }
 
        if (key == 0)
            puts("찾지못하였습니다.");
    }
 
    if (judge == 2)
    {
        for (i = 0; i <= num; i++)
        {
            if (strcmp(a[i].student.name, judge_name) == 0)
            {
                printf("%d등 - 이름 : %s / 점수 : %d \n", a[i].grade, a[i].student.name, a[i].point);
                key++;
            }
        }
 
        if (key == 0)
            puts("찾지못하였습니다.");
    }
}
 
void all_print(infor a[], int num) // 전체보기
{
    int tmp_point;
    char *tmp_name = NULL;
 
 
    for (int i = 0; i < num; i++// 정열하기
    {
        for (int j = 0; j < num-1; j++)
        {
            if (a[j].point < a[j + 1].point)
            {
                swap(a[j].type, a[j + 1].type);
                /*for (int x = 0; x < num; x++)
                {
                    printf("%d ", a[x].type);
                }
                puts("");*/
 
                swap(a[j].student.school_num, a[j + 1].student.school_num);
 
                /*tmp_school_num = a[j].student.school_num;
                a[j].student.school_num = a[j + 1].student.school_num;
                a[j + 1].student.school_num = tmp_school_num;*/
                /*for (int x = 0; x < num; x++)
                {
                    printf("%d ", a[x].student.school_num);
                }
                puts("");*/
 
                tmp_name = a[j].student.name;
                a[j].student.name = a[j + 1].student.name;
                a[j + 1].student.name = tmp_name;
 
                tmp_point = a[j].point;
                a[j].point = a[j + 1].point;
                a[j + 1].point = tmp_point;
                /*for (int x = 0; x < num; x++)
                {
                    printf("%d ", a[x].point);
                }
                puts("");*/
 
            }
        }
    }
    
    puts("");
 
    for (int j = 0; j <= num; j++// 전체보기
    {
        if (a[j].type == 1)
        {
            printf("%d등 - 학번 : %d / 점수 : %d \n", a[j].grade, a[j].student.school_num, a[j].point);
        }
 
        if (a[j].type == 2)
        {
            printf("%d등 - 이름 : %s / 점수 : %d \n", a[j].grade, a[j].student.name, a[j].point);
        }
    }
 
    puts("");
}
 
void del(infor a[], int num) // 삭제하기
{
    int tmp_point;
    char *tmp_name = NULL;
 
    int judge;
    int judge_num;
    char *judge_name = NULL;
 
    int i;
    int key = 0;
    int del_code;
 
    for (i = 0; i < num; i++// 정렬하기
    {
        for (int j = 0; j < num - 1; j++)
        {
            if (a[j].point < a[j + 1].point)
            {
 
                swap(a[j].type, a[j + 1].type);
 
                swap(a[j].student.school_num, a[j + 1].student.school_num);
 
                /*tmp_school_num = a[j].student.school_num;
                a[j].student.school_num = a[j + 1].student.school_num;
                a[j + 1].student.school_num = tmp_school_num;*/
 
                tmp_name = a[j].student.name;
                a[j].student.name = a[j + 1].student.name;
                a[j + 1].student.name = tmp_name;
 
                tmp_point = a[j].point;
                a[j].point = a[j + 1].point;
                a[j + 1].point = tmp_point;
            }
        }
    }
 
    do // 삭제할 값 찾기
    {
        printf("삭제할 값의 입력타입은? : ");
        scanf("%d"&judge);
 
        if (judge == 1)
        {
            printf("학번을 입력하시오 : ");
            scanf("%d"&judge_num);
        }
        else if (judge == 2)
        {
            printf("이름을 입력하시오 : ");
            scanf("%s"&judge_name);
        }
        else
        {
            puts("잘못된 수가 입력되었습니다.");
        }
    } while (judge != 1 && judge != 2);
 
 
    if (judge == 1// 값 삭제
    {
        for (i = 0; i <= num; i++)
        {
            if (a[i].student.school_num == judge_num)
            {
                del_code = i;
 
                printf("학번 : %d 의 데이터를 삭제했습니다.\n", a[i].student.school_num);
                key++;
            }
        }
 
        if (key == 0)
            puts("찾지못하였습니다.");
    }
 
    if (judge == 2)
    {
        for (i = 0; i <= num; i++)
        {
            if (strcmp(a[i].student.name, judge_name) == 0)
            {
                del_code = i;
 
                printf("이름 : %s 의 데이터를 삭제했습니다.\n", a[i].student.name);
                key++;
            }
        }
 
        if (key == 0)
            puts("찾지못하였습니다.");
    }
 
    for (int j = del_code; j < num; j++// 삭제한 빈칸 채우기
    {
 
        a[j].type = a[j + 1].type;
        if(a[j].type==1)
            a[j].student.school_num = a[j + 1].student.school_num;
        if (a[j].type == 2)
            a[j].student.name = a[j + 1].student.name;
        a[j].point = a[j + 1].point;
    }
 
    a[num].type = NULL;
    a[num].grade = NULL;
    a[num].student.school_num = NULL;
    a[num].student.name = NULL;
    a[num].point = NULL;
 
    puts("");
}
 
void view(infor a[], int num) // 새창보기
{
    system("cls");
}
 
void swap(int a, int b)
{
    int tmp;
 
    tmp = a;
    a = b;
    a = tmp;
}
cs

(본 프로그램은 저자 본인인 직접만든 프로그램임을 명시합니다.)

 

(위 포스팅에 사용된 이미지는 저작권이 해결된 이미지임을 밝힙니다.)

 

 

728x90