在minix2.0源代码中,有将字符串类型转换为int、long、double类型的函数实现,相关的实现函数分别在atoi.c、atol.c、atof.c文件中,我们来逐一学习其中的源码:

1、int atoi(register const char *nptr) :将字符串类型转换为int类型

int atoi(register const char *nptr)
{
int total = ;
int minus = ; //记录正负的变量(0:'+',1:'-') while (isspace(*nptr)) nptr++; //滤去前导空格字符
if (*nptr == '+') nptr++;
else if (*nptr == '-') {
minus = ;
nptr++;
}
while (isdigit(*nptr)) {
total *= ;
total += (*nptr++ - '');
}
return minus ? -total : total;
}

2、long atol(register const char *nptr) :将字符串类型转换为long类型,与atoi极为类似

 long atol(register const char *nptr)
{
long total = ;
int minus = ; while (isspace(*nptr)) nptr++;
if (*nptr == '+') nptr++;
else if (*nptr == '-') {
minus = ;
nptr++;
}
while (isdigit(*nptr)) {
total *= ;
total += (*nptr++ - '');
}
return minus ? -total : total;
}

3、double atof(const char *nptr):将字符串类型转换为double类型

 double atof(const char *nptr)
{
double d;
int e = errno; d = strtod(nptr, (char **) NULL); //strtod: stdlib.h中定义的库函数,将字符串转换为double型
//double strtod(const char * restrict nptr, char ** restrict endptr);
errno = e;
return d;
}

PS:在《C程序设计语言》中Ritchie提供了一种atof的实现:

 double atof(char *s)
{
double val, power;
int sign, i; for (i = ; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? - : ;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '');
power *= 10.0;
}
return sign * val / power;
}

后面的练习4-2,要求我们对atof函数进行扩展,使它能够处理形如123.45e-6这样的科学表示法

 #include<stdio.h>

 double atof(char *s)
{
double val, power, temp, exp;
char flag;
int sign, i; for (i = ; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? - : ;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '');
power *= 10.0;
}
temp = sign * val / power;
if (s[i] == 'e' || s[i] == 'E')
i++;
flag = s[i];
if (flag == '-' || flag == '+')
i++;
for (exp = ; isdigit(s[i]) && s[i] !='\0'; i++) {
exp = 10.0 * exp + (s[i] - '');
}
if (flag == '+') {
while(exp--) temp *= ;
} else {
while(exp--) temp /= ;
}
return temp;
} int main()
{
char a[] = " -2309.12E-15";
printf("%e\n",atof(a));
return ; }

minix中atoi、atol、atof的实现的更多相关文章

  1. strtol函數的用法 atof, atoi, atol, strtod, strtoul

    相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...

  2. _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明

    原文:http://www.cnblogs.com/lidabo/archive/2012/07/10/2584706.html _itoa 功能:把一整数转换为字符串 用法:char * _itoa ...

  3. MFC数据类型转换 _itoa atoi、atof、itoa、itow _itoa_s

    _itoa 功能:把一整数转换为字符串 用法:char * _itoa(int value, char *string, int radix); 详细解释: _itoa是英文integer to ar ...

  4. minix中二分查找bsearch的实现

    在看minix中bsearch实现的源代码之前,先学习一下C 语言中void类型以及void*类型的使用方法与技巧. void的含义: void的字面意思是“无类型”,void *则为“无类型指针”, ...

  5. Minix中的字符判定ctype.c

    minix中关于如何判定一个字符的类型,如大写.小写.数字…… 如果采用传统的方法,如判断一个字母大写的方法: if(c>='A' && c<'Z') return tru ...

  6. 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper

    atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...

  7. atoi atol strtod strtol strtoul _gcvt

    如果以下函数,您在使用的时候,总是输出一个莫名的值,是因为您忘记了引用头文件 #include <stdlib.h> 1- atoi int atoi(const char *nptr); ...

  8. atoi(),atof等函数的实现

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

  9. 模拟实现C库的atoi、atof和itoa

    1.C函数atoi atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数.广泛的应用在计算机程序和办公软件中.atoi( ) 函数会扫描参数 nptr字符 ...

随机推荐

  1. 配置IDEA Scala环境

    http://snglw.blog.51cto.com/5832405/1634595

  2. php脚本超时 结束执行代码

    函数:stream_context_create ,file_get_content 创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设 ...

  3. Backlight当前行背景高亮显示

    下载地址:https://github.com/limejelly/Backlight-for-XCode PS:Xcode 8.0 默认支持了 跟VVDocumenter规范注释生成器的安装方式一样 ...

  4. windows下redis启动失败提示maxheap flag

    windows下redis启动失败 D:\redis>redis-server.exe redis.conf [] Oct ::39.789 # The Windows version of R ...

  5. QT 实现QGraphicsProxyWidget对象可选择或移动(item管理实现)

    上篇博文<QT QGraphicsProxyWidget对象可选择或移动的一些tricks>介绍了实现QT QGraphicsProxyWidget对象可选择或移动的一些小的第三方技巧,但 ...

  6. Python打包-py2exe

    上篇文章讲了pyinstaller,可以打包成包含Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX等操作系统下的可执行文件,如果只针对Windows ...

  7. css3-rem

    http://www.w3cplus.com/css3/define-font-size-with-css3-rem https://mp.weixin.qq.com/s/DpLXJhfCHsgrbg ...

  8. c#系统消息类封装

    今天封装了一个返回json的消息类 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  9. android 不能在子线程中更新ui的讨论和分析

    问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...

  10. java的子类覆盖梗

    项目上线,用户注册时验证码一直报错误,数据库也没问题,代码貌似也没问题. 后面排查到最后,发现是一个子类覆盖父属性问题. JAVA代码中,子类覆盖父类的私有.保护属性,如果不设置get.set方法,拿 ...