#include <stdio.h>
#include <stdlib.h>
#include <limits.h> int my_atoi(char *str)
{
if(NULL == str)
{
printf("argc is null\n");
return -;
}
while(' ' == *str)
str++;
int flag = ;
if('-' == *str)
{
flag = -;
str++;
}
if('+' == *str)
{
str++;
}
unsigned int sum = ;
while(*str)
{
if(*str >= '' && *str <= '')
{
int tmp = *str - '';
sum *= ;
sum += tmp;
//此处判断溢出的目的是避免sum在计算过程中溢出
if(( == flag) && (sum > INT_MAX))
{
printf("int 正数溢出\n");
return -;
}
//计算最小int的绝对值,注意要使用unsigned int否则会溢出
unsigned int absIntMin = (unsigned int)INT_MAX + ;
if((- == flag) && (sum > absIntMin))
{
printf("int 负数溢出\n");
return -;
}
}
else
{
printf("str (%s) exists illegal character\n", str);
return -;
}
str++;
}
return flag * sum;
}

测试代码

#include <float.h>
#include <limits.h>
extern int my_atoi(char *str);
int main(int argc, char *argv[])
{
char a[] = {'\0'};
while()
{
scanf("%s", a);
printf("count = %d\n", my_atoi(a));
}
return ;
}

原创:实现atoi函数的更多相关文章

  1. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

  2. C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...

  3. 题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用

    #include<stdio.h> #include<stdlib.h> int sw(char *a){ ,c=; while(a[i]){ ') c=c*+a[i]-'; ...

  4. atoi()函数的实现

    atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...

  5. C语言itoa()函数和atoi()函数详解(整数转字符)

    http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...

  6. C语言itoa函数和atoi 函数

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h>  ...

  7. 【C语言】模拟实现atoi函数

    atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  9. atoi()函数(转载)

    atoi()函数 原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...

  10. 源码实现 --> atoi函数实现

    atoi函数实现 atoi()函数的功能是将一个字符串转换为一个整型数值. 例如“12345”,转换之后的数值为12345,“-0123”转换之后为-123. #include <stdio.h ...

随机推荐

  1. Django项目中添加富文本编辑器django-ckeditor

    django-ckeditor库的使用步骤: 1.在命令行下安装django-ckeditor这个库: 命令:pip install django-ckeditor 2.安装成功后,配置Django项 ...

  2. 安装consul

    概述consul是google开源的一个使用go语言开发的服务发现.配置管理中心服务.内置了服务注册与发现框架.分布一致性协议实现.健康检查.Key/Value存储.多数据中心方要依赖其他工具(比如Z ...

  3. MySQL 事务、视图、索引

    一.事务(Transaction) 1.1 什么是事务? SQL中,事务是指将一系列数据操作捆绑成为一个整体进行统一管理. 如果一个事务执行成功,该事务中进行的所有数据均会提交,称为数据库中的永久组成 ...

  4. .Net面试题一

    1.进程和线程的区别是什么? 答:https://www.cnblogs.com/renzhuang/articles/6733461.html2.请列举ASP.Net页面之间传递值的几种方式?列出3 ...

  5. jsp其实是一个java类

    我们打开tomcat的D:\Program_Files\apache-tomcat-8.0.32\work\Catalina\localhost\venus\org\apache\jsp, 当我们访问 ...

  6. word、ppt转换为pdf

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. 移动端真机debug调试神器 vConsole学习(二)之实战

    项目中实际使用 在项目中实际使用的时候发现还是有很多问题的 最初使用方式  <script src="vconsole.min.js"></script> ...

  8. O036、Snapshot Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5510296.html   有时候系统损坏的很严重,通过 Rescue 操作无法修复,那么我们就得重新考虑通过备份恢复了. ...

  9. LeetCode:181.超过经理收入的员工

    题目链接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/ 题目 Employee 表包含所有员 ...

  10. 谷歌浏览器禁用JS步骤

    前奏: 当你想访问一个页面,因为不符合访问条件,而被JS阻拦:或者你打开的页面特效太多,干扰视线:亦或者JS的某个功能禁止了你的某些行为: 是不是很难受, 好办,咱给他禁了不就好啦,大胆地屏蔽它,不管 ...