Guest User

Untitled

a guest
Jan 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. int my_atoi(char* pointer)
  2. {
  3. int result = 0;
  4. char* pointer1;
  5. multiplier = 1;
  6. char sign = 1;
  7.  
  8. if(*pointer == '-')
  9. sign =- 1;
  10.  
  11. pointer1 = pointer;
  12.  
  13. while(*pointer != '')
  14. {
  15. if(*pointer >= '0' && *pointer <= '9')
  16. multiplier = multiplier * 10;
  17.  
  18. pointer = pointer + 1;
  19. }
  20.  
  21. pointer = pointer1;
  22.  
  23. while(*pointer != '')
  24. {
  25. if(*pointer >= '0' && *pointer <= '9')
  26. {
  27. result = result + ( (*pointer%48) * multiplier);
  28. multiplier = multiplier / 10;
  29. }
  30.  
  31. pointer = pointer+1;
  32. }
  33.  
  34. return (result * sign) / 10;
  35. }
  36.  
  37. while(*pointer != '')
  38. {
  39. if(*pointer >= '0' && *pointer <= '9')
  40. multiplier = multiplier * 10;
  41.  
  42. pointer = pointer + 1;
  43. }
  44.  
  45. pointer = pointer1;
  46.  
  47. while(*pointer != '')
  48. {
  49. if(*pointer >= '0' && *pointer <= '9')
  50. {
  51. result = result + ( (*pointer%48) * multiplier);
  52. multiplier = multiplier / 10;
  53. }
  54.  
  55. pointer = pointer+1;
  56. }
  57.  
  58. while (isdigit(*c))
  59. {
  60. value *= 10;
  61. value += (int) (*c - '0');
  62. c++;
  63. }
  64.  
  65. result = result + ( (*pointer%48) * multiplier);
  66.  
  67. if(*pointer == '-')
  68. sign =- 1;
  69.  
  70. if(*pointer == '-') sign = -1;
  71.  
  72. int my_atoi(const char* pointer)
  73.  
  74. pointer++; // same as pointer = pointer+1;
  75. multiplier /= 10; // same as multiplier = multiplier / 10;
  76. multiplier *= 10; // same as multiplier = multiplier * 10;
  77.  
  78. #include <stdio.h>
  79. #include <assert.h>
  80. #include <ctype.h>
  81.  
  82. long long int my_atoi(const char *c)
  83. {
  84. long long int value = 0;
  85. int sign = 1;
  86. if( *c == '+' || *c == '-' )
  87. {
  88. if( *c == '-' ) sign = -1;
  89. c++;
  90. }
  91. while (isdigit(*c))
  92. {
  93. value *= 10;
  94. value += (int) (*c-'0');
  95. c++;
  96. }
  97. return (value * sign);
  98. }
  99.  
  100. int main(void)
  101. {
  102. assert(5 == my_atoi("5"));
  103. assert(-2 == my_atoi("-2"));
  104. assert(-1098273980709871235 == my_atoi("-1098273980709871235"));
  105. puts("All good."); // I reach this statement on my system
  106. }
  107.  
  108. int my_atoi(const char* pointer) {
  109. while (isspace((unsigned char) *pointer)) {
  110. pointer++;
  111. }
  112. ...
  113.  
  114. #include <ctype.h>
  115. #include <limits.h>
  116.  
  117. int my_atoi(const char* pointer) { // good idea to make the `const`
  118. int result = 0;
  119. while (isspace((unsigned char) *pointer)) {
  120. pointer++;
  121. }
  122. char sign = *pointer;
  123. if (*pointer == '-' || *pointer == '+') { // text could lead with a '+'
  124. pointer++;
  125. }
  126. int ch;
  127. // isdigit() expects an unsigned char or EOF, not char
  128. while ((ch = (unsigned char)(*pointer)) != 0) {
  129. if (!isdigit(ch)) break;
  130. ch -= '0';
  131. // Will overflow occur?
  132. if ((result < INT_MIN/10) ||
  133. (result == INT_MIN/10 && ch > -(INT_MIN%10))) Handle_Overflow();
  134. result *= 10;
  135. result -= ch; // - , not +
  136. pointer++;
  137. }
  138. if (sign != '-') {
  139. if (result < -INT_MAX) Handle_Overflow();
  140. result = -result;
  141. }
  142. return result;
  143. }
  144.  
  145. int my_atoi(const char *s, int *ErrorCode);
  146.  
  147. int my_atoi(const char *s, const char **endptr);
  148.  
  149. int my_atoi(const char* pointer) {
  150. int result = 0;
  151. while (isspace((unsigned char) *pointer)) {
  152. pointer++;
  153. }
  154. char sign = *pointer;
  155. if (*pointer == '-' || *pointer == '+') {
  156. pointer++;
  157. }
  158. while (isdigit((unsigned char)*pointer)) {
  159. result = result*10 - (*pointer++ - '0');
  160. }
  161. if (sign != '-') {
  162. result = -result;
  163. }
  164. return result;
  165. }
  166.  
  167. char sign = *pointer;
  168. if (*pointer == '-' || *pointer == '+') {
  169. pointer++;
  170. }
  171.  
  172. char sign = *pointer;
  173. if (sign == '-' || sign == '+') {
  174. pointer++;
  175. }
  176.  
  177. #include <string.h>
  178. #include <math.h>
  179. #include <stdbool.h>
  180.  
  181. int to_natural_number(const char* string) {
  182. int index = strlen(string) - 1;
  183. int number = pow(10, index) * (*string - '0');
  184.  
  185. return (index == 0) ? number : number + to_natural_number(string + 1);
  186. }
  187.  
  188. bool isMinusSign(char symbol) {
  189. return symbol == '-';
  190. }
  191.  
  192. int my_atoi(const char* string) {
  193. int sign = isMinusSign(*string) ? -1 : 1;
  194. int offset = isMinusSign(*string) ? 1 : 0;
  195.  
  196. return sign * to_natural_number(string + offset);
  197. }
  198.  
  199. /* test cases */
  200. my_atoi("-100") == -100;
  201. my_atoi("0") == 0;
  202. my_atoi("100") == 100;
Add Comment
Please, Sign In to add comment