numerate_number.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include<stdlib.h>
  18. #include<string.h>
  19. // void* calloc(int count, int size);
  20. #define TRUE 1
  21. //CONSTANT TRUE 1
  22. #define FALSE 0
  23. //CONSTANT FALSE 0
  24. char* numerate_number(int a)
  25. {
  26. char* result = calloc(16, sizeof(char));
  27. int i = 0;
  28. /* Deal with Zero case */
  29. if(0 == a)
  30. {
  31. result[0] = '0';
  32. return result;
  33. }
  34. /* Deal with negatives */
  35. if(0 > a)
  36. {
  37. result[0] = '-';
  38. i = 1;
  39. a = a * -1;
  40. }
  41. /* Using the largest 10^n number possible in 32bits */
  42. int divisor = 0x3B9ACA00;
  43. /* Skip leading Zeros */
  44. while(0 == (a / divisor)) divisor = divisor / 10;
  45. /* Now simply collect numbers until divisor is gone */
  46. while(0 < divisor)
  47. {
  48. result[i] = ((a / divisor) + 48);
  49. a = a % divisor;
  50. divisor = divisor / 10;
  51. i = i + 1;
  52. }
  53. return result;
  54. }
  55. int char2hex(int c)
  56. {
  57. if (c >= '0' && c <= '9') return (c - 48);
  58. else if (c >= 'a' && c <= 'f') return (c - 87);
  59. else if (c >= 'A' && c <= 'F') return (c - 55);
  60. else return -1;
  61. }
  62. int hex2char(int c)
  63. {
  64. if((c >= 0) && (c <= 9)) return (c + 48);
  65. else if((c >= 10) && (c <= 15)) return (c + 55);
  66. else return -1;
  67. }
  68. int char2dec(int c)
  69. {
  70. if (c >= '0' && c <= '9') return (c - 48);
  71. else return -1;
  72. }
  73. int char2oct(int c)
  74. {
  75. if (c >= '0' && c <= '7') return (c - 48);
  76. else return -1;
  77. }
  78. int dec2char(int c)
  79. {
  80. if((c >= 0) && (c <= 9)) return (c + 48);
  81. else return -1;
  82. }
  83. int numerate_string(char *a)
  84. {
  85. int count = 0;
  86. int index;
  87. int negative;
  88. /* If NULL string */
  89. if(NULL == a)
  90. {
  91. return 0;
  92. }
  93. if(0 == a[0])
  94. {
  95. return 0;
  96. }
  97. /* Deal with hex */
  98. else if (a[0] == '0' && a[1] == 'x')
  99. {
  100. if('-' == a[2])
  101. {
  102. negative = TRUE;
  103. index = 3;
  104. }
  105. else
  106. {
  107. negative = FALSE;
  108. index = 2;
  109. }
  110. while(0 != a[index])
  111. {
  112. if(-1 == char2hex(a[index])) return 0;
  113. count = (16 * count) + char2hex(a[index]);
  114. index = index + 1;
  115. }
  116. }
  117. /* Deal with octal */
  118. else if(a[0] == '0')
  119. {
  120. if('-' == a[1])
  121. {
  122. negative = TRUE;
  123. index = 2;
  124. }
  125. else
  126. {
  127. negative = FALSE;
  128. index = 1;
  129. }
  130. while(0 != a[index])
  131. {
  132. if(-1 == char2oct(a[index])) return 0;
  133. count = (8 * count) + char2oct(a[index]);
  134. index = index + 1;
  135. }
  136. }
  137. /* Deal with decimal */
  138. else
  139. {
  140. if('-' == a[0])
  141. {
  142. negative = TRUE;
  143. index = 1;
  144. }
  145. else
  146. {
  147. negative = FALSE;
  148. index = 0;
  149. }
  150. while(0 != a[index])
  151. {
  152. if(-1 == char2dec(a[index])) return 0;
  153. count = (10 * count) + char2dec(a[index]);
  154. index = index + 1;
  155. }
  156. }
  157. if(negative)
  158. {
  159. count = count * -1;
  160. }
  161. return count;
  162. }