avatar
atoi overflow怎么办?# JobHunting - 待字闺中
p*7
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<p++;
}
return res*flag;
}
请问这个代码怎么改才能支持overflow比较好?
avatar
l*8
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';

avatar
n*n
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';

avatar
p*2
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';

avatar
h*e
5
C/C++用long long会简化很多。只用int也可以解,但是非常繁琐,因为
你要和-(2^31)/10、(2^31-1)/10之类的边界值做比较。面试的时候除非
被要求用int,不然还是选择long long好。
avatar
w*x
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);
}
avatar
w*x
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);
}
avatar
t*h
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)

avatar
r*e
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;

avatar
l*8
10
that's because it's faster or it's easier to handle overflow?

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

avatar
r*e
11
faster and it's a common practice

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

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

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

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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。