为避免与标准库中的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;
}

最新文章

  1. java多线程监听JMS、MQ队列
  2. 把ResultSet对象转变成List对象
  3. dataZoom 详细参数
  4. git生成sshkey
  5. BZOJ 2683: 简单题(CDQ分治 + 树状数组)
  6. 2018年多校第三场第一题 A. Ascending Rating hdu6319
  7. win7系统查看硬盘序列号步骤
  8. 【HNOI 2018】道路
  9. BZOJ4942 NOI2017整数(线段树)
  10. Codeforces Round #506 (Div. 3) C. Maximal Intersection
  11. HTML鼠标悬停改变样式
  12. 【centOS】centos7 查看和关闭防火墙
  13. 虚拟机配置Cognos报错CFG-ERR-0106
  14. 20155339 2016-2017-2 《Java程序设计》第十周学习总结
  15. intellij idea 远程tomcat 调试
  16. pyqt5改变窗体颜色
  17. PostgreSQL源码分析之shared buffer与磁盘文件
  18. C# 中如何将List&lt;string&gt;里的集合转换成字符串并按指定的字符进行分隔?
  19. VirtualBox network / study environment setup for RHEL
  20. VMware虚拟机如何设置从U盘启动

热门文章

  1. redis介绍和安装和主从介绍(二)
  2. 【bzoj2901】矩阵求和 前缀和
  3. [LA3620]Manhattan Wiring
  4. BZOJ 4650 [Noi2016]优秀的拆分 ——后缀数组
  5. [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)
  6. 通过new ClasspathApplicationContext(&quot;applicationContext.xml&quot;)找不到文件时
  7. Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]
  8. 如何新建.htaccess文件
  9. docker使用 命令
  10. 王垠:谈 Linux,Windows 和 Mac ( 2013)