Write a function that converts a numeric string to a number corresponding to the string (including positive floating-point numbers, negative floating-point numbers)
For example: "12.34 " returns 12.34 "123.34" return-123.34 function prototype: Double my_atof (char *str)
#include <stdio.h> #include <ctype.h> #include <cmath>double my_atof (char *str) {DOUBLE ret = 0.0;int Sign = 1;char *point;while (*str! = ') {if (Isspace (*STR))//isspace Determine the space str++;else if (*str = = '-') {sign = -1;str++;} else if (*str = = ' + ') str++;else if (*str = = '. ') {point = str;str++;} else if ((*str >= ' 0 ') && (*str <= ' 9 ')) {Ret=ret * 10.0 + (*STR-' 0 ');//character and number difference between characters 0str++;}} ret = Sign*ret/pow (10,str-point-1); return ret;} int main () {double Ret;char arr[100];scanf ("%s", arr), ret = My_atof (arr);p rintf ("%f\n", ret); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Double My_atof (char *str)