Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
spoilers alert... click to show requirements for atoi.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
class Solution {
public:
int atoi(const char *str) {
if (str==NULL){
return 0;
}
while (*str==' '){
++str;
}
//char positiveNum[]="2147483647";
//char negativeNum[]="-2147483648";
int flag=1;
int num=0;
int len = 9;
if ( *str=='+' ){
++str;
}
if ( *str=='-' ){
flag = -1;
++str;
}
//以0开头非法,除非只有一个0,注意-0和+0都是合法的.
//if ( *str=='0' && *++str){
// globalflag = 0;
// return 0;
// }
while (*str=='0'){
++str;
}
//将num控制在9位数以内(包括9位数)
while ( len && *str && *str<='9' && *str>='0' ){
num = num*10 + *str-'0';
++str;
--len;
}
const char *temp = str;
while (*temp && *temp && *temp<='9' && *temp>='0'){
temp++;
--len;
}
if ( !*str ||*str<'0' || *str>'9'){
return num*flag;
}
if (flag==1 && ( len <-1 ||
( num==214748364 && *str>'7') || num>214748364 ) ){
return 2147483647;
}
if (flag==-1 && ( len <-1 ||
( num==214748364 && *str>'8') || num>214748364) ){
return -2147483648;
}
return num*flag*10+(*str-'0')*flag;
}
public:
static int globalflag;
};
int Solution::globalflag=1;
/
//
//
常规atoi函数:
#include <iostream>
using namespace std;
static const int INTOVERFLOW =0x01;
static const int INTUNDERFLOW=0x02;
static const int CANTRESERVER=0x04;
static const int BEGINWITH0 =0x08;
class Solution {
public:
int atoi(const char *str) {
//char positiveNum[]="2147483647";
//char negativeNum[]="-2147483648";
int flag=1;
int num=0;
int len = 9;
if ( *str=='+' ){
++str;
}
if ( *str=='-' ){
flag = -1;
++str;
}
//以0开头非法,除非只有一个0,注意-0和+0都是合法的.
if ( *str=='0' && *++str){
globalflag = 0;
return 0;
}
//将num控制在9位数以内(包括9位数)
while ( len && *str && *str<='9' && *str>='0' ){
num = num*10 + *str-'0';
++str;
--len;
}
if ( !*str){
return num*flag;
}
if ( *str<'0' || *str>'9' ){
globalflag = 0;
return 0;
}
if ( num>214748364 || (num==214748364 && flag==1 && *str>'7')
|| (num==214748364 && flag==-1 && *str>'8')){
globalflag = 0;
return 0;
}
if ( !*(str+1) ){
return num*flag*10+(*str-'0')*flag;
}
return 0;
}
public:
static int globalflag;
};
int Solution::globalflag=1;
int main()
{
//char positiveNum[]="2147483647";
//char negativeNum[]="-2147483648";
Solution s;
//cout<<s.atoi("2147483647")<<endl;
cout<<s.atoi("214748364798");
}