在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. Rx与Async Task的简单对比

    有关Reactive Extensions的介绍可见https://rx.codeplex.com/,总的来说,你可以当它是又一个异步编程的框架,它以观察者模式实现了对数据流的的“订阅”.一个列表,一 ...

  2. 源码分析四(HashMap与HashTable的区别 )

    这一节看一下HashMap与HashTable这两个类的区别,工作一段时间的程序员都知道, hashmap是非线程安全的,而且key值和value值允许为null,而hashtable是非线程安全的, ...

  3. eclipse中开发js会卡,去掉.project中的validate即可

    注释掉 <buildCommand> <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name> & ...

  4. pycharm使用docker容器的python解释器,

    上一篇是pycharm调用docker的镜像的python解释器. 此篇介绍pycharm 调用docker的容器的python解释器. 这两个思路还是不一样的,第一个是用pycham界面的选择pyt ...

  5. php 内存分配

    php内核中的内存分配 使用的函数有 emalloc(), erealloc() ,这两个函数分别是malloc(),realloc()函数的封装 关于内存分配有四个容器:cache,小块内存链表,大 ...

  6. SQLServer------备份与还原

    转载: http://www.cnblogs.com/zgqys1980/archive/2012/07/04/2576382.html

  7. Linux+Redis实战教程_day01_Linux系统上安装tomcat

    Linux系统上安装tomcat 安装tomcat 上传tomcat的安装文件 Alt+p 拖拽上传 创建tomcat的安装路径 mkdir -p /usr/local/tomcat 解压tomcat ...

  8. ios开发之--textview意见反馈页面(占位label,字数统计,提交按钮的交互设置)

    记录一个页面的功能: textview的占位符,字数统计,提交按钮的交互设置,具体效果图如下:

  9. 【Cesium】天空盒子

    skyBox: new Cesium.SkyBox({ sources: { positiveX: 'static/image/skyBox/posx.jpg', negativeX: 'static ...

  10. Unity Animation需要Inspector右键打开Debug模式,然后勾选Legacy,最后再Inspector右键打开Normal