三沙网站设计公司,公司的网站建设费用怎么入账,wordpress5.1.1编辑器还原,张家界建设局网站电话号码文章目录 一. 什么是atoi函数二. 模拟实现函数atoi三. 总结atoi 一. 什么是atoi函数
atoi函数是将常量字符串转化为整数 int atoi(const char str);* 注意#xff1a;该函数的返回值为int#xff0c;所以不能超过int范围#xff0c;如果数字很大超过就会截断#xff0c;返… 文章目录 一. 什么是atoi函数二. 模拟实现函数atoi三. 总结atoi 一. 什么是atoi函数
atoi函数是将常量字符串转化为整数 int atoi(const char str);* 注意该函数的返回值为int所以不能超过int范围如果数字很大超过就会截断返回截断后的值即int的最大左右范围值。 举个例子
#include stdio.h
#include stdlib.h
int main()
{int ret1 atoi(-123);int ret2 atoi(11111111111111111111111);printf(%d\n, ret1);printf(%d\n, ret2);printf(%d\n, INT_MAX);
}结果表明超过int范围的值结果会截断截断后值和int最大值一样。
二. 模拟实现函数atoi
上面不理解没事看例子——》 例子一传一个空字符串 例子二:情况二、传一个空指针 例子三:传一个空格加整数的字符串 例子四传一个整数加字母的字符串 例子五传一个字母加整数字符串 例子6传一个负整数
#includestdio.h
#includestdlib.h
#includeassert.h
#includectype.hint main()
{printf(%d\n, atoi());printf(%d\n, atoi( 123));printf(%d\n, atoi(123sgsdg));printf(%d\n, atoi(adgd123));printf(%d\n, atoi(-123));return 0;
}例子一和四都是非法转换所以模拟实现要先处理这两种情况
#include stdio.h
#include ctype.h
#include limits.h
enum Status
{VALID,INVALID
};
enum Status status INVALID;
int my_atoi(const char* str)
{if (str NULL){return 0;}if (*str \0){return 0;}//空白字符while (isspace(*str)){str;}int flag 1;if (*str ){flag 1;str;}else if (*str -){flag -1;str;}//处理数字字符long long ret 0;while (isdigit(*str)){ret ret * 10 (*str - 0) * flag;if (retINT_MIN || retINT_MAX){return 0;}str;}if (*str \0){status VALID;return (int)ret;}else{return (int)ret;}
}
int main()
{int ret my_atoi(-123);if (status VALID)printf(合法转换%d\n, ret);elseprintf(非法转换%d\n, ret);return 0;
}三. 总结atoi 1、该函数首先会丢弃尽可能多的空白字符直到找到第一个非空白字符然后从这个字符开始取一个可选的初识加号或者减号后跟尽可能多的十进制数字并将他们返回一个int类型的数值。 2、若该字符串是在整数的字符后包含其他字符则这些字符将会被忽略返回其他字符之前的整数并且不会对该函数造成任何影响。 3、若该字符串中第一个非空字符序列表示有效的整数或是一个空指针或只包含空白字符则不执行任何转换并且返回零。 小弟制作不易劳烦各位兄弟点点小小的关注谢谢了 文章转载自: http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.krtky.cn.gov.cn.krtky.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.qieistand.com.gov.cn.qieistand.com http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.c-ae.cn.gov.cn.c-ae.cn http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.yghlr.cn.gov.cn.yghlr.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.wylpy.cn.gov.cn.wylpy.cn http://www.morning.nqwz.cn.gov.cn.nqwz.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.gxqpm.cn.gov.cn.gxqpm.cn http://www.morning.dlrsjc.com.gov.cn.dlrsjc.com http://www.morning.jkbqs.cn.gov.cn.jkbqs.cn http://www.morning.wjndl.cn.gov.cn.wjndl.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn