atoi(),atof等函数的实现

时间:2024-04-16 11:19:37

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

atoi()函数实现的代码:

  1. /*
  2. * name:xif
  3. * coder:xifan@2010@yahoo.cn
  4. * time:08.20.2012
  5. * file_name:my_atoi.c
  6. * function:int my_atoi(char* pstr)
  7. */
  8. int my_atoi(char* pstr)
  9. {
  10. int Ret_Integer = 0;
  11. int Integer_sign = 1;
  12. /*
  13. * 判断指针是否为空
  14. */
  15. if(pstr == NULL)
  16. {
  17. printf("Pointer is NULL\n");
  18. return 0;
  19. }
  20. /*
  21. * 跳过前面的空格字符
  22. */
  23. while(isspace(*pstr) == 0)
  24. {
  25. pstr++;
  26. }
  27. /*
  28. * 判断正负号
  29. * 如果是正号,指针指向下一个字符
  30. * 如果是符号,把符号标记为Integer_sign置-1,然后再把指针指向下一个字符
  31. */
  32. if(*pstr == '-')
  33. {
  34. Integer_sign = -1;
  35. }
  36. if(*pstr == '-' || *pstr == '+')
  37. {
  38. pstr++;
  39. }
  40. /*
  41. * 把数字字符串逐个转换成整数,并把最后转换好的整数赋给Ret_Integer
  42. */
  43. while(*pstr >= '0' && *pstr <= '9')
  44. {
  45. Ret_Integer = Ret_Integer * 10 + *pstr - '0';
  46. pstr++;
  47. }
  48. Ret_Integer = Integer_sign * Ret_Integer;
  49. return Ret_Integer;
  50. }

现在贴出运行my_atoi()的结果,定义的主函数为:int  main  ()

  1. int main()
  2. {
  3. char a[] = "-100";
  4. char b[] = "456";
  5. int c = 0;
  6. int my_atoi(char*);
  7. c = atoi(a) + atoi(b);
  8. printf("atoi(a)=%d\n",atoi(a));
  9. printf("atoi(b)=%d\n",atoi(b));
  10. printf("c = %d\n",c);
  11. return 0;
  12. }

头文件:#include <stdlib.h>函数 atof() 用于将字符串转换为双精度浮点数(double),其原型为:
double atof (const char* str);
atof() 的名字来源于 ascii to floating point numbers 的缩写,它会扫描参数str字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数str
字符串可包含正负号、小数点或E(e)来表示指数部分,如123. 456 或123e-2。
【返回值】返回转换后的浮点数;如果字符串 str 不能被转换为 double,那么返回 0.0。

温馨提示:ANSI C 规范定义了 stof()atoi()atol()strtod()strtol()strtoul() 共6个可以将字符串转换为数字的函数,大家可以对比学习;使用
atof() 与使用 strtod(str, NULL) 结果相同。另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull()。

#include<iostream>

using namespace std;

double atof_my(const char *str)
{
double s=0.0; double d=10.0;
int jishu=; bool falg=false; while(*str==' ')
{
str++;
} if(*str=='-')//记录数字正负
{
falg=true;
str++;
} if(!(*str>=''&&*str<=''))//如果一开始非数字则退出,返回0.0
return s; while(*str>=''&&*str<=''&&*str!='.')//计算小数点前整数部分
{
s=s*10.0+*str-'';
str++;
} if(*str=='.')//以后为小树部分
str++; while(*str>=''&&*str<='')//计算小数部分
{
s=s+(*str-'')/d;
d*=10.0;
str++;
} if(*str=='e'||*str=='E')//考虑科学计数法
{
str++;
if(*str=='+')
{
str++;
while(*str>=''&&*str<='')
{
jishu=jishu*+*str-'';
str++;
}
while(jishu>)
{
s*=;
jishu--;
}
}
if(*str=='-')
{
str++;
while(*str>=''&&*str<='')
{
jishu=jishu*+*str-'';
str++;
}
while(jishu>)
{
s/=;
jishu--;
}
}
} return s*(falg?-1.0:1.0);
} int main()
{
char *s1=" 123.456567567e-10";
char *a1=" 123.456567567e-10"; char *s2="1234567.235e+10";
char *a2="1234567.235e+10"; char *s3=" 123.45656\07567e-10";
char *a3=" 123.45656\07567e-10"; double sum_1=atof_my(s1);
double sum1=atof(a1); double sum_2=atof_my(s2);
double sum2=atof(a2); double sum_3=atof_my(s3);//遇到'\0'结束
double sum3=atof(a3); cout<<"atof_my:"<<sum_1<<endl;
cout<<"atof :"<<sum1<<endl; cout<<"atof_my:"<<sum_2<<endl;
cout<<"atof :"<<sum2<<endl; cout<<"atof_my:"<<sum_3<<endl;
cout<<"atof :"<<sum3<<endl; system("pause");
return ;
}

相关文章