p*l
2 楼
马上要file PERM了,律师发了9089表格草稿让我核对。
查了相关问题,有人提到要确定是按哪个category申请的。怎么看呢,表格上都没有这
一项,招工要求上倒是注明了要master+1yr, 这个就行了吗?
谢谢!
查了相关问题,有人提到要确定是按哪个category申请的。怎么看呢,表格上都没有这
一项,招工要求上倒是注明了要master+1yr, 这个就行了吗?
谢谢!
w*g
3 楼
想在后院装一个storage building。sears好象价格比较好。但是有好几种,不知道有
什么不同呢?价格差别比较大.还有,后院的地上没有铺水泥,但铺了砖头,没有用水泥固定。还需要做floor base么?
http://www.sears.com/shc/s/p_10153_12605_07176083000P?prdNo=1
http://www.sears.com/shc/s/p_10153_12605_07168627000P?prdNo=25
http://www.sears.com/shc/s/p_10153_12605_07168616000P?prdNo=7
谢谢!
什么不同呢?价格差别比较大.还有,后院的地上没有铺水泥,但铺了砖头,没有用水泥固定。还需要做floor base么?
http://www.sears.com/shc/s/p_10153_12605_07176083000P?prdNo=1
http://www.sears.com/shc/s/p_10153_12605_07168627000P?prdNo=25
http://www.sears.com/shc/s/p_10153_12605_07168616000P?prdNo=7
谢谢!
r*e
4 楼
看看哥怎么剪指甲的
s*c
6 楼
140上面才有。
w*g
7 楼
tititii
i*l
8 楼
PSed..
g*e
9 楼
这些经典题还是找机会背出来吧最妥
i*r
11 楼
我也被问过这个,当时没想清楚怎么处理溢出的问题
挂了
挂了
i*e
15 楼
用 long long 检测 overflow,简单一些。。。
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sign = 1;
while (*str == ' ') {
str++;
}
if (*str == '+') {
str++;
} else if (*str == '-') {
sign = -1;
str++;
}
long long num = 0;
bool overflow = false;
while (!overflow && isAlpha(*str)) {
int dig = *str - '0';
num = num*10 + dig;
if (sign == 1 && num > INT_MAX ||
sign == -1 && -num < INT_MIN)
overflow = true;
str++;
}
if (sign == -1) {
num = -num;
}
if (overflow) {
return (sign == 1) ? INT_MAX : INT_MIN;
} else {
return (int)num;
}
}
bool isAlpha(char c) {
return c >= '0' && c <= '9';
}
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sign = 1;
while (*str == ' ') {
str++;
}
if (*str == '+') {
str++;
} else if (*str == '-') {
sign = -1;
str++;
}
long long num = 0;
bool overflow = false;
while (!overflow && isAlpha(*str)) {
int dig = *str - '0';
num = num*10 + dig;
if (sign == 1 && num > INT_MAX ||
sign == -1 && -num < INT_MIN)
overflow = true;
str++;
}
if (sign == -1) {
num = -num;
}
if (overflow) {
return (sign == 1) ? INT_MAX : INT_MIN;
} else {
return (int)num;
}
}
bool isAlpha(char c) {
return c >= '0' && c <= '9';
}
i*e
17 楼
还有另一个检测溢出的思路就是 wraparound,因为这里只是乘于十而以,如果wraparound 的话 sign 必定会有变化,例如从 + 变成 -。但这个因
语言而异。Java 有wraparound,可以利用此方法来检测溢出。但是 C++ 里的 standard 说明溢出是 undefined behavior,不是每一个编译器都有 wraparound。
语言而异。Java 有wraparound,可以利用此方法来检测溢出。但是 C++ 里的 standard 说明溢出是 undefined behavior,不是每一个编译器都有 wraparound。
i*e
21 楼
Thanks for your test case.
Actually this is ok, because there is always a '\0' character that
terminates a C-style string.
isAlpha('\0') will evaluate as false and break out the loop immediately.
【在 c*****e 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: another issue:
: suppose char s[] = " -"
: your code will access over the boundary of the string
Actually this is ok, because there is always a '\0' character that
terminates a C-style string.
isAlpha('\0') will evaluate as false and break out the loop immediately.
【在 c*****e 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: another issue:
: suppose char s[] = " -"
: your code will access over the boundary of the string
c*e
22 楼
and how about
char s = '-'
then call your func with &s?
firstly, "-" is not a valid number, but you return 0.
second, it may access over boundary of memory as well.
【在 i**********e 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: Thanks for your test case.
: Actually this is ok, because there is always a '\0' character that
: terminates a C-style string.
: isAlpha('\0') will evaluate as false and break out the loop immediately.
char s = '-'
then call your func with &s?
firstly, "-" is not a valid number, but you return 0.
second, it may access over boundary of memory as well.
【在 i**********e 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: Thanks for your test case.
: Actually this is ok, because there is always a '\0' character that
: terminates a C-style string.
: isAlpha('\0') will evaluate as false and break out the loop immediately.
c*e
24 楼
Tested it with
char s = '1', r = ' ';
std::cout << atoi(&s) << "\n";
it prints something randomly, and are wrong.
[[email protected] hello]# ./atoi
1
[[email protected] hello]# ./atoi
10
【在 i**********e 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: Thanks for your test case.
: Actually this is ok, because there is always a '\0' character that
: terminates a C-style string.
: isAlpha('\0') will evaluate as false and break out the loop immediately.
char s = '1', r = ' ';
std::cout << atoi(&s) << "\n";
it prints something randomly, and are wrong.
[[email protected] hello]# ./atoi
1
[[email protected] hello]# ./atoi
10
【在 i**********e 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: Thanks for your test case.
: Actually this is ok, because there is always a '\0' character that
: terminates a C-style string.
: isAlpha('\0') will evaluate as false and break out the loop immediately.
s*e
29 楼
贴个我之前写的代码,仿照java里面的源码写的
public static long stringToLong(String str) throws Exception{
if(!chekValid(str)){
throw new Exception("invalid input!!");
}
long limit;
boolean isNegative = false;
int curIndex = 0;
if(str.charAt(0) == '-'){
limit = Long.MIN_VALUE;
isNegative = true;
curIndex = 1;
}else{
limit = -1 * Long.MAX_VALUE;
}
long preLimit = limit/10;
long result = -1 * (str.charAt(curIndex++) - '0');
while(curIndex < str.length()){
int digit = (str.charAt(curIndex++) - '0');
if(result < preLimit) throw new Exception("preLimit overflow !");
result *= 10;
if(result < limit + digit) throw new Exception("limit overflow!"
);
result = result - digit;
}
if(!isNegative){
result *= -1;
}
return result;
}
public static boolean chekValid(String str){
if(str == null || str.length() == 0) return false;
int strtIndex = 0;
if(str.charAt(0) == '-'){
if(str.length() == 1){
return false;
}
strtIndex = 1;
}
for(int i = strtIndex; i < str.length(); i++){
if(str.charAt(i) - '0' < 0 || str.charAt(i) - '0' > 9)
return false;
}
return true;
}
}
public static long stringToLong(String str) throws Exception{
if(!chekValid(str)){
throw new Exception("invalid input!!");
}
long limit;
boolean isNegative = false;
int curIndex = 0;
if(str.charAt(0) == '-'){
limit = Long.MIN_VALUE;
isNegative = true;
curIndex = 1;
}else{
limit = -1 * Long.MAX_VALUE;
}
long preLimit = limit/10;
long result = -1 * (str.charAt(curIndex++) - '0');
while(curIndex < str.length()){
int digit = (str.charAt(curIndex++) - '0');
if(result < preLimit) throw new Exception("preLimit overflow !");
result *= 10;
if(result < limit + digit) throw new Exception("limit overflow!"
);
result = result - digit;
}
if(!isNegative){
result *= -1;
}
return result;
}
public static boolean chekValid(String str){
if(str == null || str.length() == 0) return false;
int strtIndex = 0;
if(str.charAt(0) == '-'){
if(str.length() == 1){
return false;
}
strtIndex = 1;
}
for(int i = strtIndex; i < str.length(); i++){
if(str.charAt(i) - '0' < 0 || str.charAt(i) - '0' > 9)
return false;
}
return true;
}
}
w*z
34 楼
Source Code from Java
/*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Josh Bloch
* @version 1.92, 04/07/06
* @since JDK1.0
*/
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign
* indicate a negative value. The resulting integer value is returned.
*
*
* @param s the
* representation to be parsed
* @param radix the radix to be used while parsing
* @return the integer represented by the string argument in the
* specified radix.
* @exception NumberFormatException if the
* does not contain a parsable
*/
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;
if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / radix;
if (i < max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}
/*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Josh Bloch
* @version 1.92, 04/07/06
* @since JDK1.0
*/
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign
'-'
('\u002D'
) to* indicate a negative value. The resulting integer value is returned.
*
* An exception of type NumberFormatException
is
* thrown if any of the following situations occurs:
*
- The first argument is
null
or is a string of
* length zero.
* - The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.
* - Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
*'-'
('\u002D'
) provided that the
* string is longer than length 1.
* - The value represented by the string is not a value of type
*int
.
*
*
* Examples:
*
* parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("2147483647", 10) returns 2147483647
* parseInt("-2147483648", 10) returns -2147483648
* parseInt("2147483648", 10) throws a NumberFormatException
* parseInt("99", 8) throws a NumberFormatException
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787
*
*
* @param s the
String
containing the integer* representation to be parsed
* @param radix the radix to be used while parsing
s
.* @return the integer represented by the string argument in the
* specified radix.
* @exception NumberFormatException if the
String
* does not contain a parsable
int
.*/
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;
if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / radix;
if (i < max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}
a*a
35 楼
按照那个 从左往右*10+当前位 的方法,溢出的时候数就变小了,这种方法可行吗?
k*y
36 楼
这样写是不是比较safe?
bool is_overflow(int sign, int head_number, int unit_number) {
if (sign == 1)
return ((INT_MAX - unit_number)/10 >= head_number)?
false : true;
else
return ((INT_MIN + unit_number)/10 <= head_number)?
false : true;
}
int my_atoi(char *s, int &error) {
int the_number = 0;
int sign = 1;
error = 0;
// skip the the spaces at front
while (*s && *s==' ')
++s;
// determine if there is a sign
if (*s == '-'){
sign = -1;
++s;
}
else if (*s == '+')
++s;
// valid first digit?
if (*s >= '0' && *s <= '9')
// loop while it is still a digit
while (*s >= '0' && *s <= '9') {
int unit_number = *s-'0';
if (is_overflow(sign, the_number, unit_number)) {
error = 1;
break;
}
else
the_number = the_number*10 + sign*unit_number;
++s;
}
else
error = 1;
return the_number;
}
bool is_overflow(int sign, int head_number, int unit_number) {
if (sign == 1)
return ((INT_MAX - unit_number)/10 >= head_number)?
false : true;
else
return ((INT_MIN + unit_number)/10 <= head_number)?
false : true;
}
int my_atoi(char *s, int &error) {
int the_number = 0;
int sign = 1;
error = 0;
// skip the the spaces at front
while (*s && *s==' ')
++s;
// determine if there is a sign
if (*s == '-'){
sign = -1;
++s;
}
else if (*s == '+')
++s;
// valid first digit?
if (*s >= '0' && *s <= '9')
// loop while it is still a digit
while (*s >= '0' && *s <= '9') {
int unit_number = *s-'0';
if (is_overflow(sign, the_number, unit_number)) {
error = 1;
break;
}
else
the_number = the_number*10 + sign*unit_number;
++s;
}
else
error = 1;
return the_number;
}
n*e
37 楼
How to handle overflow issue on atoi? Hopefully this will be of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8), the number is too big,
and we will return a range error.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8), the number is too big,
and we will return a range error.
n*e
38 楼
How to handle overflow issue on atoi? Hope this is of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8),the number is too big,
and we will return a range error.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8),the number is too big,
and we will return a range error.
n*e
39 楼
How to handle overflow issue on atoi? Hopefully this will be of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8), the number is too big,
and we will return a range error.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8), the number is too big,
and we will return a range error.
n*e
40 楼
How to handle overflow issue on atoi? Hopefully this will be of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8), the number is too big,
and we will return a range error.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8), the number is too big,
and we will return a range error.
n*e
41 楼
How to handle overflow issue on atoi? Hope this is of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8),the number is too big,
and we will return a range error.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8),the number is too big,
and we will return a range error.
n*e
42 楼
How to handle overflow issue on atoi? Hope this is of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8),the number is too big,
and we will return a range error.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input base is 10, cutoff will be set to 214748364 and cutLimit to either 7 (
sign==1) or 8 (sign==-1), meaning that if we have accumulated a value >
214748364, or equal but the next digit is > 7 (or 8),the number is too big,
and we will return a range error.
p*i
43 楼
int myatoi(const string& str) {
const int n = str.size();
int curr = 0; // current index
while ((curr < n) && (isspace(str[curr]))) curr++; // skip leading
spaces
int sign = 1; // positive
if (str[curr] == '-') {
sign = -1; curr++;
}
if (curr == n)
throw runtime_error("Invalid input");
// prepare overflow check
int base, extra;
if (sign == 1) {
base = numeric_limits::max() / 10;
extra = numeric_limits::max() % 10;
} else {
// be careful: |MIN| = |MAX|+1
base = -(numeric_limits::min() / 10);
extra = -(numeric_limits::min() % 10);
}
unsigned result = 0;
while ((curr < n) && (isdigit(str[curr]))) {
// first, test if overflow
if (result < base) {
// no problem
result = result*10 + (str[curr] - '0');
} else if (result > base) {
// overflow happened
throw overflow_error("Overflow");
} else {
// result == base, tricky
int digit = str[curr] - '0';
if (digit <= extra)
result = result*10 + digit;
else
throw overflow_error("Overflow");
}
curr++;
}
// we need to know the exact reason of quitting the while loop
while (curr < n) {
if (!isspace(str[curr++]))
throw runtime_error("Invalid input");
}
return (sign==1)? result:-result;
}
【在 c*****e 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 主要是溢出判断上,正的和负的有区别。
: 网上一搜原来几乎全部都写的有同样问题。
const int n = str.size();
int curr = 0; // current index
while ((curr < n) && (isspace(str[curr]))) curr++; // skip leading
spaces
int sign = 1; // positive
if (str[curr] == '-') {
sign = -1; curr++;
}
if (curr == n)
throw runtime_error("Invalid input");
// prepare overflow check
int base, extra;
if (sign == 1) {
base = numeric_limits
extra = numeric_limits
} else {
// be careful: |MIN| = |MAX|+1
base = -(numeric_limits
extra = -(numeric_limits
}
unsigned result = 0;
while ((curr < n) && (isdigit(str[curr]))) {
// first, test if overflow
if (result < base) {
// no problem
result = result*10 + (str[curr] - '0');
} else if (result > base) {
// overflow happened
throw overflow_error("Overflow");
} else {
// result == base, tricky
int digit = str[curr] - '0';
if (digit <= extra)
result = result*10 + digit;
else
throw overflow_error("Overflow");
}
curr++;
}
// we need to know the exact reason of quitting the while loop
while (curr < n) {
if (!isspace(str[curr++]))
throw runtime_error("Invalid input");
}
return (sign==1)? result:-result;
}
【在 c*****e 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 主要是溢出判断上,正的和负的有区别。
: 网上一搜原来几乎全部都写的有同样问题。
相关阅读
浙大副校长的大手笔超级碰瓷这能拍到啥?交友8上这个mm的id叫“能死”——Can DieRe: 三胖的氢弹用的是啥构型 (转载)用超巨型马克杯去星巴克买咖啡,结果深入揭批星球大战种族歧视的真相老型回帖的八哥是不是无解了?周中午间福利我再贴个大笑话 (转载)学术一下,我发现有的男式小便器很不卫生!!! (转载)也来说句日狗女神 (转载)今年没有了white christmas英企进口中国廉价花岗岩被批“亵渎神圣”留园的在线影视完蛋了Re: 网友:恒大要能进巴萨一个,就裸奔长安街 (转载)人往高处走,水往低处流 (转载)话说我鳖为啥不整治一下碰瓷?货币战争爆发!宋鸿兵太原演讲被围殴 (转载)gif图,江西初中生自行车摔倒后被车压爆头 (转载)(ZT) 张益唐的这篇八卦没人转? (转载)