当前位置:网站首页>【C 练习】「atoi」模拟实现
【C 练习】「atoi」模拟实现
2022-07-21 04:08:00 【Domeecky】
atoi:
将字符串中的数字转为整型。
思路:
(1)查看是否有负号
(2)忽略字母、符号等字符
(3)忽略空格
(4)考虑溢出
实现:
int my_atoi(const char* s)
{
int flag = 0;
if (*s == '-')//判断正负
flag = 1;
int ret = 0;
while (*s)
{
if (0 < (*s - '0') && (*s - '0') < 10)//若该字符减'0'在0~10间,说明是数字
ret = ret * 10 + (*s - '0');//累加数字
if (((flag == 0) && (ret > INT_MAX)) || (flag == 1) && ((ret * -1) < INT_MIN))//判断是否超过最大值或最小值
return;
s++;
}
if (flag == 1)//判断正负
return (ret * -1);
return ret;
}
边栏推荐
- orangePi3 lts
- Lombok simplifies development
- Deep learning - (5) class of data imbalance_ weight
- The blue light and green light go out on time, and program the secret language of guru
- Codeworks 5 questions per day (average 1500) - day 21
- Penetration test - Application of file containing vulnerabilities and PHP pseudo protocol
- PWM输出实验
- Deep learning - (6) pytorch freezes the parameters of some layers
- SAP IDOC教程:定义,结构,类型,格式和表格-019
- 信号处理系统综合设计-最小阶数的IIR数字高通滤波器
猜你喜欢
动作活体检测能力,构建安全可靠的支付级“刷脸”体验
DOM event flow (event capture and event bubbling)
Résoudre l'erreur signalée: uncaught typeerror: impossible de lire les propriétés sous - jacentes (lire « installer»)
Tidb distributed batch solution
Preparation of dihydrotanshinone I loaded albumin nanoparticles / norcantharidin albumin nanoparticles / voriconazole albumin nanoparticles
C language file operation
C what are the output points of DSP core resample of digital signal processing toolkit
解决报错:Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)
Deep learning - (5) class of data imbalance_ weight
攻防世界----favorite_number
随机推荐
C language file operation
维密萎靡,曾经“性感”现在真的“凉了”!
Sql优化(九):分页语句优化
Software testing interview question: what is your biggest interest in testing? Why?
Reading notes: secondary flowers bloom
Verilog grammar basics HDL bits training 03
攻防世界----favorite_number
Software testing interview question: what is the difference between quotation and pointer?
Software test interview question: briefly describe what you have done in your previous work and what you are familiar with. The reference answers are as follows.
Find a single dog -- repeat the number twice to find a single number
攻防世界----lottery
OKR case of personnel department: create the best office environment for colleagues
orangePi3 lts
Matrix
Preparation of dihydrotanshinone I loaded albumin nanoparticles / norcantharidin albumin nanoparticles / voriconazole albumin nanoparticles
Okaleido tiger NFT is about to log in to binance NFT platform, and the era of NFT rights and interests is about to start
无线定位技术实验三 基于信号强度的位置指纹定位仿真
滲透測試-文件包含漏洞以及php偽協議的應用
Use the mogdb operator to deploy the mogdb cluster (mogdb stack) on kubernetes
Preparation of hemoglobin albumin nanoparticles encapsulated by Lumbrokinase albumin nanoparticles / erythrocyte membrane