在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. 【LSTM】Understanding-LSTMs

    http://colah.github.io/posts/2015-08-Understanding-LSTMs/

  2. jquery animate动画持续运动

    function fingers(){ $(".box01 .fingers").animate({"width":"7.5rem",&qu ...

  3. daterangepicker日历插件使用参数注意问题

    显示具体时间时分秒: timePicker设置为true,//有些资料写的pickerTime不太对 重点大坑:修改时间默认展示格式,把fomat写在locale中,网上很多资料说直接写在datera ...

  4. Android : Your APK does not seem to be designed for tablets.

    1. 解决办法: Add these config in AndroidManifest.xml <supports-screens android:smallScreens="tru ...

  5. mysql中显示当前数据库下的所有表,包括视图。

    环境说明: mysql版本:5.5.57-log 操作系统:Red Hat Enterprise Linux Server release 6.6 (Santiago) 需求:查看当前数据库下所有的表 ...

  6. event.keyCode与event.which

        //Netscape/Firefox/Opera中不支持 window.event.keyCode,需要用event.which代替//IE用event.keCode方法获取当前被按下的键盘按 ...

  7. HTML5标签canvas制作动画

    摘要: canvas可以绘制图像,自然而然的就可以制作动画,因为动画的每一帧都是图像.我们可以利用javascript的setInterval函数来实现动画效果. 下面是一个例子,小圆绕着红点圆心不停 ...

  8. springboot+jps+druid项目搭建

    pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  9. Ulua_toLua_基本案例(六)_LuaCoroutine2

    Ulua_toLua_基本案例(六)_LuaCoroutine2 using UnityEngine; using System.Collections; using LuaInterface; pu ...

  10. 8 -- 深入使用Spring -- 8...2 管理Hibernate的SessionFactory

    8.8.2 管理Hibernate的SessionFactory 当通过Hibernate进行持久层访问时,必须先获得SessionFactory对象,它是单个数据库映射关系编译后的内存镜像.在大部分 ...