발전하는 성적표 관리 시스템

2015. 3. 24. 01:35엘키스공간/엘키스코딩공방

728x90
728x90

1. 과제 내용

성적표을 입력받고 이를 출력하는 프로그램을 구현한다.

수업 중 배운 개념을 토대로 프로그램을 업그레이드 한다.


3.23 C++ 입문, 입출력 부분 C++로 구현하기.

3.19 동적할당, 함수로 뺐을 때 더블 포인터로 동적할당 구현하기.

3.17 구조체버전으로 학생 정보를 구현하기.

3.9 if문과 case문으로 구현하기.


2. 구현 결과

최종결과 영상찍기.


3. 날짜별 구현


- 3.23 현재 구현된 C 소스에서 입출력부분만 cin, cout, endl로 구현

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
/*
작성일 2015.03.23
작성자 조민혁
프로그램 설명
성적표 프로그램을 작성한다.
iostream을 이용하여 cout를 cout으로 변경.
메뉴
1.성적입력
2.성적출력
3.공간추가
4.학생삭제
5.학생검색
0.종료
*/
 
#include <iostream>
#include <stdlib.h>
#include <string.h>
 
#define TRUE 1
using namespace std;
 
// 점수 기록 구조체
typedef struct score
{
    float kor;
    float eng;
    float math;
    float sum;
    float average;
    char grade;
}Score;
 
// 학생 성적 구조체
typedef struct StudentGrade
{
    int id;
    char name[10];
    Score score;
}StudentGrade;
 
// 메뉴 출력 함수.
void OutputMenu( void );
// 등급 출력 함수.
char getGrade(float);
// 1. 성적입력
void InputStudentInfo(int* nowNumOfStu, int* curMemory, StudentGrade* stu);
// 2. 성적출력
void PrintStudentGrade(const int nowNumOfStu, const StudentGrade* stu);
// 3. 공간추가
void ExtedMemorySpace(int* curMemory, const int maxMemory, StudentGrade** stu);
// 4. 학생삭제
void DeleteStudentById(int* nowNumOfStu, int* curMemory, StudentGrade** stu);
// 5. 학생검색
void SearchStudentById(const int nowNumOfStu, const StudentGrade* stu);
 
void main()
{
    int        i = 0;                                                // 제어문 변수
    char        end = 0;                                            // cmd 종료시 사용
    int        selMenu = 0;                                    // 메뉴선택
    int        nowNumOfStu = 0;                        // 현재 학생의 수
    int        maxMemory = 10;                        // 최대 메모리
    int        curMemory = 0;                            // 현재 메모리
    
    StudentGrade *stu = 0;                            // 학생 구조체 포인터
 
    while(TRUE)
    {
        OutputMenu();
        cout<<"현재 메모리 상태" << nowNumOfStu << "/"<<
            curMemory<<"/"<< maxMemory << "현재 인원/잡힌 메모리/최대 메모리\n";
        cin>>selMenu;
 
        // 스위치문 시작
        switch (selMenu)
        {
            // 학생 정보 입력
        case 1:
            {
                InputStudentInfo(&nowNumOfStu, &curMemory, stu);
            }
            break;
            // 학생 출력
        case 2:
            {
                PrintStudentGrade(nowNumOfStu, stu);    
            }
            break;
            //공간 늘리기
        case 3:
            {
                ExtedMemorySpace(&curMemory, maxMemory, &stu);
            }
            break;
            // 삭제
        case 4:
            {
                DeleteStudentById(&nowNumOfStu, &curMemory, &stu);
            }
            break;
            // 검색
        case 5:
            {
                SearchStudentById(nowNumOfStu, stu);
            }
            break;
            // 해제 후 프로그램 리턴
        case 0:
            if (stu != NULL)
            {
                free(stu);
            }
            return;
        default:
            break;
        }
    }    
}
 
void OutputMenu( void )
{
    // 출력
    cout<<"1.성적입력"<<endl;
    cout<<"2.성적출력"<<endl;
    cout<<"3.공간추가"<<endl;
    cout<<"4.학생삭제"<<endl;
    cout<<"5.학생검색"<<endl;
    cout<<"0.종료"<<endl;
    cout<<"입력 : ";
}
 
char getGrade(float average)
{
    // 간단한 if else로 구현
    if ((average >= 90) && (average <= 100)) 
    {
        return 'A';
    }
    else if ((average >= 80) && (average < 90)) 
    {
        return 'B';        
    }
    else 
    {
        return 'F';
    }
}
 
void InputStudentInfo(int *nowNumOfStu, int *curMemory, StudentGrade* stu)
{
    int idFlag = 0;        // 이름 유효성 검사. 같은게 존재하면 1. 존재하지 않으면 0.
    int i = 0;                    //    반복문 변수
    if(*nowNumOfStu == *curMemory)
    {
        cout<<"공간이 부족합니다!"<<endl;
    }
    else
    {
        // id 유효성 검사.
        while(TRUE)
        {
            cout<<"학생의 학번을 입력하세요. : ";
            cin>>stu[*nowNumOfStu].id;
            // 현재 인원수에 맞춰서 검사
            for (i = 0; i < *nowNumOfStu; i++)
            {
                // i번째 학번와 입력한 학번이 같다면?
                if (stu[i].id == stu[*nowNumOfStu].id)
                {
                    idFlag = 1
                }
            }
            // flag가 1 중복체크
            if (idFlag == 1)
            {
                cout<<"중복입니다!!! 다시 입력하세요!"<<endl;
                idFlag = 0// flag 초기화
            }
            else
            {
                idFlag = 0// flag 초기화
                break;
            }
        }
 
        // 나머지 입력
        cout<<("학생의 이름을 입력하세요. : ");
        cin>>stu[*nowNumOfStu].name;
        cout<<("학생의 국어 점수를 입력하세요.: ");
        cin>>stu[*nowNumOfStu].score.kor;
        cout<<("학생의 영어 점수를 입력하세요.: ");
        cin>>stu[*nowNumOfStu].score.eng;
        cout<<("학생의 수학 점수를 입력하세요.: ");
        cin>>stu[*nowNumOfStu].score.math;
 
        // 합계, 평균, 등급 계산
        stu[*nowNumOfStu].score.sum = stu[*nowNumOfStu].score.kor 
            + stu[*nowNumOfStu].score.eng + stu[*nowNumOfStu].score.math;
        stu[*nowNumOfStu].score.average = stu[*nowNumOfStu].score.sum / 3;
        stu[*nowNumOfStu].score.grade = getGrade(stu[*nowNumOfStu].score.average); 
        (*nowNumOfStu)++;
    }
}
 
void PrintStudentGrade(const int nowNumOfStu, const StudentGrade *stu)
{
    int i = 0;
 
    // 출력문
    for (i = 0; i < nowNumOfStu; i++)
    {
        cout<<" 학번 : "<< stu[i].id;
            cout<<" 이름 : "<< stu[i].name;
            cout<<" 점수합계 : "<<stu[i].score.sum;
            cout<<" 점수평균 : " << stu[i].score.average;
            cout<<" 점수등급 : "<< stu[i].score.grade;
            cout<<"\n";
    }
}
 
void ExtedMemorySpace(int* curMemory, const int maxMemory, StudentGrade** stu)
{
    int inputNumOfMem = 0// 메모리 입력 변수
 
    cout<<("몇 명 증가시키겠습니까? : ");
    cin>>inputNumOfMem;
    
    // 입력 수 + 현재 공간이 최대 공간보다 커버리면 안됨. i + c > m
    if (inputNumOfMem + *curMemory > maxMemory)
    {
        cout<<("이 공간은 할당할 수 없어!")<<endl;
    }
    // 처음 할당 시 malloc
    else if (*curMemory == 0)
    {
        *stu = (StudentGrade*)malloc(sizeof(StudentGrade)*inputNumOfMem);
        *curMemory += inputNumOfMem;
    }
    // 재할당 realloc 
    else
    {
        *stu = (StudentGrade*)realloc(*stu,sizeof(StudentGrade)*(*curMemory + inputNumOfMem));
        *curMemory += inputNumOfMem;
    }
}
 
void DeleteStudentById(int* nowNumOfStu, int* curMemory, StudentGrade** stu)
{
    int deleteId = 0;        // 삭제할 아이디 받을 변수                        
    int delFlag = 0;        // 삭제 되었는지 확인
    int i = 0;
    cout<<("삭제할 대상의 학번을 입력해주세요. : ");
                cin>>deleteId;
                // 대상 학생 찾기
                for (i = 0; i < *nowNumOfStu; i++)
                {
                    // 현재 검색 학생과 입력받은 학번이 같다면?
                    if ((*stu)[i].id == deleteId)
                    {
                        delFlag = 1// 삭제 플레그 가동
                    }
                    
                    // flag가 1일때 한개씩 앞으로 복사 + 마지막항 처리
                    if (delFlag == 1 && (i != *nowNumOfStu))
                    {
                        memcpy_s(*(stu)+i , sizeof(StudentGrade),*(stu) + i + 1sizeof(StudentGrade));
                    }
                }
 
                // 삭제 실패 시나리오
                if (delFlag == 0)
                {
                    cout<<("학번을 확인해주세요. 지우려는 녀석이 없습니다!")<<endl;
                }
                else
                {
                    // 삭제 후 메모리 재할당
                    *stu = (StudentGrade*)realloc(*stu,sizeof(StudentGrade)*(*curMemory - 1));
                    (*nowNumOfStu)--;
                    (*curMemory)--;
                }
 
                delFlag = 0;
}
 
void SearchStudentById(const int nowNumOfStu, const StudentGrade* stu)
{
    int searchId = 0;        // 학번 입력용 변수
    int searchFlag = 0;    // 검색 확인용 변수
    int i = 0;                        // 반복문 변수
 
    cout<<("찾을 학생 학번을 입력해주세요. : ");
    cin>>searchId;
    // 학생 검색
    for (i = 0; i < nowNumOfStu; i++)
    {
        // 현재 학생의 학번과 검색 학번이 같다면?
        if (stu[i].id == searchId )
        {
            // 해당 학생 출력
            cout<<"찾으시는 학생 정보"<<endl;
            cout<<" 학번 : "<< stu[i].id;
            cout<<" 이름 : "<< stu[i].name;
            cout<<" 점수합계 : "<<stu[i].score.sum;
            cout<<" 점수평균 : " << stu[i].score.average;
            cout<<" 점수등급 : "<< stu[i].score.grade;
            cout<<"\n";
            searchFlag = 1
        }
    }
 
    // 검색 실패 예외처리
    if (searchFlag == 0)
    {
        cout<<("찾으려는 학생정보가 없어요! 학번을 확인해주세요!")<<endl;
    }
    else
    {
        searchFlag = 0;
    }
}
cs


- 3.19 동적할당 학생 정보 동적으로 구현

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
/*
작성일 2015.03.19
작성자 조민혁
프로그램 설명
성적표 프로그램을 작성한다.
구조체와 동적할당을 이용하여 동적으로 학생 정보를 관리하는 프로그램.
함수로 뺀 버전.
메뉴
1.성적입력
2.성적출력
3.공간추가
4.학생삭제
5.학생검색
0.종료
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define TRUE 1
 
// 점수 기록 구조체
typedef struct score
{
    float kor;
    float eng;
    float math;
    float sum;
    float average;
    char grade;
}Score;
 
// 학생 성적 구조체
typedef struct StudentGrade
{
    int id;
    char name[10];
    Score score;
}StudentGrade;
 
// 메뉴 출력 함수.
void OutputMenu( void );
// 등급 출력 함수.
char getGrade(float);
// 1. 성적입력
void InputStudentInfo(int* nowNumOfStu, int* curMemory, StudentGrade* stu);
// 2. 성적출력
void PrintStudentGrade(const int nowNumOfStu, const StudentGrade* stu);
// 3. 공간추가
void ExtedMemorySpace(int* curMemory, const int maxMemory, StudentGrade** stu);
// 4. 학생삭제
StudentGrade** DeleteStudentById(int* nowNumOfStu, int* curMemory, StudentGrade** stu);
// 5. 학생검색
void SearchStudentById(const int nowNumOfStu, const StudentGrade* stu);
 
void main()
{
    int        i = 0;                                                // 제어문 변수
    char        end = 0;                                            // cmd 종료시 사용
    int        selMenu = 0;                                    // 메뉴선택
    int        nowNumOfStu = 0;                        // 현재 학생의 수
    int        maxMemory = 10;                        // 최대 메모리
    int        curMemory = 0;                            // 현재 메모리
    
    StudentGrade *stu = 0;                            // 학생 구조체 포인터
 
    while(TRUE)
    {
        OutputMenu();
        printf("현재 메모리 상태 %d/%d/%d 현재 인원/잡힌 메모리/최대 메모리\n"
            , nowNumOfStu, curMemory, maxMemory);
        scanf_s("%d",&selMenu);
 
        // 스위치문 시작
        switch (selMenu)
        {
            // 학생 정보 입력
        case 1:
            {
                InputStudentInfo(&nowNumOfStu, &curMemory, stu);
            }
            break;
            // 학생 출력
        case 2:
            {
                PrintStudentGrade(nowNumOfStu, stu);    
            }
            break;
            //공간 늘리기
        case 3:
            {
                ExtedMemorySpace(&curMemory, maxMemory, &stu);
            }
            break;
            // 삭제
        case 4:
            {
                DeleteStudentById(&nowNumOfStu, &curMemory, &stu);
            }
            break;
            // 검색
        case 5:
            {
                SearchStudentById(nowNumOfStu, stu);
            }
            break;
            // 해제 후 프로그램 리턴
        case 0:
            if (stu != NULL)
            {
                free(stu);
            }
            return;
        default:
            puts("번호를 잘 보세요 ~!! 0~5)");
            break;
        }
    }    
}
 
void OutputMenu( void )
{
    // 출력
    puts("1.성적입력");
    puts("2.성적출력");
    puts("3.공간추가");
    puts("4.학생삭제");
    puts("5.학생검색");
    puts("0.종료");
    printf_s("입력 : ");
}
 
char getGrade(float average)
{
    // 간단한 if else로 구현
    if ((average >= 90) && (average <= 100)) 
    {
        return 'A';
    }
    else if ((average >= 80) && (average < 90)) 
    {
        return 'B';        
    }
    else 
    {
        return 'F';
    }
}
 
void InputStudentInfo(int *nowNumOfStu, int *curMemory, StudentGrade* stu)
{
    int idFlag = 0;        // 이름 유효성 검사. 같은게 존재하면 1. 존재하지 않으면 0.
    int i = 0;                    //    반복문 변수
    if(*nowNumOfStu == *curMemory)
    {
        puts("공간이 부족합니다!");
    }
    else
    {
        // id 유효성 검사.
        while(TRUE)
        {
            printf("학생의 학번을 입력하세요. : ");
            scanf_s("%d", &stu[*nowNumOfStu].id);
            // 현재 인원수에 맞춰서 검사
            for (i = 0; i < *nowNumOfStu; i++)
            {
                // i번째 학번와 입력한 학번이 같다면?
                if (stu[i].id == stu[*nowNumOfStu].id)
                {
                    idFlag = 1
                }
            }
            // flag가 1 중복체크
            if (idFlag == 1)
            {
                puts("중복입니다!!! 다시 입력하세요!");
                idFlag = 0// flag 초기화
            }
            else
            {
                idFlag = 0// flag 초기화
                break;
            }
        }
 
        // 나머지 입력
        printf("학생의 이름을 입력하세요. : ");
        scanf_s("%s", stu[*nowNumOfStu].name, 10);
        printf("학생의 국어 점수를 입력하세요.: ");
        scanf_s("%f", &stu[*nowNumOfStu].score.kor);
        printf("학생의 영어 점수를 입력하세요.: ");
        scanf_s("%f", &stu[*nowNumOfStu].score.eng);
        printf("학생의 수학 점수를 입력하세요.: ");
        scanf_s("%f", &stu[*nowNumOfStu].score.math);
 
        // 합계, 평균, 등급 계산
        stu[*nowNumOfStu].score.sum = stu[*nowNumOfStu].score.kor 
            + stu[*nowNumOfStu].score.eng + stu[*nowNumOfStu].score.math;
        stu[*nowNumOfStu].score.average = stu[*nowNumOfStu].score.sum / 3;
        stu[*nowNumOfStu].score.grade = getGrade(stu[*nowNumOfStu].score.average); 
        (*nowNumOfStu)++;
    }
}
 
void PrintStudentGrade(const int nowNumOfStu, const StudentGrade *stu)
{
    int i = 0;
 
    // 출력문
    for (i = 0; i < nowNumOfStu; i++)
    {
        printf("학번 : %-5d", stu[i].id);
        printf("이름 : %-10s", stu[i].name);
        printf("점수합계 : %-8.2f", stu[i].score.sum);
        printf("점수평균 : %-6.2f", stu[i].score.average);
        printf("점수등급 : %-3c", stu[i].score.grade);
        printf("\n");
    }
}
 
void ExtedMemorySpace(int* curMemory, const int maxMemory, StudentGrade** stu)
{
    int inputNumOfMem = 0// 메모리 입력 변수
 
    printf("몇 명 증가시키겠습니까? : ");
    scanf_s("%d", &inputNumOfMem);
    
    // 입력 수 + 현재 공간이 최대 공간보다 커버리면 안됨. i + c > m
    if (inputNumOfMem + *curMemory > maxMemory)
    {
        puts("이 공간은 할당할 수 없어!");
    }
    // 처음 할당 시 malloc
    else if (*curMemory == 0)
    {
        *stu = (StudentGrade*)malloc(sizeof(StudentGrade)*inputNumOfMem);
        *curMemory += inputNumOfMem;
    }
    // 재할당 realloc 
    else
    {
        *stu = (StudentGrade*)realloc(*stu,sizeof(StudentGrade)*(*curMemory + inputNumOfMem));
        *curMemory += inputNumOfMem;
    }
}
 
StudentGrade** DeleteStudentById(int* nowNumOfStu, int* curMemory, StudentGrade** stu)
{
    int deleteId = 0;        // 삭제할 아이디 받을 변수                        
    int delFlag = 0;        // 삭제 되었는지 확인
    int i = 0;
    printf("삭제할 대상의 학번을 입력해주세요. : ");
                scanf_s("%d", &deleteId);
                // 대상 학생 찾기
                for (i = 0; i < *nowNumOfStu; i++)
                {
                    // 현재 검색 학생과 입력받은 학번이 같다면?
                    if ((*stu)[i].id == deleteId)
                    {
                        delFlag = 1// 삭제 플레그 가동
                    }
                    
                    // flag가 1일때 한개씩 앞으로 복사 + 마지막항 처리
                    if (delFlag == 1 && (i != *nowNumOfStu))
                    {
                        memcpy_s(*(stu)+i , sizeof(StudentGrade),*(stu) + i + 1sizeof(StudentGrade));
                    }
                }
 
                // 삭제 실패 시나리오
                if (delFlag == 0)
                {
                    puts("학번을 확인해주세요. 지우려는 녀석이 없습니다!");
                }
                else
                {
                    // 삭제 후 메모리 재할당
                    *stu = (StudentGrade*)realloc(*stu,sizeof(StudentGrade)*(*curMemory - 1));
                    (*nowNumOfStu)--;
                    (*curMemory)--;
                }
 
                delFlag = 0;
                return stu;
}
 
void SearchStudentById(const int nowNumOfStu, const StudentGrade* stu)
{
    int searchId = 0;        // 학번 입력용 변수
    int searchFlag = 0;    // 검색 확인용 변수
    int i = 0;                        // 반복문 변수
 
    printf("찾을 학생 학번을 입력해주세요. : ");
    scanf_s("%d",&searchId);
    // 학생 검색
    for (i = 0; i < nowNumOfStu; i++)
    {
        // 현재 학생의 학번과 검색 학번이 같다면?
        if (stu[i].id == searchId )
        {
            // 해당 학생 출력
            puts("찾으시는 학생 정보");
            printf("학번 : %-5d", stu[i].id);
            printf("이름 : %-10s", stu[i].name);
            printf("점수합계 : %-8.2f", stu[i].score.sum);
            printf("점수평균 : %-6.2f", stu[i].score.average);
            printf("점수등급 : %-3c", stu[i].score.grade);
            printf("\n");
            searchFlag = 1
        }
    }
 
    // 검색 실패 예외처리
    if (searchFlag == 0)
    {
        puts("찾으려는 학생정보가 없어요! 학번을 확인해주세요!");
    }
    else
    {
        searchFlag = 0;
    }
}
cs


- 3.17 구조체로 학생 정보 구현

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 <stdio.h>
/*
작성일 2015.03.17
작성자 조민혁
score : 3명의 성적(국어/영어/수학)을 입력받아
합,평균,등급을 출력해라
90 이상~100 이하 : A
80 이상 ~ 90 미만 : B
60 미만 : F
나머지
*/
 
char getGradeUseIf(float); // if문으로 등급 구하는 함수
char getGradeUseSwitch(float); // switch case문으로 등급 구하는 함수
 
typedef struct StudentGrade // 학생 성적 구조체
{
    char grade;
    float score1;
    float score2;
    float score3;
    float sum;
}StudentGrade;
 
void main()
{
    int        i = 0;                                                // 제어문 변수
    char        end = 0;                                            // cmd 종료시 사용
    char*    firstString[3= {"첫","두","세"};    // 문자 출력에 사용되는 변수
     
    StudentGrade stu[3];
    
    //연산부
    printf("제출일 : 2015.03.17 작성자 : 조민혁\n");
    for (i  = 0; i  < 3; i ++)
    {
        printf("%s 번째 학생의 국어 점수를 입력하세요.: ", firstString[i]);
        scanf_s("%f", &stu[i].score1);
        printf("%s 번째 학생의 영어 점수를 입력하세요.: ", firstString[i]);
        scanf_s("%f", &stu[i].score2);
        printf("%s 번째 학생의 수학 점수를 입력하세요.: ", firstString[i]);
        scanf_s("%f", &stu[i].score3);
 
        stu[i].sum = stu[i].score1 + stu[i].score2 + stu[i].score3;
        stu[i].grade = getGradeUseSwitch(stu[i].sum / 3); 
    }
    
    //출력부
    for (i = 0; i < 3; i++)
    {
        printf("점수합계 : %-8.2f", stu[i].sum);
        printf("점수평균 : %-6.2f", stu[i].sum / 3);
        printf("점수등급 : %-3c", stu[i].grade);
        printf("\n");
    }
    getchar();
    printf("종료하시려면 아무버튼이나 눌러주세요.");
    getchar();
}
 
char getGradeUseIf(float average) // 평균을 넘겨서 등급을 리턴한다.
{
    if ((average >= 90) && (average <= 100)) 
    {
        return 'A';
    }
    else if ((average >= 80) && (average < 90)) 
    {
        return 'B';        
    }
    else 
    {
        return 'F';
    }
}
 
char getGradeUseSwitch(float average)
{
    int caseNum = (int)average / 10// 명시적 형변환
 
    switch (caseNum)
    {
    case 9:
        {
            return 'A';
        }
        break;
    case 8:
        {
            return 'B';
        }
        break;
    default:
        {
            return 'F';
        }
        break;
    }
}
 
cs


- 3.9 if, case

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
#include <stdio.h>
/*
작성일 2015.03.09
작성자 조민혁
score : 3명의 성적(국어/영어/수학)을 입력받아
합,평균,등급을 출력해라
90 이상~100 이하 : A
80 이상 ~ 90 미만 : B
60 미만 : F
나머지
*/
 
char getGradeUseIf(float); // if문으로 등급 구하는 함수
char getGradeUseSwitch(float); // switch case문으로 등급 구하는 함수
 
 
void main()
{
    char        grade[3= { 0 };                            // 등급
    float    score[3= { 0 };                                // 점수 받을 배열
    float    sum[3= { 0 };                                // 합계
    int        i = 0;                                                // 제어문 변수
    char        end = 0;                                            // cmd 종료시 사용
    char*    firstString[3= {"첫","두","세"};    // 문자 출력에 사용되는 변수
    
    //연산부
    printf("제출일 : 2015.03.09 작성자 : 조민혁\n");
    for (i  = 0; i  < 3; i ++)
    {
        printf("%s 번째 학생의 국어 점수를 입력하세요.: ", firstString[i]);
        scanf_s("%f", &score[0]);
        printf("%s 번째 학생의 영어 점수를 입력하세요.: ", firstString[i]);
        scanf_s("%f", &score[1]);
        printf("%s 번째 학생의 수학 점수를 입력하세요.: ", firstString[i]);
        scanf_s("%f", &score[2]);
 
        sum[i] = score[0+ score[1+ score[2];
        grade[i] = getGradeUseSwitch(sum[i] / 3); 
    }
    
    //출력부
    for (i = 0; i < 3; i++)
    {
        printf("점수합계 : %-8.2f", sum[i]);
        printf("점수평균 : %-6.2f", sum[i] / 3);
        printf("점수등급 : %-3c", grade[i]);
        printf("\n");
    }
    getchar();
    printf("종료하시려면 아무버튼이나 눌러주세요.");
    getchar();
}
 
char getGradeUseIf(float average) // 평균을 넘겨서 등급을 리턴한다.
{
    if ((average >= 90) && (average <= 100)) 
    {
        return 'A';
    }
    else if ((average >= 80) && (average < 90)) 
    {
        return 'B';        
    }
    else 
    {
        return 'F';
    }
}
 
char getGradeUseSwitch(float average)
{
    int caseNum = (int)average / 10// 명시적 형변환
 
    switch (caseNum)
    {
    case 9:
        {
            return 'A';
        }
        break;
    case 8:
        {
            return 'B';
        }
        break;
    default:
        {
            return 'F';
        }
        break;
    }
}
cs


728x90
반응형

'엘키스공간 > 엘키스코딩공방' 카테고리의 다른 글

큐로 메시지 큐 구현하기  (0) 2015.06.10
큐 구현하기  (0) 2015.06.10
[코딩] 스택 구현하기  (1) 2015.06.10
가위바위보 게임 만들기  (0) 2015.03.24
베스킨라빈스 31 게임  (0) 2015.03.24
반복문  (0) 2015.03.23
문자열 함수 구현해보기  (2) 2015.03.10