原创:实现atoi函数
- #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函数的更多相关文章
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- 题目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]-'; ...
- atoi()函数的实现
atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- atoi()函数(转载)
atoi()函数 原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...
- 源码实现 --> atoi函数实现
atoi函数实现 atoi()函数的功能是将一个字符串转换为一个整型数值. 例如“12345”,转换之后的数值为12345,“-0123”转换之后为-123. #include <stdio.h ...
随机推荐
- PDO原生分页
** PDO分页** 1.PDO连接数据库 $dbh=new PDO('mysql:host=127.0.0.1;dbname=03a','root','root');//使用pdo 2.接收页码 $ ...
- webSocket协议和Socket.IO
一.Http无法轻松实现实时应用: ● HTTP协议是无状态的,服务器只会响应来自客户端的请求,但是它与客户端之间不具备持续连接. ● 我们可以非常轻松的捕获浏览器上发生的事件(比如用户点击了盒子), ...
- css;js学习(一)
推荐基础前端学习地址https://ke.qq.com/course/315961蝉壳学院 清除浮动 .clearfix:before,.clearfix:after{ content: " ...
- 你不知道的css各类布局(四)之响应式布局
响应式布局 概念 响应式布局指的是同一页面在不同屏幕尺寸下有不同的布局 布局特点 响应式设计的目标是确保一个页面在所有终端上(各种尺寸的PC.手机.手表.冰箱的Web浏览器等等)都能显示出令人满意的效 ...
- JS基础_一元运算符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- SpringCloud 配置文件 application.yml和 bootstrap.yml区别
前言: SpringBoot默认支持properties和YAML两种格式的配置文件.前者格式简单,但是只支持键值对.如果需要表达列表,最好使用YAML格式.SpringBoot支持自动加载约定名称的 ...
- webpack升级4出现的问题
webpack3升级到4出现了很多问题,经过验证报错信息如下 1 Module parse failed: Unexpected token (:) You may need an appropria ...
- JAVA语言程序设计课后习题----第二单元解析(仅供参考)
1 注意不同类型转换 import java.util.Scanner; public class Ch02 { public static void main(String[] args) { Sc ...
- 命令行工具--curl
目录 命令:curl 一.简介 二.使用案例 1.基本用法 2.保存访问的网页 3.测试网页返回值 4.指定proxy服务器以及其端口 5.cookie 6.模仿浏览器 7.伪造referer(盗链) ...
- OpenResty 执行流程阶段
nginx有11个处理阶段,如下图所示: 指令 所处处理阶段 使用范围 解释 init_by_luainit_by_lua_file loading-config http nginx Master进 ...