fork download
  1. #include <stdio.h>
  2.  
  3. int str2int(char *s)
  4. {
  5. char *p;
  6. int r = 0;
  7. for(p = s; *p; ++p)
  8. if(0x30 <= *p && *p <= 0x39) r = r * 10 + *p - 0x30;
  9. return r;
  10. }
  11.  
  12. double str2double(char *s)
  13. {
  14. char *p;
  15. double r = 0.0, f = 1.0;
  16. for(p = s; *p; ++p)
  17. if(0x30 <= *p && *p <= 0x39){
  18. r = r * (f >= 1.0 ? 10 : 1) + (*p - 0x30) * (f >= 1.0 ? 1 : f);
  19. if(f < 1.0) f *= 0.1;
  20. }else f *= 0.1;
  21. return r;
  22. }
  23.  
  24. int main(int ac, char **av)
  25. {
  26. fprintf(stdout, "%d\n", str2int("9240"));
  27. fprintf(stdout, "%f\n", str2double("0.0543"));
  28. fprintf(stdout, "%f\n", str2double("9240.0543"));
  29. fprintf(stdout, "%d\n", str2int("9240.0"));
  30. return 0;
  31. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
9240
0.054300
9240.054300
92400