atoi&itoa函数

  • atoi函数实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*************************************************************************
> File Name: Atoi.c
> Author: qinchao
> Created Date:2018-01-28 Time:04:36:18.
************************************************************************/
// 32bits无符号整数表示范围[0, 2^32-1]
// 32bits有符号整数表示范围为[-2^31, 2^31-1]
#define INTEGER32_MAX (2147483647)
#define INTEGER32_MIN (-2147483648)
int Atoi(const char *str) {
unsigned int value = 0;
// 1表示负数,0表示整数
int negative = 0;
//判断str指针非NULL
if (str != NULL) {
//判断是否为负数
if (str[0] == '-') {
negative = 1;
str++;
}
else if (str[0] == '+') {
str++;
}
// str指向的字符串以0结尾
while (str[0] != '