• atoi() & itoa()函数的内部的实现



    点击(此处)折叠或打开

    1. /***
    2. *atox.c - atoi and atol conversion
    3. *
    4. * Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
    5. *
    6. *Purpose:
    7. * Converts a character string into an int or long.
    8. *
    9. *******************************************************************************/

    10. #include <cruntime.h>
    11. #include <stdlib.h>
    12. #include <ctype.h>

    13. /***
    14. *long atol(char *nptr) - Convert string to long
    15. *
    16. *Purpose:
    17. * Converts ASCII string pointed to by nptr to binary.
    18. * Overflow is not detected.
    19. *
    20. *Entry:
    21. * nptr = ptr to string to convert
    22. *
    23. *Exit:
    24. * return long int value of the string
    25. *
    26. *Exceptions:
    27. * None - overflow is not detected.
    28. *
    29. *******************************************************************************/

    30. long __cdecl atol(
    31. const char *nptr
    32. )
    33. {
    34. int c; /* current char */
    35. long total; /* current total */
    36. int sign; /* if ''-'', then negative, otherwise positive */

    37. /* skip whitespace */
    38. while ( isspace((int)(unsigned char)*nptr) )
    39. ++nptr;

    40. c = (int)(unsigned char)*nptr++;
    41. sign = c; /* save sign indication */
    42. if (c == ''-'' || c == ''+'')
    43. c = (int)(unsigned char)*nptr++; /* skip sign */

    44. total = 0;

    45. while (isdigit(c)) {
    46. total = 10 * total + (c - ''0''); /* accumulate digit */
    47. c = (int)(unsigned char)*nptr++; /* get next char */
    48. }

    49. if (sign == ''-'')
    50. return -total;
    51. else
    52. return total; /* return result, negated if necessary */
    53. }


    54. /***
    55. *int atoi(char *nptr) - Convert string to long
    56. *
    57. *Purpose:
    58. * Converts ASCII string pointed to by nptr to binary.
    59. * Overflow is not detected. Because of this, we can just use
    60. * atol().
    61. *
    62. *Entry:
    63. * nptr = ptr to string to convert
    64. *
    65. *Exit:
    66. * return int value of the string
    67. *
    68. *Exceptions:
    69. * None - overflow is not detected.
    70. *
    71. *******************************************************************************/

    72. int __cdecl atoi(
    73. const char *nptr
    74. )
    75. {
    76. return (int)atol(nptr);
    77. }

    78. #ifndef _NO_INT64

    79. __int64 __cdecl _atoi64(
    80. const char *nptr
    81. )
    82. {
    83. int c; /* current char */
    84. __int64 total; /* current total */
    85. int sign; /* if ''-'', then negative, otherwise positive */

    86. /* skip whitespace */
    87. while ( isspace((int)(unsigned char)*nptr) )
    88. ++nptr;

    89. c = (int)(unsigned char)*nptr++;
    90. sign = c; /* save sign indication */
    91. if (c == ''-'' || c == ''+'')
    92. c = (int)(unsigned char)*nptr++; /* skip sign */

    93. total = 0;

    94. while (isdigit(c)) {
    95. total = 10 * total + (c - ''0''); /* accumulate digit */
    96. c = (int)(unsigned char)*nptr++; /* get next char */
    97. }

    98. if (sign == ''-'')
    99. return -total;
    100. else
    101. return total; /* return result, negated if necessary */
    102. }

    103. #endif /* _NO_INT64 */


    104. #include <msvcrt/errno.h>
    105. #include <msvcrt/stdlib.h>
    106. #include <msvcrt/internal/file.h>
    107. char* _itoa(int value, char* string, int radix)
    108. {
    109. char tmp[33];
    110. char* tp = tmp;
    111. int i;
    112. unsigned v;
    113. int sign;
    114. char* sp;

    115. if (radix > 36 || radix <= 1)
    116. {
    117. __set_errno(EDOM);
    118. return 0;
    119. }

    120. sign = (radix == 10 && value < 0);
    121. if (sign)
    122. v = -value;
    123. else
    124. v = (unsigned)value;
    125. while (v || tp == tmp)
    126. {
    127. i = v % radix;
    128. v = v / radix;
    129. if (i < 10)
    130. *tp++ = i+''0'';
    131. else
    132. *tp++ = i + ''a'' - 10;
    133. }

    134. if (string == 0)
    135. string = (char*)malloc((tp-tmp)+sign+1);
    136. sp = string;

    137. if (sign)
    138. *sp++ = ''-'';
    139. while (tp > tmp)
    140. *sp++ = *--tp;
    141. *sp = 0;
    142. return string;
    143. }

    阅读(422) | 评论(0) | 转发(0) |
  • 相关阅读:
    [TJOI2015]棋盘
    [FJOI2017]矩阵填数——容斥
    [ZJOI2016]小星星
    [HEOI2013]SAO ——计数问题
    ZJOI2008 骑士
    莫队算法——暴力出奇迹
    可持久化线段树
    dij与prim算法
    LCA 最近公共祖先
    Linux 设置交换分区
  • 原文地址:https://www.cnblogs.com/black/p/5171831.html
Copyright © 2020-2023  润新知