atof(char *str)将一个数字字符串转换成这个字符串对应的数字(

/*将一个数字字符串转换成这个字符串对应的数字(包括正浮点数、负浮点数)例如:“12.34“ 返回12.34“-123.34“ 返回-123.34函数原型:double my_atof(char *str)*/#include<stdio.h>#include<math.h>double my_atof(char *str){double res = 0.0; //结果int count = 0;int i = 0;int j = 0;if (str[0] == '-'){for (i = 1; str[i] != '\0' && str[i] != '.'; i++){res = res * 10 + (str[i] – '0');}i++;for (j = i, count = 1; str[j] != '\0'; j++, count++){res = res + (str[j] – '0') * pow(0.1,count);}return (0 – res);}else{for (i = 0; str[i] != '\0' && str[i] != '.'; i++){res = res * 10 + (str[i] – '0');}i++;for (j = i, count = 1; str[j] != '\0'; j++, count++){res = res + (str[j] – '0') * pow(0.1, count);}return (res);}}int main(void){char *str_1 = "-8.12";//测试char *str_2 = "365.46";//测试printf("转换后的数为:%f\n", my_atof(str_1));printf("转换后的数为:%f\n", my_atof(str_2));return 0;}

,孤独是为了孤独背后的解脱,孤独的过程,就是一个寻找真爱的过程。

atof(char *str)将一个数字字符串转换成这个字符串对应的数字(

相关文章:

你感兴趣的文章:

标签云: