Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C Compiler.
- Code, Compile, Run and Debug C program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <stdio.h>
- #include <locale.h>
- const double __stdlib_string_to_double_table[17] =
- {
- 0.0,
- 0.1,
- 0.01,
- 0.001,
- 0.0001,
- 0.00001,
- 0.000001,
- 0.0000001,
- 0.00000001,
- 0.000000001,
- 0.0000000001,
- 0.00000000001,
- 0.000000000001,
- 0.0000000000001,
- 0.00000000000001,
- 0.000000000000001,
- 0.0000000000000001,
- };
- const int __stdlib_string_to_int_table_hex[6] =
- {
- 10, 11, 12, 13, 14, 15
- };
- const long long __stdlib_string_to_double_table_hex[17] =
- {
- 1,
- 16,
- 256,
- 4096,
- 65536,
- 1048576,
- 16777216,
- 268435456,
- 4294967296,
- 68719476736,
- 1099511627776,
- 17592186044416,
- 281474976710656,
- 4503599627370496,
- 72057594037927936,
- 1152921504606846976,
- -1,
- };
- const char*
- __string_skip_spaces(const char* str)
- {
- while (1)
- {
- if (*str > 0 && *str < 33)
- ++str;
- else
- break;
- if (!*str)
- break;
- }
- return str;
- }
- double
- strtod(const char* nptr, char** endptr)
- {
- double result = 0.0;
- long leftPart = 0;
- long rightPart = 0;
- int e_right = 0;
- int flags = 0; /* 1(-) 2(hex) 3(e) 4(e-)*/
- const char* str_ptr = __string_skip_spaces(nptr);
- const struct lconv* lc = localeconv();
- char decimal_point = *lc->decimal_point;
- /*with - or not*/
- if (*str_ptr == '-')
- {
- ++str_ptr;
- flags |= 0x1;
- }
- if (!*str_ptr)
- goto end;
- /*hex or not, skip 00*/
- if (*str_ptr == '0')
- {
- ++str_ptr;
- if (!*str_ptr)
- goto end;
- if (*str_ptr == 'x' || *str_ptr == 'X')
- {
- flags |= 0x2;
- ++str_ptr;
- }
- else if (*str_ptr == decimal_point)
- --str_ptr;
- else if (*str_ptr < '1' || *str_ptr > '9')
- goto end;
- }
- if (!*str_ptr)
- goto end;
- long part_2_count = 0;
- if (flags & 0x2)/*hex*/
- {
- int charNum = 0;
- const unsigned char* save_str_ptr = str_ptr;
- while (*str_ptr)
- {
- if ((*str_ptr >= 'a' && *str_ptr <= 'f')
- || (*str_ptr >= 'A' && *str_ptr <= 'F'))
- ++charNum;
- else
- break;
- ++str_ptr;
- if (!*str_ptr)
- break;
- }
- if (charNum)
- {
- str_ptr = save_str_ptr;
- int charNum2 = charNum;
- for (int i = 0; i < charNum; ++i)
- {
- int curr_char = str_ptr[i];
- if (curr_char < 'a')
- curr_char += 32;
- int curr_int = __stdlib_string_to_int_table_hex[curr_char - 97];
- leftPart += curr_int * (long)__stdlib_string_to_double_table_hex[charNum2 - 1];
- --charNum2;
- }
- }
- }
- else
- {
- /* left part 1234. */
- while (*str_ptr >= '0' && *str_ptr <= '9')
- {
- leftPart *= 10;
- leftPart += *str_ptr - '0';
- ++str_ptr;
- if (!*str_ptr)
- break;
- }
- if (*str_ptr == 'e' || *str_ptr == 'E')
- {
- flags |= 0x4;
- ++str_ptr;
- goto do_e;
- }
- /* right part .5678 */
- if (*str_ptr == decimal_point)
- {
- ++str_ptr;
- if (!*str_ptr)
- goto finish;
- while (*str_ptr >= '0' && *str_ptr <= '9')
- {
- rightPart *= 10;
- rightPart += *str_ptr - '0';
- ++str_ptr;
- ++part_2_count;
- }
- if (*str_ptr == 'e' || *str_ptr == 'E')
- {
- flags |= 0x4;
- ++str_ptr;
- goto do_e;
- }
- }
- }
- finish:;
- result = leftPart + ((float)rightPart * __stdlib_string_to_double_table[part_2_count]);
- if (flags & 0x4)
- {
- double em = 1.0;
- for (int i = 0; i < e_right; ++i)
- {
- if (flags & 0x8) /* - */
- em *= 0.1;
- else
- em *= 10.0;
- }
- result = result * em;
- }
- result = (flags & 0x1) ? -result : result;
- end:;
- if (endptr)
- *endptr = (char*)str_ptr;
- return result;
- do_e:;
- if (!*str_ptr)
- goto finish;
- if (*str_ptr == '-')
- {
- flags |= 0x8;
- ++str_ptr;
- if (!*str_ptr)
- goto finish;
- }
- else if (*str_ptr == '+')
- {
- ++str_ptr;
- if (!*str_ptr)
- goto finish;
- }
- while (*str_ptr >= '0' && *str_ptr <= '9')
- {
- e_right *= 10;
- e_right += *str_ptr - '0';
- ++str_ptr;
- }
- goto finish;
- }
- int main()
- {
- printf("Hello World %f\n", strtod("3.14159265359", 0));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement