Blog/JAVA기반 스마트웹 개발2021
프로그래밍 언어 활용 part 1 - 애플리케이션 구현
고구마달랭이
2021. 8. 1. 18:17
애플리케이션 구현
[학습내용] | [학습목표] |
▪ 프로그램 설계 ▪ 코드 분석 |
▪ 프로그래밍 개발 시 적절한 자료구조를 선택할 수 있다. ▪ 구조체를 사용하여 함수의 매개변수 전달을 구현할 수 있다. |
프로그램 설계
1. 프로그램 기능 정의
프로그램 명 | ▪ 성적 처리 프로그램 |
주요 기능 | ▪ 학생들의 성적을 입력받고 평균을 구하여 성적순으로 출력하는 프로그램 |
자료 구조 | ▪ 이름, 국어, 영어, 수학, 평균 |
요구 사항 | ▪ 학생 이름은 10글자 이내 ▪ 최대 20명의 학생 성적 처리 ▪ 평균값 자동계산 ▪메뉴 방식 UI |
2. 자료구조 정의
자료 구조 | ▪ 이름, 국어, 영어, 수학, 평균 |
struct sungjuk {
char name[10];
int kor, eng, mat;
double avg;
}
3. 메뉴 구조
코드 분석
1. 헤더 파일
#include <stdio.h>
#define ST_NUM 20
#define NAME_LENGTH 10
typedefstructsungjuk{
char name[NAME_LENGTH];
intkor, eng, mat;
double avg;
}SUNGJUK;
int inScore(SUNGJUK* , int);
void outScore(SUNGJUK* , int);
void sortScore(SUNGJUK* , int);
2. main함수
int main(){
int sel, totalSCnt=0;
SUNGJUK student[ST_NUM];
while(1){
puts("\n원하는 메뉴를 선택하세요");
puts("1. 성적입력");
puts("2. 성적순출력");
puts("0. 종료 ");
scanf("%d", &sel);
fflush(stdin);
switch(sel){
case 1 : totalSCnt= inScore(student, totalSCnt); break;
case 2 : outScore(student, totalSCnt); break;
case 0 : return 0;
default : puts(“0~3 사이의메뉴번호를선택하세요”);
}
}
}
3. 입출력함수
#include "scoreHeader.h"
int inScore(SUNGJUK* st, int cnt){
char op;
do{
fflush(stdin);
printf("\n이름 : ");
gets(st[cnt].name);
printf("국어 : ");
scanf("%d",&st[cnt].kor);
printf("영어 : ");
scanf("%d",&st[cnt].eng);
printf("수학 : ");
scanf("%d",&st[cnt].mat);
puts("계속 입력하시겠습니까?(y/n)");
fflush(stdin);
op = getc(stdin);
st[cnt].avg =
(double)(st[cnt].kor+st[cnt].eng+st[cnt].mat)/3.0;
cnt++;
}while(op!='n');
return cnt;
}
void outScore(SUNGJUK* st, int cnt){
int i;
sortScore(st,cnt);
printf("\n----------------------------------------------------");
printf("\n 순위 이름 국어 영어 수학 평균 ");
printf("\n----------------------------------------------------\n");
for(i=0;i<cnt;i++){
printf("% 2d %10s %5d %5d %5d %3.1f\n", i+1, st[i].name, st[i].kor, st[i].eng, st[i].mat, st[i].avg);
}
}
void sortScore(SUNGJUK* st, int cnt){
SUNGJUK temp;
int i,j;
for(i=0; i<cnt-1 ;i++)
{
for(j=0; j<(cnt-i)-1; j++)
{
if(st[j].avg<st[j+1].avg)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
}
학습정리
1. 프로그램 설계
▪ 프로그램 개발 시 요구사항 분석 후 적절한 자료구조를 선택하는 것이 중요함
▪ 다양한 데이터 타입을 묶어서 처리할 필요가 있는 경우 적절한 자료구조는 구조체임
2. 코드 분석
▪ 매크로 상수는 프로그램의 가독성을 높이고 유지보수를 편리하게 하는 장점이 있음
▪ 사용자 정의 헤더 파일은 “ ”를 이용하여 기술함