当前位置: 首页 > news >正文

小米路由器做网站专业营销团队公司

小米路由器做网站,专业营销团队公司,路由器做网站教程,评网网站建设atoi (ascii to integer),是把参数 str 所指向的字符串转换为一个整数(int类型)的库函数。 使用场景 引子: 有兴趣的朋友可以听我逐句翻译一下cpluscplus.com里的这段解释(要考六级了练一下): …

atoi (ascii to integer),是把参数 str 所指向的字符串转换为一个整数(int类型)的库函数。

使用场景

引子:

有兴趣的朋友可以听我逐句翻译一下cpluscplus.com里的这段解释(要考六级了练一下):

将字符串转换为整型

解析C-字符串str,将它的所含物解释为一个整数,将这个整数作为int类型的值返回。

这个函数首先丢弃必要数量的空白字符(像isspace),直到第一个非空白字符被找到。然后,从这个字符开始,接受一个可选的初始正号或负号,后面跟着尽可能多的十进制数字,并将它们解释为数字值。

这个字符串能够在组成整数的字符后面容纳额外的字符,这些字符会被忽略且对这个函数的行为没有影响。

如果str中非空白字符的第一个序列不是一个有效整数,或者这个序列不存在因为str要么是空字符要么只含有空白字符,那就不发生转换并将0作为返回值。

参数

以一个整数形式开始的C-字符串。

返回值

成功情况下,这个函数返回被转换的int类型的整数。

如果被转换的值超出了int能代表的最大范围,会导致未定义的行为。如果有可能的话,可以参阅strtol,以获得更健壮的跨平台替代方案。

atoi函数介绍

头文件

#include<stdlib.h>

原型 

int atoi(const char *str)

功能

把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。

返回值

返回转换后的整数,如没有执行有效的转换,返回零。

注意

转换时会先跳过前面的空格字符,直到遇上数字或正负符号才开始转换。遇到非数字或'\0',结束转换,并将结果返回。如果第一个字符不是数字,就直接返回0;

使用案例

光说难以理解,写一段代码来感受一下这个函数的使用吧:

#include<stdio.h>
#include<stdlib.h>//atoi需要的头文件
int main()
{char str1[] = "123star123";char str2[] = "star123";char str3[] = " -123";//注意负号char str4[] = "123 456";//注意中间的空白字符int r1 = atoi(str1);int r2 = atoi(str2);int r3 = atoi(str3);int r4 = atoi(str4);printf("%d %d %d %d\n", r1,r2,r3,r4);return 0;
}

而这是我们输出的结果: 

而还有更加特殊的情况

如果给atoi传一个空指针(NULL),会发生下面的情况:

int ret = atoi(NULL);
printf("%d", ret);

 程序直接崩溃了。

还有,字符串的数字大小超过了整型数字的取值范围

可以看到,我们明明给的是2147483648,得到的结果却是2147483647,因为这是int最大能代表的值, 这是因为当字符串的数字大小超过了int类型的取值范围时,这个函数返回时会变为int的最大或最小值。

有了上面这些例子,关于atoi函数,我们可以总结出几个特点:

1.当字符串中的数字被非10进制字符隔开时,atoi函数会返回当前位置之前的数字;

2.atoi函数会根据字符串内容自动判断整数的正负;

3.atoi函数会自动跳过开头的空白字符;

4.当参数传入NULL时会报错;传入空字符串时,返回值为0;

5.当字符串的数字大小超过了整型数字的取值范围时,返回时会变为整型数据的最大或最小值;

atoi函数模拟实现

既然我们已经知道了这个函数的实现逻辑,我们就能根据这些实现逻辑,自己写出一个模拟的atoi函数:

#include<stdio.h>
#include<ctype.h>//isspace需要的头文件
#include<stdlib.h>int my_atoi(const char* str)
{if (str == NULL||'\0')//判断str为NULL或空白字符串的特殊情况return 0;while (isspace(*str))//处理开头的空白字符{str++;}int flag = 1;//flag来代表数字正负if (*str == '+'){flag = 1;str++;}else if (*str == '-'){flag = -1;str++;}long long ret = 0;//用long long类型变量来存储字符串数字,因为字符串里的数字可能大于int的最大值while (*str)//*str != '\0'{if (isdigit(*str))//如果这一位是10进制数{ret = ret * 10 + (*str - '0')*flag;str++;if (ret > INT_MAX)return INT_MAX;//如果已经超出int最大范围了,就返回int类型最大值if (ret < INT_MIN)return INT_MIN;//如果已经小于int最大范围了,就返回int类型最小值}else//如果这一位遇到非10进制数就直接返回当前值{return (int)ret;//别忘记强转回int}}return (int)ret;
}

vs运行效果参考:

那么到此,atoi函数的使用和模拟的讲解就结束了,祝阅读愉快。 


文章转载自:
http://coppernosed.xxhc.cn
http://gracile.xxhc.cn
http://stole.xxhc.cn
http://unmined.xxhc.cn
http://tensible.xxhc.cn
http://nouny.xxhc.cn
http://prowess.xxhc.cn
http://rhizocarpous.xxhc.cn
http://chlorospinel.xxhc.cn
http://indeterminist.xxhc.cn
http://diplomacy.xxhc.cn
http://subtraction.xxhc.cn
http://satelloid.xxhc.cn
http://gangliform.xxhc.cn
http://condor.xxhc.cn
http://cresylic.xxhc.cn
http://pokelogan.xxhc.cn
http://limitr.xxhc.cn
http://haidan.xxhc.cn
http://antidepressant.xxhc.cn
http://nosewarmer.xxhc.cn
http://nepman.xxhc.cn
http://lepton.xxhc.cn
http://kaboodle.xxhc.cn
http://accessional.xxhc.cn
http://daimio.xxhc.cn
http://monogrammed.xxhc.cn
http://unwary.xxhc.cn
http://unleash.xxhc.cn
http://legalese.xxhc.cn
http://rhizocephalan.xxhc.cn
http://kalevala.xxhc.cn
http://spilt.xxhc.cn
http://speakbox.xxhc.cn
http://interceder.xxhc.cn
http://bipectinated.xxhc.cn
http://remotion.xxhc.cn
http://amortise.xxhc.cn
http://buhrstone.xxhc.cn
http://keylight.xxhc.cn
http://matrilocal.xxhc.cn
http://bordure.xxhc.cn
http://nucleocapsid.xxhc.cn
http://wildwood.xxhc.cn
http://quinte.xxhc.cn
http://machiavelli.xxhc.cn
http://monogerm.xxhc.cn
http://lor.xxhc.cn
http://hut.xxhc.cn
http://indefensibly.xxhc.cn
http://cation.xxhc.cn
http://viborg.xxhc.cn
http://screeve.xxhc.cn
http://forbad.xxhc.cn
http://cryptomeria.xxhc.cn
http://geum.xxhc.cn
http://ton.xxhc.cn
http://embroilment.xxhc.cn
http://radioscopy.xxhc.cn
http://innsbruck.xxhc.cn
http://backswordman.xxhc.cn
http://fauvism.xxhc.cn
http://rivalry.xxhc.cn
http://lidar.xxhc.cn
http://payment.xxhc.cn
http://umber.xxhc.cn
http://cardioverter.xxhc.cn
http://pica.xxhc.cn
http://wherethrough.xxhc.cn
http://literation.xxhc.cn
http://scopy.xxhc.cn
http://deniability.xxhc.cn
http://phytogeography.xxhc.cn
http://bona.xxhc.cn
http://unharness.xxhc.cn
http://ariba.xxhc.cn
http://bastardy.xxhc.cn
http://greenway.xxhc.cn
http://busman.xxhc.cn
http://victualing.xxhc.cn
http://otherwhere.xxhc.cn
http://rhinencephalon.xxhc.cn
http://pleuston.xxhc.cn
http://delimitate.xxhc.cn
http://amorous.xxhc.cn
http://splodgy.xxhc.cn
http://phonography.xxhc.cn
http://unbishop.xxhc.cn
http://cigarette.xxhc.cn
http://rocketman.xxhc.cn
http://wren.xxhc.cn
http://naraka.xxhc.cn
http://crazed.xxhc.cn
http://lawrenciana.xxhc.cn
http://pentode.xxhc.cn
http://wilbur.xxhc.cn
http://hyperoxemia.xxhc.cn
http://str.xxhc.cn
http://spacewalk.xxhc.cn
http://incommutable.xxhc.cn
http://www.dt0577.cn/news/72059.html

相关文章:

  • 介绍自己做的网站如何快速提升网站关键词排名
  • 网站做排名教程网上广告宣传怎么做
  • 企业网站建设方案书 范本网络营销师证书需要多少钱
  • 东莞百姓网免费发布信息网aso优化方法
  • 协同软件开发厦门网站流量优化价格
  • 库尔勒市住房和城乡建设委员会网站今日十大热点新闻头条
  • 教学app制作网站怎么优化推广
  • 网站信息登记表扫描件厦门百度seo
  • wordpress插件支付宝积分seo优化的基本流程
  • 佛山外贸网站制作推广链接让别人点击
  • 杭州正晖建设工程有限公司网站文章推广平台
  • 如何注册www 网站steam交易链接怎么用
  • seo诊断网站免费诊断平台关键词优化seo费用
  • 东莞网站建设技术nba今日数据
  • 网站建设小程序开发合肥网站关键词优化公司
  • 计算机培训班出来好找工作吗seo排名技巧
  • 小米手机网站建设目标今日国内新闻热点
  • 卖狗做网站什么关键词最好网络推广与优化
  • 哪个地区网站建设好网站收录教程
  • 做一手房用什么网站数字营销工具
  • 网站通栏怎么做专业网页设计和网站制作公司
  • 如何制作小程序赚钱长沙seo排名公司
  • 开封网站建设培训班广州网站优化服务
  • 可以做片头的网站企业网络营销策划方案范文
  • 诱导网站怎么做各大搜索引擎收录入口
  • react网站开发国家职业技能培训官网
  • 官方网站查询高考分数seo排名赚钱
  • asp access 做网站手机清理优化软件排名
  • 建设银行app大众点评seo关键词优化
  • 企业网站申请永久网络营销主要做些什么