#include< stdio.h> #include< stdlib.h입니다> #include< 수학.h>
unsigned long int puiss(unsigned long int x, int n) { if (n == 0) < / span> 반환 1; if(n == 1) return x; 부호 없는 긴 int x2 = powers(x,n/2); if(n%2 == 0) x2*x2 반환; 반환 x2 * x2 * x; } int char_to_int(char d) { 숯 str [2]; str [0] = d; str [1] = '\0'; 반환 (int) strtol (str, NULL, 10); }
unsigned int convertirEnBase10 (int 바이너리) < / span> { 숯 스넘[20]; // 문자열로 변환 이토아 (바이너리, snum, 10); // 문자 수? int nc = log10((int)이진)+1; int 십진수 = 0; for(int i = nc-1 ; i >= 0; i--) { 십진수 += char_to_int(snum[i]) * puiss( 2, (nc-1)-i); } 십진수를 반환합니다. } int 메인() { 바이너리 int; while(true) { printf("이진수 입력 "); scanf("%d",& 이진); printf ("밑수 2의 %ld = 밑수 10의 %ld\n",binary, convertInBase10 (바이너리); } 0을 반환합니다. }
|