C语言中经常用到字符串与数字之间的相互转换,常见的此类库函数有atof(字符串转换成浮点数)、atoi(字符串转换成整型数)、atol(字符串转换成长整形)、itoa(整型数转换成字符串)、ltoa(长整形转换为字符串)等。

  以下为自定义Myatoi()函数的实现以及测试代码。

 #include <stdio.h>
int Myatoi(char*str){
if (str == NULL){
printf("Invalid Input");
return -;
}
while (*str == ' '){
str++;
}
while ((*str == (char)0xA1) && (*(str + ) == (char)0xA1)){
str += ;
}
int nSign = (*str == '-') ? - : ;//确定符号位
if (*str == '+' || *str == '-'){
str++;
}
int nResult = ;
while (*str >= ''&& *str <= ''){
nResult = nResult * + (*str - '');
str++;
}
return nResult *nSign;
} int main(){
printf("%d\n", Myatoi(""));
return ;
}

最新文章

  1. BZOJ 1853 【Scoi2010】 幸运数字
  2. 安卓手机当Mac、Win和Linux外置WIFI网卡
  3. 纯C++文件调用MFC类
  4. Andorid 编程 系统环境安装
  5. setInterval小问题
  6. 用Unity实现的依赖注入
  7. 如何配置apache最大的并发数
  8. 【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!
  9. JAVA正则表达式之贪婪、勉强和侵占
  10. 修改Servlet模板
  11. C#总结(3)
  12. UVa 121 - Pipe Fitters
  13. 查询记录rs.previous()使用
  14. Python获取文件夹的名字
  15. chrome浏览器被reimage pair 劫持怎么处理
  16. django celery redis 定时任务
  17. Linux 创建虚拟机,配置网卡,桥接,连接XShell
  18. RescuePro Deluxe恢复软件SanDisk
  19. python hashable
  20. golang pprof 内存分析

热门文章

  1. python3-常用模块之random
  2. javascript执行上下文和变量对象
  3. windows API 第 11 篇 GetCurrentDirectory SetCurrentDirectory
  4. 廖雪峰Java9正则表达式-2正则表达式进阶-6搜索和替换
  5. Python快速搭建HTTP服务器
  6. css背景属性整理
  7. PAT甲级——A1010 Radix
  8. java 在线拆分 word文档采用什么技术比较好?
  9. java 实现文件内容的加密和解密
  10. 线程安全之suspend(挂起) 和resume(执行)