# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define LEN 5
typedef struct BinToDec {
int decimal; // 10진수
char binary[5];
}BinToDec, *ptrBinToDec;
// 함수 원형부 ================================================
ptrBinToDec createNode(); // [1]
void init(BinToDec** param); // [2]
void BinaryInput(BinToDec** param); // [3]
int TwoData(int count); // [4]
//============================================================
int main(void) {
ptrBinToDec BTD = createNode();
init(&BTD);
BinaryInput(&BTD);
int tmpLength = strlen(BTD->binary)-1;
// 2진수를 10진수로 변환*******************************
for (int i = 0; i < strlen(BTD->binary); i++) {
BTD->decimal += (TwoData(tmpLength) * ((int)BTD->binary[i] - 48));
tmpLength--;
}
printf("binary =>%s --> decimal =>%d\n", BTD->binary, BTD->decimal);
free(BTD); // 메모리 해제
return 0;
} // end of main function
//============================================================
// 함수 구현부
ptrBinToDec createNode() { // [1]
ptrBinToDec newNode = (ptrBinToDec)malloc(sizeof(BinToDec));
return newNode;
} // end of createNode function
//============================================================
void init(BinToDec** param) { // [2]
if ((*param) == NULL) {
exit(1);
}
else { // 초기화
// (*param) != NULL
((*param)->binary, '\0', sizeof(char)*LEN); // 2진수
(*param)->decimal = 0; // 10진수
}
} // end of init function
//============================================================
void BinaryInput(BinToDec** param) { // [3]
puts("2진수 입력 [4자리로 한정한다]: ");
fgets((*param)->binary, LEN, stdin);
} // end of BinaryInput function
//============================================================
int TwoData(int count) { // [4]
int twoData = 1;
int tempTwo = 2;
for (int i = 0; i < count; i++) {
twoData *= tempTwo;
}
return twoData;
} // end of TwoData function
//============================================================
'언어 > c언어' 카테고리의 다른 글
더블릿 koi_watch 문제 (0) | 2017.10.05 |
---|---|
원형 링크드 (0) | 2017.10.05 |
c언어 문제 lavida (0) | 2017.10.02 |
c언어 URL (0) | 2017.10.02 |
네이버 지식인 내가 답변준 문제 (0) | 2017.09.30 |