-
Notifications
You must be signed in to change notification settings - Fork 5
/
05a.cpp
32 lines (31 loc) · 939 Bytes
/
05a.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
/**
* Return parsing int from the const char *word.
*
* @param word const char* to be parsing
* @return int of parsing word / 0 when word can't be parsed
* / -1 when word is null / -2 when word is greater than max int size
*/
int parseQuantity(const char *word) {
if (!word) {
return -1; // word can't be null
}
if (*word < '0' || *word > '9') {
return 0;
}
const char *wordPointerCopy = word;
long int parsingResult = *wordPointerCopy - '0';
// 2 147 483 647 -> max int size
while (parsingResult <= 2147483647) {
wordPointerCopy++;
if (*wordPointerCopy >= '0' && *wordPointerCopy <= '9') {
parsingResult *= 10;
parsingResult += *wordPointerCopy - '0';
} else {
break;
}
}
if (parsingResult > 2147483647) {
return -2; // word can't be parsed (too much number)
}
return (int) parsingResult;
}