K & R Exercise 4-2, extended enabling the processing of scientific notation forms
# Include <ctype. h> double atof (char s []) {double Val, power, e_sum; int e_val; int I, sign, e_sign; for (I = 0; s [I] = ''; I ++); Sign = (s [I] = '-')? (-1): 1; if (s [I] = '+' | s [I] = '-') I ++; For (val = 0.0; isdigit (s [I]); I ++) val = 10.0 * Val + (s [I]-'0'); If (s [I] = '. ') I ++; For (power = 1.0; isdigit (s [I]); I ++) {val = 10.0 * Val + (s [I]-'0'); Power * = 10.0 ;} if (s [I] = 'E' | s [I] = 'E') I ++; e_sign = (s [I] = '-')? (-1): 1; e_sum = 1.0; If (s [I] = '+' | s [I] = '-') I ++; for (e_val = 0; isdigit (s [I]); I ++) e_val = 10 * e_val + (s [I]-'0 '); for (I = 0; I <e_val; I ++) {If (e_sign> 0) e_sum * = 10; elsee_sum/= 10 ;} return sign * Val/power * e_sum ;}
This is what I wrote. The idea is relatively simple. It is basically the copy of the previous step, but the content of the judgment is changed, so it looks smelly and long.
The answer is concise. The previous one is used without adding new variables.
Atof function expansion