【c语言】 模拟实现库函数的atoi函数
- // 模拟实现库函数的atoi函数
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <ctype.h>
- int my_atoi(char const *p)
- {
- int ret = 0;
- int a = 0;
- int flag = 1;
- assert(p != NULL);
- while (isspace(*p))
- {
- p++;
- }
- while (*p)
- {
- if (*p == '+')
- p++;
- else if (*p == '-')
- {
- p++;
- flag = -1;
- }
- else if (*p >= '0'&& *p <= '9')
- {
- a = *p - '0';
- ret = (ret * 10 + a);
- p++;
- }
- else
- return 0;
- }
- if ((flag == 1 && ret > 0x7FFFFFFF) || (flag == -1 && ret < (signed int)0x80000000))
- return 0;
- return ret*flag;
- }
- int main()
- {
- printf("%d\n", my_atoi(" +2345"));
- printf("%d\n", my_atoi(" -2345"));
- printf("%d\n", my_atoi("+2345"));
- printf("%d\n", my_atoi("-2345"));
- printf("%d\n", my_atoi("2345"));
- printf("%d\n", my_atoi("2345"));
- printf("%d\n", my_atoi(""));
- printf("%d\n", my_atoi("123ab"));
- return 0;
- }
- <img src="http://img.blog.csdn.net/20150704145110095?
- watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
【c语言】 模拟实现库函数的atoi函数的更多相关文章
- 【c语言】模拟实现库函数的atof函数
// 模拟实现库函数的atof函数 #include <stdio.h> #include <string.h> #include <assert.h> #incl ...
- 模拟实现库函数的atoi、atof和itoa
1.函数atoi atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数.广泛的应用在计算机程序和办公软件中.atoi( ) 函数会扫描参数 nptr字符串 ...
- C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数
在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...
- 【C语言】模拟实现库函数strcat函数
//模拟实现库函数strcat函数 #include <stdio.h> #include <string.h> #include <assert.h> char ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- C语言提供了几个标准库函数 itoa() atoi()
C语言提供了几个标准库函数C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <s ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...
随机推荐
- 联想Thinkpad L460安装Win7 64位
单位发了L460,自带的系统为win10,但是涉及到很多工作以及客户都是在win7环境下,所以必须安装win7的系统,经过一番折腾,终于装好了. 主要顺序如下: 1,制作WINPE启动盘,如大白菜,老 ...
- 一、SQL系列之~使用SQL语言导出数据及实现定时导出数据任务
一般情况下,SQL数据库中带有导入与导出数据的直接按键操作,点击数据表所在的数据库--任务--导出/导入数据,根据导入/导出向导直接将数据导出即可. 但导出的数据格式多为Excel格式,如果需要导出的 ...
- Kettle环境变量配置
KETTLE_DIR=安装目录 KETTLE_HOME=安装目录 安装目录比如:D:\Kettle\pdi-ce-6.0.0.0-353\data-integration
- 常见文件MIME类型
常见文件MIME类型.asx,video/x-ms-asf .xml,text/xml .tsv,text/tab-separated-values .ra,audio/x-pn-realaudio ...
- CentOS6 在线安装PostgreSQL10
本文主要通过实际案例介绍如何在CentOS6环境中在线安装PostgreSQL10,安装环境需具备能够使用yum在线安装功能.具体安装步骤如下, 1 下载对应版本的PGDG文件 从https://yu ...
- [编码]ASCII、GBK、Unicode(万国码) 和 UTF-8
American ASCII编码 (American Standard Code for Information Interchange,美国信息互换标准代码) China gbk编码 ...
- vue-pdf的使用方法及解决在线打印预览乱码
最近在用vue做项目的时候,页面中需要展示后端返回的PDF文件,于是便用到了vue-pdf,其使用方法为 : npm install --save vue-pdf 官网地址:https://www.n ...
- border使用
border属性 border-width border-style border-color inherit border-style的值:none dotted(点线) dashed(虚 ...
- jboss-as-7.1.1.Final配置Jndi数据源(以mysql为例)
1.获取mysql驱动,可以从mysql官方网站下载: http://dev.mysql.com/downloads/connector/j/ 2.进入jboss-as-7安装目录下的modules目 ...
- div 背景放图和直接放图区别
<html> <head> <meta charset="UTF-8"> <title></title> <sty ...