c언어 네이버 지식인 풀이
https://kin.naver.com/qna/detail.nhn?d1id=11&dirId=1113&docId=314491480
/*
작성자 : sleep4725
윈도우 10 환경
visual studio 2013
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define _CRT_SECURE_NO_WARNINGS
# define LEN 20
typedef struct {
char idNumber[LEN]; // 번호
char stuName[LEN]; // 학생이름
int kor; // 국어
int eng; // 영어
int math; // 수학
int tot; // 총점
double avg; // 평균
}STU, *pSTU;
// function prototype ============================
pSTU retNode(int); // func (1)
void stuInfoSetting(STU**, int*); // func (2)
void bubbleSort(STU**, int*); // func (3)
void sortPrintf(STU**, int*); // func (4)
// ===============================================
int main(void) { // main function
pSTU stuInfor = NULL;
unsigned int stuCount = 0x0;
printf("성적을 처리할 학생들은 몇 명 입니까? ");
scanf("%d", &stuCount);
stuInfor = retNode(stuCount);
if (stuInfor == NULL) {
printf("malloc fail ... \n");
exit(1);
}
stuInfoSetting(&stuInfor, &stuCount);
//bubbleSort(&stuInfor, &stuCount);
sortPrintf(&stuInfor, &stuCount);
return 0;
} // end of main function
// ===========================================
pSTU retNode(int cnt) { // func (1)
pSTU tmp = NULL;
tmp = (pSTU)malloc(sizeof(STU)*cnt);
return tmp;
} // end of retNode function
// ===========================================
void stuInfoSetting(STU** pNode, int* cnt) { // func (2)
int i; // index
for (i = 0; i < *cnt; i++) {
// 학생 이름 입력 : string
printf("학생 이름 입력: ");
rewind(stdin); // 버퍼 비우기
gets((*pNode + i)->stuName); // gets_s 함수는 비표준
// 학생 학번 입력 : string
printf("학생 학번 입력: ");
gets((*pNode + i)->idNumber);
// 국어 점수 입력 : int
printf("국어 점수 입력: ");
scanf("%d", &(*pNode + i)->kor);
// 영어 점수 입력 : int
printf("영어 점수 입력: ");
scanf("%d", &(*pNode + i)->eng);
// 수학 점수 입력 : int
printf("수학 점수 입력: ");
scanf("%d", &(*pNode + i)->math);
rewind(stdin); // 버퍼 비우기
// 총점
(*pNode + i)->tot = (*pNode + i)->kor + (*pNode + i)->eng + (*pNode + i)->math;
(*pNode + i)->avg = (double)(** pNode).tot / 3;
} // endfor
} // end of stuInfoSetting function
// ===========================================
void bubbleSort(STU** pNode, int* cnt) { // func (3)
int i, j; // index
STU swapNode;
for (i = 0; i < (*cnt)-1; i++) {
for (j = 0; j < (*cnt)-1; j++) {
// 총점을 기준으로 오름차순 정렬
if ((*pNode)[j].tot > (*pNode)[j + 1].tot) {
memcpy(&swapNode, &(*pNode)[j], sizeof(STU));
memcpy(&(*pNode)[j], &(*pNode)[j + 1], sizeof(STU));
memcpy(&(*pNode)[j + 1], &swapNode, sizeof(STU));
}
} // endfor
} // endfor
} // end of bubbleSort function
void sortPrintf(STU** pNode, int* cnt) { // func (4)
int i; // index
for (i = 0; i < (*cnt); i++) {
printf("학번 : %s\n", (*pNode)[i].idNumber);
printf("이름 : %s\n", (*pNode)[i].stuName);
printf("국어 : %d\n", (*pNode)[i].kor);
printf("영어 : %d\n", (*pNode)[i].eng);
printf("수학 : %d\n", (*pNode)[i].math);
printf("총점 : %d\n", (*pNode)[i].tot);
printf("평균 : %0.2lf\n", (*pNode)[i].avg);
printf("=================================\n");
}
} // end of sortPrintf function
'언어 > c언어' 카테고리의 다른 글
선택정렬 (0) | 2018.11.18 |
---|---|
버블 정렬 (0) | 2018.11.18 |
네이버 지식인 풀이 (0) | 2018.11.14 |
네이버 지식인 답변 (0) | 2018.11.12 |
네이버 지식인 풀이 (0) | 2018.11.10 |