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

atoi()函数实现的代码:

  1. /*
  2. * name:xif
  3. * coder:xifan@2010@yahoo.cn
  4. * time:08.20.2012
  5. * file_name:my_atoi.c
  6. * function:int my_atoi(char* pstr)
  7. */
  8. int my_atoi(char* pstr)
  9. {
  10. int Ret_Integer = 0;
  11. int Integer_sign = 1;
  12. /*
  13. * 判断指针是否为空
  14. */
  15. if(pstr == NULL)
  16. {
  17. printf("Pointer is NULL\n");
  18. return 0;
  19. }
  20. /*
  21. * 跳过前面的空格字符
  22. */
  23. while(isspace(*pstr) == 0)
  24. {
  25. pstr++;
  26. }
  27. /*
  28. * 判断正负号
  29. * 如果是正号,指针指向下一个字符
  30. * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符
  31. */
  32. if(*pstr == '-')
  33. {
  34. Integer_sign = -1;
  35. }
  36. if(*pstr == '-' || *pstr == '+')
  37. {
  38. pstr++;
  39. }
  40. /*
  41. * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer
  42. */
  43. while(*pstr >= '0' && *pstr <= '9')
  44. {
  45. Ret_Integer = Ret_Integer * 10 + *pstr - '0';
  46. pstr++;
  47. }
  48. Ret_Integer = Integer_sign * Ret_Integer;
  49. return Ret_Integer;
  50. }

现在贴出运行my_atoi()的结果,定义的主函数为:int  main  ()

  1. int main()
  2. {
  3. char a[] = "-100";
  4. char b[] = "456";
  5. int c = 0;
  6. int my_atoi(char*);
  7. c = atoi(a) + atoi(b);
  8. printf("atoi(a)=%d\n",atoi(a));
  9. printf("atoi(b)=%d\n",atoi(b));
  10. printf("c = %d\n",c);
  11. return 0;
  12. }

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. C语言itoa()函数和atoi()函数详解(整数转字符)

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

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

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

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

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

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

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

  8. atoi()函数(转载)

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

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

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

随机推荐

  1. MongoDB小记

    mongodb的一个简单使用. package com.chuntent.mongo; import java.util.Map; import java.util.Map.Entry; import ...

  2. [CCF2015.12]题解

    201512-1 数位之和 水题一个,取模除以10胡搞即可(不知道字符串为什么不行 #include <algorithm> #include <iostream> #incl ...

  3. LBS由ip查经纬度

    LBS API: https://api.map.baidu.com/highacciploc/v1?qcip=223.104.5.201&qterm=pc&ak=NLwCqrDce4 ...

  4. HDU 1869 六度分离【floyd】

    题意:给出n个人,m个关系,问是否满足任意两个人之间的距离通过6个人就可以连接 用floyd就可以了,注意距离是大于7 #include<iostream> #include<cst ...

  5. jQuery小例子

    map遍历数组 //=========for循环遍历========== var arr[1,2,3,4,5]; for(var i=0;i<=arr.length;i++) { arr[i]= ...

  6. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

  7. Windows Tftpd32 DHCP服务器 使用

    /********************************************************************* * Windows Tftpd32 DHCP服务器 使用 ...

  8. 决策树之 CART

    继上篇文章决策树之 ID3 与 C4.5,本文继续讨论另一种二分决策树 Classification And Regression Tree,CART 是 Breiman 等人在 1984 年提出的, ...

  9. (一) 从零开始搭建Spark Standalone集群环境搭建

    本文主要讲解spark 环境的搭建 主机配置  4核8线程,主频3.4G,16G内存 虚拟环境: VMWare 虚拟环境系统:Ubuntu 14.10 虚拟机运行环境: jdk-1.7.0_79(64 ...

  10. 剑指offer-第三章高质量的代码(输出该链表中倒数第K个节点)

    题目:输入一个链表,输出这个链表中倒数第K个节点.(代码的鲁棒性) 思路:用两个指针p1和p2,都指向头节点,开始的时候,p2不动,p1移动k-1次,指向第k个节点.此时,如果p1->next! ...