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

[개인학습자료] C언어 - 함수포인터의 배열을 이용한 성적프로그램 (ver.3)

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

함수포인터의 배열을 이용한 성적프로그램

<C언어 - 학생성적처리 프로그램 ver.3>

 

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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
 
// 성적시스템
 
typedef struct student_grade {
    int grade; //등수
    int school_num; // 학번
    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 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;
}
 
 
 
 
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
void input(infor a[], int num) // 입력하기
{
    a[num].grade = num + 1;
    int key;
 
    do
    {
        key = 0;
        
        printf("학번을 입력하시오 : ");
        scanf("%d"&a[num].school_num);
        
        if(a[num].school_num < 20100000 || a[num].school_num>20209999)
        {
            puts("잘못된 수가 입력되었습니다.");
        }
 
        for (int i = 0; i < num; i++// 출력
        {
            if (a[i].school_num == a[num].school_num)
            {
                puts("이미 입력되어있는 학번입니다.");
                key++;
            }
        }
 
    } while (a[num].school_num < 20100000 || a[num].school_num>20209999 || key != 0);
 
    printf("점수를 입력하시오 : ");
    scanf("%d"&a[num].point);
 
    puts("");
 
}
 
void search(infor a[], int num)
{
    int tmp_point;
    int tmp_school_num;
 
    int judge_num;
    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)
            {
                tmp_school_num = a[j].school_num;
                a[j].school_num = a[j + 1].school_num;
                a[j + 1].school_num = tmp_school_num;
 
                tmp_point = a[j].point;
                a[j].point = a[j + 1].point;
                a[j + 1].point = tmp_point;
            }
        }
    }
 
    do // 값 검색
    {
        printf("학번을 입력하시오 : ");
        scanf("%d"&judge_num);
 
        if (judge_num < 20100000 || judge_num>20209999)
        {
            puts("잘못된 수가 입력되었습니다.");
        }
    } while (judge_num < 20100000 || judge_num>20209999);
 
 
 
    for (i = 0; i < num; i++// 출력
    {
        if (a[i].school_num == judge_num)
        {
            printf("%d등 - 학번 : %d / 점수 : %d \n", a[i].grade, a[i].school_num, a[i].point);
            key++;
        }
    }
 
    if (key == 0)
        puts("찾지못하였습니다.");
}
 
 
 
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
void all_print(infor a[], int num) // 전체보기
{
    int tmp_point;
    int tmp_school_num;
 
    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].school_num, a[j + 1].school_num);
 
                tmp_school_num = a[j].school_num;
                a[j].school_num = a[j + 1].school_num;
                a[j + 1].school_num = tmp_school_num;
                /*for (int x = 0; x < num; x++)
                {
                printf("%d ", a[x].student.school_num);
                }
                puts("");*/
 
                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++// 전체보기
    {
        printf("%d등 - 학번 : %d / 점수 : %d \n", a[j].grade, a[j].school_num, a[j].point);
    }
 
    puts("");
}
 
void del(infor a[], int num) // 삭제하기
{
    int tmp_point;
    int tmp_school_num;
 
    int judge_num;
 
    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].school_num, a[j + 1].school_num);
 
                tmp_school_num = a[j].school_num;
                a[j].school_num = a[j + 1].school_num;
                a[j + 1].school_num = tmp_school_num;
 
                tmp_point = a[j].point;
                a[j].point = a[j + 1].point;
                a[j + 1].point = tmp_point;
            }
        }
    }
 
    do // 삭제할 값 찾기
    {
        printf("학번을 입력하시오 : ");
        scanf("%d"&judge_num);
 
        if(judge_num < 20100000 || judge_num>20209999)
            puts("잘못된 수가 입력되었습니다.");
 
    } while (judge_num < 20100000 || judge_num>20209999);
 
 
    
    for (i = 0; i < num; i++// 값 삭제
    {
        if (a[i].school_num == judge_num)
        {
            del_code = i;
 
            printf("학번 : %d 의 데이터를 삭제했습니다.\n", a[i].school_num);
            key++;
        }
    }
 
    if (key == 0)
        puts("찾지못하였습니다.");
    
    for (int j = del_code; j < num; j++// 삭제한 빈칸 채우기
    {
        a[j].school_num = a[j + 1].school_num;
        a[j].point = a[j + 1].point;
    }
 
    a[num].grade = NULL;
    a[num].school_num = NULL;
    a[num].point = NULL;
 
    puts("");
}
 
void view(infor a[], int num) // 새창보기
{
    system("cls");
}
cs

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

 

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

 

 

728x90