Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <limits.h>
- #include <ctype.h>
- double my_atof(char *str)
- {
- double res = 0;
- while(!isdigit(*str) && *str != 'e' && *str != 'E' && *str != '-')
- str++;
- if (*str == 'e' || *str == 'E')
- return 0;
- int sign_numb = 1;
- if (*str == '-')
- {
- sign_numb = -1;
- str++;
- }
- if (*str == 'e' || *str == 'E')
- return 0;
- while (isdigit(*str))
- res = res * 10 + (*str++ - '0');
- if (!*str)
- return res * sign_numb;
- int power = 0;
- if (*str == '.')
- {
- str++;
- while (isdigit(*str))
- {
- res = res * 10 + (*str++ - '0');
- power++;
- }
- for (int i = 0; i < power; i++)
- res /= 10;
- }
- if (!*str)
- return res * sign_numb;
- int exp = 0;
- if (*str == 'e' || *str == 'E')
- {
- str++;
- int sign_exp = 0;
- if (*str == '-')
- {
- sign_exp++;
- str++;
- }
- while (isdigit(*str))
- exp = exp * 10 + (*str++ - '0');
- if (sign_exp)
- for (int i = 0; i < exp; i++)
- res /= 10;
- else
- for (int i = 0; i < exp; i++)
- res *= 10;
- }
- return res * sign_numb;
- }
- int main(void)
- {
- double res = 0;
- res = my_atof("123");
- printf("%s %lf\n", "123", res);
- res = my_atof("123.456");
- printf("%s %lf\n", "123.456", res);
- res = my_atof("-123");
- printf("%s %lf\n", "-123", res);
- res = my_atof("-123.456");
- printf("%s %lf\n", "-123.456", res);
- res = my_atof("123.456E4");
- printf("%s %lf\n", "123.456E4", res);
- res = my_atof("123.456E-4");
- printf("%s %lf\n", "123.456E-4", res);
- res = my_atof("-123.456E4");
- printf("%s %lf\n", "-123.456E4", res);
- res = my_atof("-123.456E-4");
- printf("%s %.8lf\n", "-123.456E-4", res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement