네이버 지식인 풀이
https://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=314316632
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
# define _CRT_SECURE_NO_WARNINGS
typedef struct {
unsigned int height;
unsigned int choice;
}STAR, *ptrSTAR;
// 함수 원형_________________________________
void _init_(STAR** param); // func (1)
void _choice_(STAR** param); // func (2)
void _one_(STAR** param); // func (3)
void _two_(STAR** param); // func (4)
void _three_(STAR** param); // func (5)
// _________________________________________
int main(void) {
ptrSTAR node = NULL;
node = (ptrSTAR)malloc(sizeof(STAR));
if (node == NULL) {
printf("malloc fail ... \n");
exit(1);
}
// node != NULL
_init_(&node);
_choice_(&node);
// 동적할당 해제
free(node);
return 0;
} // end of main function
// ================================
void _init_(STAR** param) { // func (1)
(** param).height = 0x0;
while (true) {
printf("높이 입력해줘: ");
scanf("%d", &(** param).height);
if ((** param).height % 2 == 0) {
printf("높이는 반드시 홀수여야 합니다.\n");
}
else { // 높이 값이 홀수 인 경우
break;
}
}
} // end of _init_ function
// ================================
void _choice_(STAR** param) { // func (2)
while (true) {
printf("1,2,3 중 한개 선택: ");
scanf("%d", &(** param).choice);
if (1 <= (** param).choice && (** param).choice <= 3) {
break;
}
else {
printf("잘못 입력하셨습니다. \n");
}
}
switch ((**param).choice) {
case 1:
_one_(param);
break;
case 2:
_two_(param);
break;
case 3:
_three_(param);
break;
}
} // end of _choice_ function
// ================================
void _one_(STAR** param) { // func (3)
/*
ex)
*****
***
*
*/
unsigned int i, j, k;
// 높이
for (i = (** param).height; i >= 1; i--) {
// 공백
for (j = i; j < (**param).height; j++) {
printf(" ");
} // end
// 별
for (k = 2*i-1; k >=1 ; k--) {
printf("*");
} // end for2
printf("\n"); // 개행
} //
} // end of _one_ function
// ================================
void _two_(STAR** param) { // func (4)
/*
ex)
*
***
*****
***
*
*/
unsigned int i, j, k;
// 상단
for (i = 1; i <= (**param).height; i++) {
// 공백
for (j = i; j < (**param).height; j++) {
printf(" ");
} // end
// 별
for (k = 1; k <= 2 * i - 1; k++) {
printf("*");
} // end for2
printf("\n"); // 개행
if (i == (**param).height) {
// 하단
for (i = (**param).height - 1; i >= 1; i--) {
// 공백
for (j = i; j <= (**param).height-1; j++) {
printf(" ");
} // endfor
// 별
for (k = 2 * i - 1; k >= 1; k--) {
printf("*");
} // endfor
printf("\n"); // 개행
} // endfor
i = (**param).height;
} // endif
} //
} // end of _two_ function
// ================================
void _three_(STAR** param) { // func (5)
/*
ex)
*****
***
*
***
*****
*/
unsigned int i, j, k;
// 상단
for (i = (**param).height; i >= 1; i--) {
// 공백
for (j = i; j < (**param).height; j++) {
printf(" ");
} // end
// 별
for (k = 2 * i - 1; k >= 1; k--) {
printf("*");
} // end for2
printf("\n"); // 개행
if (i == 1) {
// 하단
for (i = 1; i <= (**param).height - 1; i++) {
// 공백
for (j = i; j < (**param).height - 1; j++) {
printf(" ");
} // endfor
// 별
for (k = 1; k <= 2 * i + 1; k++) {
printf("*");
} // endfor
printf("\n"); // 개행
} // endfor
i = 1;
} // endif
} //
} // end of _three_ function
'언어 > c언어' 카테고리의 다른 글
버블 정렬 (0) | 2018.11.18 |
---|---|
c언어 네이버 지식인 풀이 (0) | 2018.11.17 |
네이버 지식인 답변 (0) | 2018.11.12 |
네이버 지식인 풀이 (0) | 2018.11.10 |
네이버 풀이 (0) | 2018.11.10 |