-
Notifications
You must be signed in to change notification settings - Fork 5
/
01a.cpp
49 lines (45 loc) · 1.11 KB
/
01a.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
#include <cstring>
int parseQuantity(const char * text)
{
if(text == nullptr)
{
return 0;
}
std::size_t length{std::strlen(text)};
if(length == 0)
{
return 0;
}
else
{
static constexpr int asciiNumGap{48};
int val{0};
bool isNegative{false};
for(int i{0}; i < length; ++i)
{
if(i == 0 && text[i] == '-')
{
isNegative = true;
continue;
}
switch(text[i])
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
val *= 10;
if(isNegative)
{
val -= static_cast<int>(text[i]) - asciiNumGap;
}
else
{
val += static_cast<int>(text[i]) - asciiNumGap;
}
break;
default:
return val;
}
}
return val;
}
}