由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - atoi overflow怎么办?
相关主题
函数atoi的实现问个越界的问题
帮忙看看我写的atoi有没有bug, 谢谢问两道bloomberg的题目
atoi的溢出处理的想法atoi很不好写,头都大了...
onsite完,攒rp系列(二)请问如何安全地reverse 一个integer
经典题atoi的溢出处理str2int中overflow该如何处理?
写了个atoi,大家帮看有没有哪里错了?问个简单C reverse int
关于atoi的overflow问个《编程实践》(英文版)里面的问题
问一个atoi overflow的问题弱弱的问一个问题
相关话题的讨论汇总
话题: int话题: pcur话题: overflow话题: res话题: ures
进入JobHunting版参与讨论
1 (共1页)
p*********7
发帖数: 40
1
如果用long去存储最终结果res的话,32位机,long和int的值一样啊。
int myATOI(const char* p){
int flag=1;
if((*p)=='-') {
flag=-1;
p++;
}
long res=0;
while(*p){
res=res*10+(*p)-'0';
if(res>INT_MAX) cout<<"overflow";
p++;
}
return res*flag;
}
请问这个代码怎么改才能支持overflow比较好?
l*********8
发帖数: 4642
2
用long long

【在 p*********7 的大作中提到】
: 如果用long去存储最终结果res的话,32位机,long和int的值一样啊。
: int myATOI(const char* p){
: int flag=1;
: if((*p)=='-') {
: flag=-1;
: p++;
: }
: long res=0;
: while(*p){
: res=res*10+(*p)-'0';

n****n
发帖数: 117
3
返回个0算了
The strtol(), strtoll(), strtoimax(), and strtoq() functions return the
result of the conversion, unless the value would underflow or overflow.
If no conversion could be performed, 0 is returned and the global vari-
able errno is set to EINVAL (the last feature is not portable across
all
platforms). If an overflow or underflow occurs, errno is set to ERANGE
and the function return value is clamped according to the following ta-
ble.

【在 p*********7 的大作中提到】
: 如果用long去存储最终结果res的话,32位机,long和int的值一样啊。
: int myATOI(const char* p){
: int flag=1;
: if((*p)=='-') {
: flag=-1;
: p++;
: }
: long res=0;
: while(*p){
: res=res*10+(*p)-'0';

p*****2
发帖数: 21240
4

long在java里是64位的。你可以改成 if(res< new res)

【在 p*********7 的大作中提到】
: 如果用long去存储最终结果res的话,32位机,long和int的值一样啊。
: int myATOI(const char* p){
: int flag=1;
: if((*p)=='-') {
: flag=-1;
: p++;
: }
: long res=0;
: while(*p){
: res=res*10+(*p)-'0';

h****e
发帖数: 928
5
C/C++用long long会简化很多。只用int也可以解,但是非常繁琐,因为
你要和-(2^31)/10、(2^31-1)/10之类的边界值做比较。面试的时候除非
被要求用int,不然还是选择long long好。
w****x
发帖数: 2483
6
我使用unsigned int处理INT_MIN绝对值比INT_MAX大的情况,
对于overflow, 我分两步, 一是判断unsigned int有没有overflow,
一个是判断unsigned int没有overflow的情况下int有没有overflow
class CException
{
public:
CException(const char* str) : m_strErr(str) {}
string GetErrInfo() { return m_strErr; }
private:
std::string m_strErr;
};
int myatoi(const char* p)
{
assert(p);
//Judge signal
bool bNeg = false;
const char* pCur = p;
if(*pCur == '-' || *pCur == '+')
{
if (*pCur++ == '-')
bNeg = true;
}
//treat the rest as positive numeric number, so for negative number
//it is 2^31, and for positive number it is 2^31
unsigned int uLimit = bNeg ? 0x7FFFFFFF + 1 : 0x7FFFFFFF;
// The reson to use unsigned int rather than int is to deal with 2^31
unsigned int uRes = 0;
unsigned int uDummy = UINT_MAX/10; // used to judge overflow of unsigned
int
do
{
//Deals with illegal character
if (*pCur < '0' || *pCur > '9')
throw CException("Illegal character found");
//Get current digit
int nDigit = *pCur - '0';
//Detect the unsigned integer overflow
if (uRes > uDummy || (uRes == uDummy && nDigit > (UINT_MAX - (UINT_
MAX/10)*10)))
throw CException("Over flow detected");
//update uRes
uRes = uRes*10 + nDigit;
//Overflow situation if uRes isn't overflow
if (uRes > uLimit)
throw CException("Over flow detected");
pCur++;
}while (*pCur != 0); // WTF stupid error again, do while not while you
mother fucker!!!
return bNeg ? (int)(-uRes) : (int)(uRes);
}
w****x
发帖数: 2483
7
long long 处理overflow
int myatoi(const char* p)
{
assert(p);
//Judge signal
bool bNeg = false;
const char* pCur = p;
if(*pCur == '-' || *pCur == '+')
{
if (*pCur++ == '-')
bNeg = true;
}
long long dummy = INT_MIN;
//can't directly use -(INT_MIN) because of precompile treat -(INT_MIN)
//as 32 bit integer which will cause overflow
long long limit = bNeg ? -(dummy) : INT_MAX;
long long res = 0;
do
{
//Deals with illegal character
if (*pCur < '0' || *pCur > '9')
throw CException("Illegal character found");
//Get current digit
int nDigit = *pCur - '0';
//update uRes
res = res*10 + nDigit;
//Overflow situation if uRes isn't overflow
if (res > limit)
throw CException("Over flow detected");
pCur++;
}while (*pCur != 0); // WTF stupid error again, do while not while you
mother fucker!!!
return bNeg ? (int)(-res) : (int)(res);
}
t**********h
发帖数: 2273
8
北京,java里搞这题,long了后,还要考虑溢出么?那得多大的数阿,

long在java里是64位的。你可以改成 if(res
★ Sent from iPhone App: iReader Mitbbs Lite 7.56

【在 p*****2 的大作中提到】
:
: long在java里是64位的。你可以改成 if(res< new res)

r*****e
发帖数: 792
9
一个小建议,*10的时候改成 n<<3+n<<1
另,刚注意到你的注释,对你这种自我批评的精神致敬。
我自己的注释会比较含蓄,以鼓励和表扬为主,呵呵

【在 w****x 的大作中提到】
: 我使用unsigned int处理INT_MIN绝对值比INT_MAX大的情况,
: 对于overflow, 我分两步, 一是判断unsigned int有没有overflow,
: 一个是判断unsigned int没有overflow的情况下int有没有overflow
: class CException
: {
: public:
: CException(const char* str) : m_strErr(str) {}
: string GetErrInfo() { return m_strErr; }
: private:
: std::string m_strErr;

l*********8
发帖数: 4642
10
that's because it's faster or it's easier to handle overflow?

【在 r*****e 的大作中提到】
: 一个小建议,*10的时候改成 n<<3+n<<1
: 另,刚注意到你的注释,对你这种自我批评的精神致敬。
: 我自己的注释会比较含蓄,以鼓励和表扬为主,呵呵

r*****e
发帖数: 792
11
faster and it's a common practice

【在 l*********8 的大作中提到】
: that's because it's faster or it's easier to handle overflow?
l*********8
发帖数: 4642
12
I see. thanks!

【在 r*****e 的大作中提到】
: faster and it's a common practice
p*****2
发帖数: 21240
13

long溢出也不是不可能的。但是这题应该跟面试官交流好。问用long解决overflow行不
行。比赛的时候有时候long也会溢出。

【在 t**********h 的大作中提到】
: 北京,java里搞这题,long了后,还要考虑溢出么?那得多大的数阿,
:
: long在java里是64位的。你可以改成 if(res
: ★ Sent from iPhone App: iReader Mitbbs Lite 7.56

1 (共1页)
进入JobHunting版参与讨论
相关主题
弱弱的问一个问题经典题atoi的溢出处理
150上的11.3,用1GByte的memory找出4B整数中的missing one写了个atoi,大家帮看有没有哪里错了?
请教一个题: Median of Two Sorted Arrays关于atoi的overflow
reverse an integer 怎么判断是否 overflow 来着问一个atoi overflow的问题
函数atoi的实现问个越界的问题
帮忙看看我写的atoi有没有bug, 谢谢问两道bloomberg的题目
atoi的溢出处理的想法atoi很不好写,头都大了...
onsite完,攒rp系列(二)请问如何安全地reverse 一个integer
相关话题的讨论汇总
话题: int话题: pcur话题: overflow话题: res话题: ures