准完美的atoi函数实现
来源:互联网 发布:教然后知困读音 编辑:程序博客网 时间:2024/05/17 05:52
#include <limits.h> #include <ctype.h> #include <stdlib.h> #include <stdio.h> #include <string.h>int Danny_Atoi(char *SrcString){ char *IterIndex = SrcString; int RetVal = 0; int SignedNegative = 1; int int_bound = INT_MAX /10; int Len= strlen(SrcString); while ( isspace( *IterIndex ) ) { IterIndex ++; printf("NULL SPACE...Continue....\n"); continue; } if ( '-' == *IterIndex ) SignedNegative = -1; int i =0; while( !isdigit (*IterIndex) && int_bound && i<Len ) { printf("Skip a non-digit: %c...\n", *IterIndex); IterIndex ++; i ++; } while ( isdigit( *IterIndex ) && int_bound ) { printf("A digit Found:%c...\n", *IterIndex); RetVal = RetVal*10 + (*IterIndex - '0'); int_bound/=10; IterIndex ++; } if ( -1 == SignedNegative ) { printf("The String you input is negative........\n"); RetVal*= -1; } if (!int_bound) { printf("OverFlow Number.....\n"); return -1; } return RetVal;}int main( int argc, char **argv ){ if (argc <= 1) { printf("Please Provide Your String that you want to convert....\n"); return 0; } int RetVal; RetVal = Danny_Atoi ( argv[1] ); if ( 0 == RetVal ) {printf("No Number found in the input string.....\n"); } else if ( -1 == RetVal ) { printf("IntGer OverFlow....\n"); } else { printf("Congratulations..Number is:%d..\n", RetVal); } return 0;}
编译:# gcc -g -o Atoi Atoi-implementation.c
异常运行结果:
# ./Atoi
Please Provide Your String that you want to convert....
正常运行结果:
# ./Atoi get123get
Skip a non-digit: g...
Skip a non-digit: e...
Skip a non-digit: t...
A digit Found:1...
A digit Found:2...
A digit Found:3...
Congratulations..Number is:123..
- 准完美的atoi函数实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- 常见文件扩展名和它们的说明
- Java how to program(4th)中的专业词汇ch07
- Android学习笔记进阶17之LinearGradient
- tomcat启用HTTPS的设置
- 算法实验之回溯法解决背包问题
- 准完美的atoi函数实现
- linux下的errno定义
- Java how to program(4th)中的专业词汇ch08
- Tarball介绍
- 判断一个点是否在多边形内部的问题
- SYS$BACKGROUND SYS$USERS
- 常用正则表达式
- eclipse 条件断点
- 衡量软件质量的指标