atoi函数的实现
来源:互联网 发布:淘宝管控记录会降权吗 编辑:程序博客网 时间:2024/04/29 06:02
请实现函数int atoi(const char *nptr))
函数说明: atoi会扫描参数nptr字符串,跳过前面的空格字符,遇到数字或正负符号开始做转换,直至遇到非数字或字符串结束时('\0')结束转换,并将结果返回。
不同人能够写出不同水平的代码,是考查coding能力的经典问题。
函数实现:
int atoi(const char* nptr){int result = 0;if(nptr == NULL){return result;}//while(nptr==" ")while(*nptr==' '){nptr++; }bool flag = false;if(*nptr=='-'){flag = true;nptr++;}else{if(*nptr=='+'){nptr++;}}while(nptr != '\0'){int temp = *nptr - '0';if(temp < 0 || temp > 9){break;}else{result = result * 10 + temp;}nptr++;}if(flag){result = 0 - result;}return result;}
测试用例:
void main(){int num0 = atoi("+123455");printf("num0=%d\n", num0);int num1 = atoi("-123455");printf("num1=%d\n", num1);int num2 = atoi(" 123455z");printf("num2=%d\n", num2);int num3 = atoi("-123455.5");printf("num3=%d\n", num3);}
0 0
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- android 中apk如何防止反编译?
- Eclipse在插件开发中对话框FileDialog、DirectoryDialog、ContainerSelectionDialog的整理
- CString与LPCWSTR/LPSTR/LPWSTR/char*等类型的转换
- 钓鱼岛是中国的领土居然有分歧
- Android应用层修改LED指示灯
- atoi函数的实现
- HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.***_jsp
- SmartUpload
- 动态生成 DataGrid列
- Laravel五大功能之Eloquent关系模式
- Java数据结构(排序篇)
- 大数据布景下轿车推广走向何方?
- 等待和煦的风,吹醒幸福
- linux下如何使用和安装mysql