네이버 지식인 풀이, c언어 숫자 거꾸로 출력
https://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=313575352
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define LEN 40
# define ERROR 1
// ==========================
typedef struct Num {
char sNum[LEN]; // 문자열
int iNum; // 정수
}Num, *ptrNum;
// ============================
ptrNum newNode(); // func (1)
void Init(Num** p); // func (2)
void ValueInsert(Num** p); // func (3)
void ReversePrint(Num** p); // func (4)
// ============================
int main(void) {
ptrNum node = NULL;
node = newNode();
if (node == NULL) {
printf("malloc fail ...!!\n");
exit(ERROR);
}
else { // node != NULL
Init(&node);
ValueInsert(&node);
ReversePrint(&node);
}
free(node); // 메모리 해제
return 0;
}
ptrNum newNode() { // func (1)
ptrNum retNode = NULL;
retNode = (ptrNum)malloc(sizeof(Num));
return retNode;
} // end of newNode function
// ==============================
void Init(Num** p) { // func (2)
(*p)->iNum = 0;
strcpy((*p)->sNum, "\0");
} // end of Init function
// ==============================
void ValueInsert(Num** p) {
printf("정수 입력: ");
scanf("%d", &(*p)->iNum);
sprintf((*p)->sNum, "%d", (*p)->iNum);
//printf("%s\n", (*p)->sNum);
} // end of ValueInsert function
void ReversePrint(Num** p) {
int i; // index
int num = strlen((*p)->sNum)-1;
for (i = num; i >= 0; i--) {
printf("%c", (*p)->sNum[i]);
} printf("\n"); // 개행
} // end of ReversePrint function
'언어 > c언어' 카테고리의 다른 글
네이버 풀이 (0) | 2018.11.10 |
---|---|
네이버 지식인 풀이 (0) | 2018.11.07 |
네이버 지식인 풀이 (0) | 2018.10.14 |
네이버 지식인 풀이 링크드 리스트 (0) | 2018.10.13 |
네이버 지식인 문제 풀이 (0) | 2018.10.09 |