-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path16.cpp
54 lines (45 loc) · 1004 Bytes
/
16.cpp
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
int parseQuantity(const char *data)
{
constexpr int maxInt = 2147483647;
unsigned long long result = 0;
int resultSign = 1; //positive
auto dataPtr = data;
if (dataPtr == nullptr)
{
return 0;
}
if (dataPtr[0] == '-')
{
resultSign = -1;
++dataPtr;
}
bool oneTime = true;
for (char c = *dataPtr; c; c = *++dataPtr)
{
auto cInteger = static_cast<unsigned>(c);
if (c == '\0' || cInteger > 57 || cInteger < 48)
{
break;
}
if (oneTime)
{
result = static_cast<unsigned long long>(c) - 48;
oneTime = false;
}
else
{
result *= 10;
result += static_cast<unsigned long long>(c) - 48;
if (result > maxInt)
{
return 0;
}
}
}
result *= resultSign;
if (result)
{
return static_cast<int>(result);
}
return 0;
}