1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. int my_atoi(char *str)
  6. {
  7. if(NULL == str)
  8. {
  9. printf("argc is null\n");
  10. return -;
  11. }
  12. while(' ' == *str)
  13. str++;
  14. int flag = ;
  15. if('-' == *str)
  16. {
  17. flag = -;
  18. str++;
  19. }
  20. if('+' == *str)
  21. {
  22. str++;
  23. }
  24. unsigned int sum = ;
  25. while(*str)
  26. {
  27. if(*str >= '' && *str <= '')
  28. {
  29. int tmp = *str - '';
  30. sum *= ;
  31. sum += tmp;
  32. //此处判断溢出的目的是避免sum在计算过程中溢出
  33. if(( == flag) && (sum > INT_MAX))
  34. {
  35. printf("int 正数溢出\n");
  36. return -;
  37. }
  38. //计算最小int的绝对值,注意要使用unsigned int否则会溢出
  39. unsigned int absIntMin = (unsigned int)INT_MAX + ;
  40. if((- == flag) && (sum > absIntMin))
  41. {
  42. printf("int 负数溢出\n");
  43. return -;
  44. }
  45. }
  46. else
  47. {
  48. printf("str (%s) exists illegal character\n", str);
  49. return -;
  50. }
  51. str++;
  52. }
  53. return flag * sum;
  54. }

测试代码

  1. #include <float.h>
  2. #include <limits.h>
  3. extern int my_atoi(char *str);
  4. int main(int argc, char *argv[])
  5. {
  6. char a[] = {'\0'};
  7. while()
  8. {
  9. scanf("%s", a);
  10. printf("count = %d\n", my_atoi(a));
  11. }
  12. return ;
  13. }

原创:实现atoi函数的更多相关文章

  1. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

  2. C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...

  3. 题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用

    #include<stdio.h> #include<stdlib.h> int sw(char *a){ ,c=; while(a[i]){ ') c=c*+a[i]-'; ...

  4. atoi()函数的实现

    atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...

  5. C语言itoa()函数和atoi()函数详解(整数转字符)

    http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...

  6. C语言itoa函数和atoi 函数

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h>  ...

  7. 【C语言】模拟实现atoi函数

    atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  9. atoi()函数(转载)

    atoi()函数 原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...

  10. 源码实现 --> atoi函数实现

    atoi函数实现 atoi()函数的功能是将一个字符串转换为一个整型数值. 例如“12345”,转换之后的数值为12345,“-0123”转换之后为-123. #include <stdio.h ...

随机推荐

  1. PDO原生分页

    ** PDO分页** 1.PDO连接数据库 $dbh=new PDO('mysql:host=127.0.0.1;dbname=03a','root','root');//使用pdo 2.接收页码 $ ...

  2. webSocket协议和Socket.IO

    一.Http无法轻松实现实时应用: ● HTTP协议是无状态的,服务器只会响应来自客户端的请求,但是它与客户端之间不具备持续连接. ● 我们可以非常轻松的捕获浏览器上发生的事件(比如用户点击了盒子), ...

  3. css;js学习(一)

    推荐基础前端学习地址https://ke.qq.com/course/315961蝉壳学院 清除浮动 .clearfix:before,.clearfix:after{ content: " ...

  4. 你不知道的css各类布局(四)之响应式布局

    响应式布局 概念 响应式布局指的是同一页面在不同屏幕尺寸下有不同的布局 布局特点 响应式设计的目标是确保一个页面在所有终端上(各种尺寸的PC.手机.手表.冰箱的Web浏览器等等)都能显示出令人满意的效 ...

  5. JS基础_一元运算符

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. SpringCloud 配置文件 application.yml和 bootstrap.yml区别

    前言: SpringBoot默认支持properties和YAML两种格式的配置文件.前者格式简单,但是只支持键值对.如果需要表达列表,最好使用YAML格式.SpringBoot支持自动加载约定名称的 ...

  7. webpack升级4出现的问题

    webpack3升级到4出现了很多问题,经过验证报错信息如下 1 Module parse failed: Unexpected token (:) You may need an appropria ...

  8. JAVA语言程序设计课后习题----第二单元解析(仅供参考)

    1 注意不同类型转换 import java.util.Scanner; public class Ch02 { public static void main(String[] args) { Sc ...

  9. 命令行工具--curl

    目录 命令:curl 一.简介 二.使用案例 1.基本用法 2.保存访问的网页 3.测试网页返回值 4.指定proxy服务器以及其端口 5.cookie 6.模仿浏览器 7.伪造referer(盗链) ...

  10. OpenResty 执行流程阶段

    nginx有11个处理阶段,如下图所示: 指令 所处处理阶段 使用范围 解释 init_by_luainit_by_lua_file loading-config http nginx Master进 ...