Atof function for converting a string argument to a floating-point type
/* system:linux Self-implemented atof function, the main function is used to determine the legalization of parameters, if the law calls the sub-function, or exit This program contains two algorithms atof implementation, are valid */#include < stdio.h> #include <string.h>float str_to_float (CHAR&NBSP;*STR); FLOAT&NBSP;STR_TO_FLOAT1 (char &NBSP;*STR); Int main (int argc, char* argv[]) { /* i for string traversal n for recording the length of a string flag_point Used to mark whether the decimal point has occurred default 0 occurs after the decimal point is 1 flag_return the owning tag string is illegal default is 0 illegal after 1 result to store results without +/- str point to String to convert */ int i, n, flag_point, flag_return; Float result; char *str; if (argc == 1) { fprintf (stderr, "Usage: %s string\n ", argv[0]); return 1; } flag_point = 0; flag_return = 0; n = strlen (argv[1]); str = argv[1]; result = 0.0; for (i = 0;i < n;i++) { //determines if +/-is present in the string, if it is valid in the first place, or illegal if (str[i ] == ' + ' | | str[i] == '-') { if (i == 0) continue; else { flag_return = 1; break; } } //to judge that the decimal point can occur at most once, otherwise illegal if (str[i] == '. ') { if (flag_point == 0) if (i == 0) { flag_return = 1; break; } else&nbsP { flag_point = 1; continue; } else { flag_return = 1; break; } } //the rest of the word 符必 within 0-9 range Otherwise illegal if (str[i] >= ' 0 ' && str[i] <=&nbSP; ' 9 ') continue; else { flag_return = 1; break; } } if (flag_ return == 0) { result = str_to_float ( ARGV[1]); printf ("The %s transform float is %f\n ", argv[1], result); } else printf ("the %s string is illegal\n", ARGV[1]); return 0; }/* algorithm 1 traversing a string from left to right (from high to Low) example: integer part taken 123 = (((1*10) +2) *10) +3 method The fractional part takes the 0.123 = 1/10 + 2/100 + 3/1000 method and finally adds the integer to the fractional part */float str_to _float (CHAR&NBSP;*STR) { /* m saving results for small parts k Save the integer part of the result o save the fractional part of each bit flag Save sign flag_point standard decimal point has appeared, Change the conversion mode p Save the value that corresponds to the number of digits that are currently in the decimal point, such as the first bit p 10 second bit p 100 */ float m, o; int n, i, flag, flag_point, k, p; n = strlen (str); flag = 1; flag_point = 1; k = 0; m = 0; o = 0; for (i = 0;i < n;i++) { if (i == 0 && (str[i] == ' + ' | | str[i] == '-') { if (str[i] == '-') flag = -1; continue; } if (str[i] == '. ') { flag_point = 0; p = 1; continue; } if (Flag_point) { k = k*10 + (str[i] - ' 0 '); } else { p *= 10; o = str[i] - ' 0 '; m += o/p; } } return flag* (k+m);} /* method 2 traversal string from right to left (from low to high) example: 12.34 first get 1234 and record decimal position re-use 1234/100 = &NBSP;12.34*/FLOAT&NBSP;STR_TO_FLOAT1 (CHAR&NBSP;*STR) {/* flag for marking decimal places flag1 the positive and negative result that are used to tag strings have stored results p is used to save the current to access how many bits q is used to save a total of several decimal */ int i, n, p, flag, q, flag1; float result; n = strlen (str); result = 0; p = 1; q = 1; flag = 0 ; flag1 = 1; for (i = n- 1;i >= 0;i--) { //used to skip the extra 0 at the right end if (i == (n -1)) while (str[i] == ' 0 ') i--; if (str[i] == '. ') { flag = 1; continue; } if (flag == 0) q *= 10; if (i == 0 && (str[i] == ' + ' | | str[i] == '-') { if (str[i] == '-') flag1 = -1; continue; } result += p* (str[i] -' 0 '); p *= 10; } return flag1*result/q;}
Summarize:
Seemingly small functions, realized quite a lot of trouble, and practice.
1, the variable after the application, must be assigned the initial value before use
2, Float type variable has the precision loss in the assignment process
3. The problem of boundary judgment
Self-implementation of atof function