-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_funcs.cc
108 lines (101 loc) · 1.67 KB
/
std_funcs.cc
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stack>
void my_strcpy(char *src, char *dst) {
if (!src || !dst) {
return;
}
// 假设src,dst都是合法的字符串
int i = 0;
while (dst[i]) {
src[i] = dst[i];
i++;
}
}
int my_strcmp(char *src, char *dst) {
if (!src || !dst) {
return 1;
}
int i = 0;
while (src[i] && dst[i]) {
if (src[i] < dst[i]) {
return -1;
} else if (src[i] > dst[i]) {
return 1;
}
i++;
}
if (src[i]) {
return 1;
}
if (dst[i]) {
return -1;
}
return 0;
}
// 只考虑十进制
int my_atoi(char *src) {
int i = 0;
// 检查空串
if (!src) {
return 0;
}
// 处理负号
int minus = 1;
if (src[i] == '-') {
i++;
minus = -1;
}
int sum = 0;
while (src[i]) {
if (src[i] <= '9' && src[i] >= '0') {
sum = (src[i] - '0') + sum * 10;
// 处理溢出
if (sum < 0) {
return -1;
}
} else {
// 如果不是合法字符,就直接返回
break;
}
i++;
}
return sum * minus;
}
void swap(char *src, int a, int b) {
int t = src[a];
src[a] = src[b];
src[b] = t;
}
void my_itoa(int src, char *dst) {
// 处理最小值
if (src == INT_MIN) {
strcpy(dst, "-2147483648");
return;
}
int j = 0;
if (src < 0) {
dst[j++] = '-';
src = -src;
}
std::stack<char> tmp;
while (src != 0) {
tmp.push(src % 10 + '0');
src = src / 10;
}
while (!tmp.empty()) {
dst[j++] = tmp.top();
tmp.pop();
}
}
int main(int argc, char *argv[]) {
char dst[100];
memset(dst, 0, sizeof(dst));
my_itoa(INT_MIN, dst);
printf("%s\n", dst);
//printf("%d\n", my_strcmp("0123", "123"));
//printf("%d\n", my_strcmp("", "123"));
//printf("%d\n", my_strcmp(NULL, "123"));
}