- 论坛徽章:
- 0
|
我的实现,未必高效,但能用。- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- int myatoi(const char*str);
- int main()
- {
- unsigned int i = 0,j=0;
- char *s="123423432423424234";
- i = atoi(s);
- j = myatoi(s);
- printf("i is :%d,%d\n",i,j);
- return 0;
- }
- int myatoi(const char*str) {
- if(NULL == str || '\0' == *str) {
- return 0;
- }
- int ret=0,left_bit=sizeof(int)*8-1;
- unsigned int tmp=0;
- while(*str>='0' && *str <='9') {
- tmp = (*str-'0') + ret*10;
- if(tmp>>left_bit==1) {
- return INT_MAX;
- }
- ret = tmp;
- str++;
- }
- return ret;
- }
复制代码 |
|