博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
atoi()函数的实现
阅读量:7175 次
发布时间:2019-06-29

本文共 1159 字,大约阅读时间需要 3 分钟。

   

    上一篇博客讲的是atoi()函数的功能及举例,如今呢,就自己写写代码(依据atoi()的功能)来表示atoi()函数的实现。我在这里先把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;}
    执行结果:

转载地址:http://lzbzm.baihongyu.com/

你可能感兴趣的文章
(转载)Http 请求处理流程
查看>>
GetVersion和GetVersionEx
查看>>
软工实践第一次作业
查看>>
php采集利器snoopy应用技巧
查看>>
java事件处理机制(自定义事件)j
查看>>
字符串反转
查看>>
我的友情链接
查看>>
Mysq-MMM读写分离实操(简单易懂版)
查看>>
博文索引
查看>>
修改mysql用户密码
查看>>
Postgresql PostGIS使用总结
查看>>
Django中间件
查看>>
活动目录服务的配置与管理(7) 利用组策略实现文件夹重定向
查看>>
删除除了匹配到的所有文件以及文件夹
查看>>
Linux中安装GRUB的两种方式
查看>>
iptables 详解
查看>>
Rancher中的服务升级实验
查看>>
数据结构-------顺序表的实现
查看>>
供应虚拟机没有usb端口和无法识别映射usb解决方案
查看>>
sed的用法
查看>>