minix中atoi、atol、atof的实现
在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的实现的更多相关文章
- strtol函數的用法 atof, atoi, atol, strtod, strtoul
相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
原文:http://www.cnblogs.com/lidabo/archive/2012/07/10/2584706.html _itoa 功能:把一整数转换为字符串 用法:char * _itoa ...
- MFC数据类型转换 _itoa atoi、atof、itoa、itow _itoa_s
_itoa 功能:把一整数转换为字符串 用法:char * _itoa(int value, char *string, int radix); 详细解释: _itoa是英文integer to ar ...
- minix中二分查找bsearch的实现
在看minix中bsearch实现的源代码之前,先学习一下C 语言中void类型以及void*类型的使用方法与技巧. void的含义: void的字面意思是“无类型”,void *则为“无类型指针”, ...
- Minix中的字符判定ctype.c
minix中关于如何判定一个字符的类型,如大写.小写.数字…… 如果采用传统的方法,如判断一个字母大写的方法: if(c>='A' && c<'Z') return tru ...
- 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper
atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...
- atoi atol strtod strtol strtoul _gcvt
如果以下函数,您在使用的时候,总是输出一个莫名的值,是因为您忘记了引用头文件 #include <stdlib.h> 1- atoi int atoi(const char *nptr); ...
- atoi(),atof等函数的实现
atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...
- 模拟实现C库的atoi、atof和itoa
1.C函数atoi atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数.广泛的应用在计算机程序和办公软件中.atoi( ) 函数会扫描参数 nptr字符 ...
随机推荐
- 本文将详细介绍oracle 10g OEM常规错误
本文将详细介绍oracle 10g OEM常规错误-------Unknown host specified解决方法,需要了解的朋友可以参考下 详细出处参考:http://www.jb51.net/a ...
- virtualbox谨记:win7上只有4.3.x的版本支持ubuntu14.04.3虚拟机安装Oracle Rac,其他的版本3.x和5.0.2(至2015-08-30)均不可以
virtualbox谨记:win7上只有4.3.x的版本支持ubuntu14.04.3虚拟机安装Oracle Rac,其他的版本3.x和5.0.2(至2015-08-30)均不可以
- Ubuntu下PHP动态编译出现Cannot find autoconf的解决方法
执行phpize时出现Cannot find autoconf 错误 Ubuntu下解决方法 sudo apt-get install autoconf
- 8 -- 深入使用Spring -- 3...1 Resource实现类ClassPathResource
8.3.1 Resource实现类------ClassPathResource : 访问类加载路径下的资源的实现类 2.访问类加载路径下的资源 ClassPathResource 用来访问类加载路径 ...
- PHP 5.4 中的traits
PHP 5.4中的traits,是新引入的特性,中文还真不知道如何准确翻译好.其实际的目的,是为了有的场合想用多继承,但PHP又没多继承,于是就发明了这样的一个东西. Traits (横向重用/多重继 ...
- /usr/bin/ld: cannot find -lxxx 的解决办法
/usr/bin/ld: cannot find -lxxx 的解决办法 在软件编译过程中,经常会碰到类似这样的编译错误: /usr/bin/ld: cannot find -lhdf5 这表示找不到 ...
- video标签 api
video 设置及控制:http://www.w3school.com.cn/tags/html_ref_audio_video_dom.asp video 事件:http://www.w3schoo ...
- RF判断列表、字典、整数、字符串类型是否相同方法
${d} create list shk shsh${w} create list ${e} evaluate type(${d}) ${t} evaluate type(${w}) should ...
- 【RF库Collections测试】Remove From List
Name:Remove From ListSource:Collections <test library>Arguments:[ list_ | index ]Removes and r ...
- MongoDB(五)-- 副本集(replica Set)
一.副本集介绍 搭建副本集是为了实现mongodb高可用. Mongodb(M)表示主节点,Mongodb(S)表示备节点,Mongodb(A)表示仲裁节点.主备节点存储数据,仲裁节点不存储数据.客户 ...