008String to Integer (atoi) (C)

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.

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.

做这道题也是醉了,在没有看requirements for atoi时, 除了忘记空格的判断,确实都想到了,不过在leetcode运行时却大跌眼镜

int myAtoi(char* str) {
    int a[10],k=0,i=0,flag=0,flag1=0,num=0;
    char *s;
    if(str[0]=='\0'){
    	return 0;
    }
   while(str[i]==' '){
   	i++;
   } 
	 if(str[i]=='-'){
    	flag=1;
    	i++;
    }else if(str[i]=='+'){
        i++;
    }
    while(str[i]=='0'){
    	i++;
    }
    s=&str[i];
    while(str[i]!='\0'&&flag1==0){
    	if(str[i]>='0'&&str[i]<='9'){
    		a[k]=str[i]-'0';
    		i++;
        	k++;
    	}else{
    		flag1=1;
    	}
    	
    	if(k==10&&str[i]>='0'&&str[i]<='9'){
    		if(flag==1){
    		    return -2147483648;
    		}else{
    		    return 2147483647;
    		}
    	}
    }
    if(k==10){
    	if(flag==0){
    		if(strcmp(s,"2147483647")>0){
    			return 2147483647;
    		}
    	}else{
    		if(strcmp(s,"2147483648")>0){
    			return 2147483648;
    		}
    	}
    }
	i=0;
    while(i<k){
    	if(flag==1){
    		a[i]=-a[i];
    	}
    	num+=a[i];
		i++;
		if(i!=k){
			num*=10;
		}
    }
    return num;
}
附上写此程序的过程

#include<stdio.h>
#include<String.h>
/*
  -2147483648~2147483647 
  假设对于负数形式的字符串,打头应为"-.....",("-000123")对于形如"0000-123"报错 
  看完提示忘记了对空格的处理
  第一次运行:竟然会输入这样的字符串:"+1"  没有考虑 
  第二次运行:真是晕了 ,对于字符串:"-+1"  还非得加个else 
  第三次运行:字符串:"-0012a42"  对于这样的字符串我自己判0,给出的答案竟然是-12;看了我得再设标志位flag1 
  第四次运行:少看了一句话,对于out of the range of representable values ,返回 -2147483648或2147483647,而我均返回0
  注意:
     while(str[i++]==' ');
	 和
	 while(str[i]==' '){i++;}
	 是有区别的,除非当第一个判断的i有str[i]=' '时,两个语句才相等 
*/
int myAtoi(char* str) {
    int a[10],k=0,i=0,flag=0,flag1=0,num=0;
    char *s;
    if(str[0]=='\0'){
    	return 0;
    }
   while(str[i]==' '){
   	i++;
   } 
	 if(str[i]=='-'){
    	flag=1;
    	i++;
    }else if(str[i]=='+'){
    	i++;
    } 
    while(str[i]=='0'){
    	i++;
    }
    s=&str[i];
    while(str[i]!='\0'&&flag1==0){
    	if(str[i]>='0'&&str[i]<='9'){
    		a[k]=str[i]-'0';
    		i++;
        	k++;
    	}else{
		    flag1=1;
//    		return 0;
    	}
    	
    	if(k==10&&str[i]>='0'&&str[i]<='9'){
    		if(flag==1){
    			return -2147483648;
    		}else{
    			return 2147483647;
    		}
    	}
    }
    if(k==10){
    	if(flag==0){
    		if(strcmp(s,"2147483647")>0){
    			return 2147483647;
    		}
    	}else{
    		if(strcmp(s,"2147483648")>0){
    			return -2147483648;
    		}
    	}
    }
	i=0;
    while(i<k){
    	if(flag==1){
    		a[i]=-a[i];
    	}
    	num+=a[i];
		i++;
		if(i!=k){
			num*=10;
		}
    }
    return num;
}
int main()
{
	char s[100];
	gets(s);
	printf("%d\n",myAtoi(s));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值