fork download
  1. /*
  2.  * 2進数を10進数に変換するプログラム(負の数は扱えません)
  3.  *
  4.  * ideone上でSIZEの数値変えて試すのなら左上のforkをクリック
  5.  *
  6.  * Author: 俺
  7.  */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. /* 2進数の最大桁数(メモリの許す限りいくらでもどうぞ) */
  12. #define SIZE (1000)
  13.  
  14. void genBits /* 適当な2進数を生成 */ (int*, const int);
  15. void initDecimal /* 10進数の初期化 */ (int*, const int);
  16. void bits2decimal /* 2進数を10進数に変換 */ (const int*, const int, int*, const int);
  17. void printBits /* 2進数を表示 */ (const int*, const int);
  18. void printDecimal /* 10進数を表示 */ (const int*, const int);
  19. void addDecimal /* 10進数の加算 */ (int*, const int, const int);
  20. void mulDecimal /* 10進数の乗算 */ (int*, const int, const int);
  21.  
  22. int main(void) {
  23. int bits[SIZE];
  24. int decimal[SIZE];
  25.  
  26. genBits(bits, SIZE);
  27. initDecimal(decimal, SIZE);
  28.  
  29. bits2decimal(bits, SIZE, decimal, SIZE);
  30.  
  31. printBits(bits, SIZE);
  32. printDecimal(decimal, SIZE);
  33.  
  34. return 0;
  35. }
  36.  
  37. void genBits(int* bits, const int size) {
  38. int i;
  39. for (i = 0; i < size; i++) {
  40. *(bits++) = rand() % 2;
  41. }
  42. }
  43.  
  44. void initDecimal(int* decimal, const int size) {
  45. int i;
  46. for (i = 0; i < size; i++) {
  47. *(decimal++) = 0;
  48. }
  49. }
  50.  
  51. void printBits(const int* bits, const int size) {
  52. char* BIT = "01";
  53. int f = 0;
  54. int i;
  55. printf("BITS:\n");
  56. bits += size - 1;
  57. for (i = 0; i < size; i++) {
  58. if (f == 0) {
  59. if (*bits == 0) {
  60. bits--;
  61. continue;
  62. }
  63. f = 1;
  64. }
  65. putchar(BIT[*bits]);
  66. bits--;
  67. }
  68. if (f == 0) {
  69. putchar(BIT[0]);
  70. }
  71. putchar('\n');
  72. }
  73.  
  74. void printDecimal(const int* decimal, const int size) {
  75. char* NUMBER = "0123456789";
  76. int f = 0;
  77. int i;
  78. printf("DECIMAL:\n");
  79. decimal += size - 1;
  80. for (i = 0; i < size; i++) {
  81. if (f == 0) {
  82. if (*decimal == 0) {
  83. decimal--;
  84. continue;
  85. }
  86. f = 1;
  87. }
  88. putchar(NUMBER[*decimal]);
  89. decimal--;
  90. }
  91. if (f == 0) {
  92. putchar(NUMBER[0]);
  93. }
  94. putchar('\n');
  95. }
  96.  
  97. void addDecimal(int* decimal, const int size, const int add) {
  98. int i;
  99. (*decimal) += add;
  100. for (i = 1; i < size; i++) {
  101. if (*decimal < 10) {
  102. return;
  103. }
  104. (*(decimal + 1)) += *decimal / 10;
  105. (*decimal) %= 10;
  106. decimal++;
  107. }
  108. (*decimal) %= 10;
  109. }
  110.  
  111. void mulDecimal(int* decimal, const int size, const int mul) {
  112. int i;
  113. int flow = 0;
  114. for (i = 1; i < size; i++) {
  115. (*decimal) *= mul;
  116. (*decimal) += flow;
  117. if (*decimal > 9) {
  118. flow = *decimal / 10;
  119. (*decimal) %= 10;
  120. } else {
  121. flow = 0;
  122. }
  123. decimal++;
  124. }
  125. (*decimal) %= 10;
  126. }
  127.  
  128. void bits2decimal(const int* bits, const int b_size, int* decimal, const int d_size) {
  129. int f = 0;
  130. int i;
  131. bits += b_size - 1;
  132. for (i = 0; i < b_size; i++) {
  133. if (f == 0) {
  134. if (*bits == 0) {
  135. bits--;
  136. continue;
  137. }
  138. f = 1;
  139. }
  140. mulDecimal(decimal, d_size, 2);
  141. if (*bits == 1) {
  142. addDecimal(decimal, d_size, 1);
  143. }
  144. bits--;
  145. }
  146. }
  147.  
  148.  
  149.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
BITS:
1010111010011010000010100011100111110100110001001111011101001101101010011000011010000010010110010100111001101101101001100010111010111011100010110011111010011100110000111010010000010011111101011111100001011001000010001001010100110001001000110001001001101101011101100110111111000011010011011111101000101011100110100101000101010111011011100001110111100011100010000011000110000111111011111000011100111110011110010010110101111101110111110010000001101101101000100011110101001000000000010010000110101111100100110001011001111001111101101101011101101001111000001000100011010010011111011000100100011101000000100010000010011011110100100111001001110000100010110000001010000001011011011111011010001100010101100000000010011101111001011110011000001000001000001001110100001100100111000100110010101010011100000001110010110011111010110110011111010000010100011110101010100011101001101011011111111110101110010001011100011000010000101100000101001010101110101011100010010101001011110101110001111000110100000110101100111101
DECIMAL:
7308095478423318885807579709539867782621157866027581272074081820985521359318862068721081251581261305623623915045338120306971806150294972931371299333983306384312835854745359424112763464176977734260433753320549023072213881706734800277195427863257173978246947821813546764577941799678558806927999375534909