当前位置:网站首页>【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
2022-07-29 05:09:00 【甘城なつき】
꧁ 各位大佬们好!很荣幸能够得到您的访问,让我们一起在编程道路上任重道远!꧂
* 博客专栏:【剑指offer】*
本篇内容简介:C语言库函数中的atoi函数详介以及模拟实现!
了解作者:励志成为一名编程大牛的学子,目前正在升大二的编程小白。
励志术语:编程道路的乏味,让我们一起学习变得有趣!

目录
前言
首先介绍一下,这是一道来着《剑指offer 》面试题。
atoi函数,是干什么的?我想这是很多人的疑问,我第一次见这个函数时,也是一脸懵b的,不过经过老师的详解以及自己的模拟实现,可以说把这个函数摸索了大概了,好,接下来给大家介绍atoi函数以及如何模拟实现atoi函数。
atoi函数功能介绍
我们来看库里面是什么介绍atoi函数的!

我们可以看到库函数atoi的功能是将一段字符串转换为整数。
![]()
而它用时所引用的头文件,是<stdlib.h>。
我们看一段代码,看它是如何将字符串转换成整数的。
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[20] = "123456";
int ret = atoi(arr);
printf("%d\n", ret);//以%d的形式打印
return 0;
}运行结果:

这就说明了它真的将字符串转换成了整数。
atoi函数处理特殊情况
传递空指针
程序就会挂掉,像这样!

空字符串
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[] = "";
int ret = atoi(arr);
printf("%d\n", ret);//以%d的形式打印
return 0;
}运行结果:

我们可以看到当转递空字符串时,它就返回一个0,我们现在先假设这个0是一个非法的0。
空格
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[] = " ";//返回0 假设这是一个合法的0
char arr_1[20] = " 123456";
int ret = atoi(arr);
int ret_1 = atoi(arr_1);
printf("%d\n", ret);//以%d的形式打印
printf("%d\n", ret_1);
return 0;
}运行结果:

我们可以看到有两种情况,一种是纯转递空格,它返回的也是0,假设这是个合法的0,当字符串前面有空格时,它会跳过空格,直接将后面的字符串转换为整型。
字符串 “ + ”与" - "
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[] = "-12345";//返回0 假设这是一个合法的0
int ret = atoi(arr);
printf("%d\n", ret);//以%d的形式打印
return 0;
}运行结果:

我们可以看到它会将字符串“+ -”的内容一起转换为整数打印出来。
越界访问
所谓越界访问就是给定一个很大的字符串,这个字符串转换时,超过了 C语言规定的 INT_MAX值或者INT_MIN的值时,编译器会怎么做!
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[] = "+1234511111111111111";//返回很大一个数
int ret = atoi(arr);
printf("%d\n", ret);//以%d的形式打印
return 0;
}运行结果:

可以看到,当越界时,它转换的是一个很大的数,我们在模拟实现时,可以将它转换为非法的0。
非数字字符串
#include<stdio.h>
#include<stdlib.h>
int main()
{
char arr[] = "-1234abc";
char arr_1[20] = "abcd";//返回0 假设这是一个合法的0
int ret = atoi(arr);
int ret_1 = atoi(arr_1);
printf("%d\n", ret);//以%d的形式打印
printf("%d\n", ret_1);
return 0;
}运行结果:

我们可以看到分为两种情况,当全是非数字字符串时,假设返回的是一个合法的0,但有其他数字字符串时,会直接不打印非数字字符串。
模拟实现atoi函数
实现转换成非法0与合法0
我们可以枚举常量出来,表示当前的状态,默认转换是非法的0。
enum State
{
VAILD,
INVAILD
}Sta=INVAILD;//创造变量默认为非法
空指针
可以加上assert()函数断言,预防是空指针。
assert(str);
空字符串
若遇到空字符串(即 ‘\0’ 时),我们直接返回0,此时的0是一个非法的0。
if (*str == '\0')
{
return 0;
}
空格
判断空格我们可以用isspace()这个函数,若是空格返回1,直接跳过空格。(需要引用头文件 #include<ctype.h>)
while (isspace(*str))
{
str++;
}
字符串" + "与" - "
我们可以定义一个flag记录当前遇到的是哪个字符串,再跳过字符串" + "或者" - "。
int flag = 1;
if (*str == '+')
{
flag = 1;
str++;
}
else if (*str == '-')
{
flag = -1;
str++;
}
非数字字符
可以用isdigit来判断,是数字字符,返回1,进入代码继续执行,否则直接return ret;(ret为返回值)。
if (isdigit(*str))
{
ret = ret * 10 + flag * (*str - '0');//减去字符0,才是数字0
}
越界访问
可以看得到的这个字符串转换为整数后,是否大于INT_MAX或者小于INT_MIN,是返回非法0。
long long ret = 0;
while (*str != '\0')
{
if (isdigit(*str))
{
ret = ret * 10 + flag * (*str - '0');//减去字符0,才是数字0
if (ret > INT_MAX || ret < INT_MIN)
{
return 0;
}
}
else
{
return (int)ret;//强制类型转化为int(函数的返回值是int)
}
str++;
}
实现atoi函数整个源代码
#include<stdio.h>
#include<assert.h>
#include<ctype.h>
#include<stdlib.h>
enum State
{
VAILD,
INVAILD
}Sta=INVAILD;//创造变量默认为非法
int my_atoi(const char* str)
{
assert(str);
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 (*str != '\0')
{
if (isdigit(*str))
{
ret = ret * 10 + flag * (*str - '0');//减去字符0,才是数字0
if (ret > INT_MAX || ret < INT_MIN)
{
return 0;
}
}
else
{
return (int)ret;//强制类型转化为int(函数的返回值是int)
}
str++;
}
if (*str == '\0')
{
Sta = VAILD; //正常转换完了,到末尾的 \0
}
return (int)ret;
}
int main()
{
char arr[20] = "-1234";
int ret = my_atoi(arr);
if (Sta == VAILD)
{
printf("合法转换:%d\n", ret);
}
else if (Sta == INVAILD)
{
printf("非法转换:%d\n", ret);
}
return 0;
}结束语
模拟实现atoi函数就给大家介绍以及模拟实现完了,觉得学到一点的话,可以给博主三联哦!!!
边栏推荐
猜你喜欢

The road to success in R & D efficiency of 1000 person Internet companies

6.3 references

MySQL的基础概念+数据库系统结构+拓展延申+基础命令学习

容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)

Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?

哈夫曼树以及哈夫曼编码在文件压缩上的应用

365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions

365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
![[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start](/img/6e/6b4deeedbfd9d6baa651019f3dabfa.jpg)
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start

英伟达周锡健:设计到数字营销的最后一公里
随机推荐
指针
Xiaolu Inn - Trailer
携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
The latest tank battle 2022 full development notes-1
终端shell常用命令
CryEngine技术
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
OCCT学习001-----简介
Getting started with arfoundation tutorial 10- plane detection and placement
WDDM学习
365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
Complete ecological map of R & D Efficiency & selection of Devops tools
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
Vs code的安装步骤及环境配置
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
小鲁客栈---预告篇
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录