C标准库中atoi的一种可能的实现
2024-09-30 07:30:01
为避免与标准库中的atoi产生歧义,
我将自己编写的函数命名为strToInt,
以下是示例代码
#include <stdio.h>
int strToInt(const char *str);
int main()
{
int a = strToInt("-123.456");
int b = strToInt("+3.14");
int c = strToInt("123+123");
int d = strToInt("0001a2");
int e = strToInt("-+567");
printf("%d %d %d %d %d\n", a, b, c, d, e);
return 0;
}
int strToInt(const char *str)
{
int signedFlag = 1;
int sum = 0;
if (*str == '+')
{
++str;
}
else if (*str == '-')
{
signedFlag = -1;
++str;
}
while (*str >= '0' && *str <= '9')
{
sum = sum * 10 + *str++ - '0';
}
return signedFlag * sum;
}
最新文章
- java多线程监听JMS、MQ队列
- 把ResultSet对象转变成List对象
- dataZoom 详细参数
- git生成sshkey
- BZOJ 2683: 简单题(CDQ分治 + 树状数组)
- 2018年多校第三场第一题 A. Ascending Rating hdu6319
- win7系统查看硬盘序列号步骤
- 【HNOI 2018】道路
- BZOJ4942 NOI2017整数(线段树)
- Codeforces Round #506 (Div. 3) C. Maximal Intersection
- HTML鼠标悬停改变样式
- 【centOS】centos7 查看和关闭防火墙
- 虚拟机配置Cognos报错CFG-ERR-0106
- 20155339 2016-2017-2 《Java程序设计》第十周学习总结
- intellij idea 远程tomcat 调试
- pyqt5改变窗体颜色
- PostgreSQL源码分析之shared buffer与磁盘文件
- C# 中如何将List<;string>;里的集合转换成字符串并按指定的字符进行分隔?
- VirtualBox network / study environment setup for RHEL
- VMware虚拟机如何设置从U盘启动
热门文章
- redis介绍和安装和主从介绍(二)
- 【bzoj2901】矩阵求和 前缀和
- [LA3620]Manhattan Wiring
- BZOJ 4650 [Noi2016]优秀的拆分 ——后缀数组
- [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)
- 通过new ClasspathApplicationContext(";applicationContext.xml";)找不到文件时
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]
- 如何新建.htaccess文件
- docker使用 命令
- 王垠:谈 Linux,Windows 和 Mac ( 2013)