-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_8_stringToInteger.c
More file actions
85 lines (78 loc) · 2.2 KB
/
Copy path_8_stringToInteger.c
File metadata and controls
85 lines (78 loc) · 2.2 KB
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
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <limits.h>
// https://leetcode.com/problems/string-to-integer-atoi/
// 25.12.23 - Vladislav Ershov
#define INT_MAX_DIV_10 214748364 // INT_MAX / 10
#define INT_MAX_PLACE_NUMBER_M1 100000000
int myAtoi(char* s) {
int len = strlen(s);
if(len > 0) {
if (isalpha(s[0])) return 0;
}
int res = 0;
bool negative = false;
bool marked = false;
bool started = false;
for (int i = 0; i < len; ++i) {
if(s[i] == ' ' && !started && !marked) continue;
if(isdigit(s[i])) {
started = true;
if(s[i] =='0' && res == 0) {
continue;
}
int dgt = ((int)s[i]-48);
if (res > INT_MAX_DIV_10)
return negative ? INT_MIN : INT_MAX;
if(res == INT_MAX_DIV_10) {
if (dgt > 7 || res / INT_MAX_PLACE_NUMBER_M1 > 2)
return negative ? INT_MIN : INT_MAX;
}
res *= 10;
res += dgt;
}
else {
if(s[i] == '-') {
if(started) break;
if(marked) break;
negative = !negative;
marked = true;
}
else if(s[i] == '+') {
if(started) break;
if(marked) return 0;
marked = true;
}
else {
break;
}
}
}
if(negative) res *= -1;
return res;
}
#define TEST(s, exp) assert(myAtoi(s) == exp)
int main() {
TEST("42", 42);
TEST(" -42", -42);
TEST("4193 with words", 4193);
TEST("words and 987", 0);
TEST("-91283472332", -2147483648);
TEST("3.14159", 3);
TEST("+-12", 0);
TEST("00000-42a1234", 0);
TEST(" 0000000000012345678", 12345678);
TEST("21474836460", 2147483647);
TEST(" -0012a42", -12);
TEST(" +0 123", 0);
TEST(".1", 0);
TEST("-2147483647", -2147483647);
TEST("2147483646", 2147483646);
TEST("21474836460", INT_MAX);
TEST("-91283472332", INT_MIN);
TEST("-5--", -5);
TEST("123-", 123);
TEST(" + 413", 0);
}