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 prototypes: Doublemy_atof (char *str)
Tips:
You need to judge the minus sign in the function, the decimal point, and whether it is a numeric character. When judging a decimal point, you need to define a counter to calculate the number of numeric characters after the decimal point.
#include <stdio.h> #include <math.h>double my_atof (char *str) { char *ch = str; int flag=1; int point=0; int count=0; Double sum = 0; while (*ch! = ') } { if (*ch = = '-') //Judging minus sign { flag =-1; ch++; } if (*ch = = '. ') { point=1; ch++; } if (*ch >= ' 0 ' && *ch <= ' 9 ') { sum=sum*10+ (*ch-' 0 '); ch++; } if (point==1) //encounters a decimal counter plus 1 { count++; } } Sum=flag*sum*pow (10,-count); return sum;} int main () { char *str= "12.83"; Double N; N=my_atof (str); printf ("%f\n", n); return 0;}
The C language converts a numeric string to a number corresponding to this string (including positive floating-point numbers, negative floating-point function prototypes: Double my_atof (char *str)