atoi()函数的实现
atoi()函数的功能:将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
atoi()函数实现的代码:
- /*
- * name:xif
- * coder:xifan@2010@yahoo.cn
- * time:08.20.2012
- * file_name:my_atoi.c
- * function:int my_atoi(char* pstr)
- */
- int my_atoi(char* pstr)
- {
- int Ret_Integer = 0;
- int Integer_sign = 1;
- /*
- * 判断指针是否为空
- */
- if(pstr == NULL)
- {
- printf("Pointer is NULL\n");
- return 0;
- }
- /*
- * 跳过前面的空格字符
- */
- while(isspace(*pstr) == 0)
- {
- pstr++;
- }
- /*
- * 判断正负号
- * 如果是正号,指针指向下一个字符
- * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符
- */
- if(*pstr == '-')
- {
- Integer_sign = -1;
- }
- if(*pstr == '-' || *pstr == '+')
- {
- pstr++;
- }
- /*
- * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer
- */
- while(*pstr >= '0' && *pstr <= '9')
- {
- Ret_Integer = Ret_Integer * 10 + *pstr - '0';
- pstr++;
- }
- Ret_Integer = Integer_sign * Ret_Integer;
- return Ret_Integer;
- }
现在贴出运行my_atoi()的结果,定义的主函数为:int main ()
- int main()
- {
- char a[] = "-100";
- char b[] = "456";
- int c = 0;
- int my_atoi(char*);
- c = atoi(a) + atoi(b);
- printf("atoi(a)=%d\n",atoi(a));
- printf("atoi(b)=%d\n",atoi(b));
- printf("c = %d\n",c);
- return 0;
- }
atoi()函数的实现的更多相关文章
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- 题目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]-'; ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- atoi()函数(转载)
atoi()函数 原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...
- 源码实现 --> atoi函数实现
atoi函数实现 atoi()函数的功能是将一个字符串转换为一个整型数值. 例如“12345”,转换之后的数值为12345,“-0123”转换之后为-123. #include <stdio.h ...
随机推荐
- URAL 1097 Square Country 2 离散化
一共才100个正方形,将所有正方形左下角和右上角的X坐标和Y坐标离散化,直接枚举新建公园的点的坐标即可. O(n^3)的时间复杂度. #include <cstdio> #include ...
- javascript 简繁转换
js 简繁转换 function copy(ob) { var obj=findObj(ob); if (obj) { obj.select();js=obj.createTextRange();js ...
- 运行javascript的方式
1.放在超链接中: <a href="javascript:alert('aaaa')" >Test</a> 2.直接加载 <script type= ...
- [原]最短路专题【基础篇】(updating...)
hud1548 a strange lift 最短路/bfs 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:一个奇怪的电梯,每层楼的 ...
- Hook入门
Hook入门 2014-07-24 基本概念 Windows消息机制 Hook(钩子) 运行机制 核心函数 C# hook示例 基本概念[1] Windows消息机制[5] Windows操作系统是建 ...
- JSON 之 SuperObject(4): 增、删、改
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- php如何控制用户对图片的访问 PHP禁止图片盗链(转载)
把images目录设置成不充许http访问(把图片目录的:读取.目录浏览 两个权限去掉). 用一个PHP文件,直接用file函数读取这个图片.在这个PHP文件里进行权限控制. apache环境中,在你 ...
- UVa 12100 (模拟) Printer Queue
用一个队列模拟,还有一个数组cnt记录9个优先级的任务的数量,每次找到当前最大优先级的任务然后出队,并及时更新cnt数组. #include <iostream> #include < ...
- 多线程-NSOperation中使用ASIHttpRequest注意事项
最近做的iPhone项目中有一如下功能: app在用户许可后将本地Photos的照片上传到服务器,期间用户可以做其他任何操作,等上传成功后弹出一个toast通知用户. 原先的代码结构是: 获取照片的操 ...
- web.xml 中的listener、 filter、servlet 加载顺序
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...