Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- double _atof_(const char *x);
- double _pow_(int x, int y);
- int main()
- {
- printf("%lf\n", _atof_("3.141592"));
- printf("%lf\n", _atof_("-3.14159"));
- printf("%lf\n", _atof_("78"));
- printf("%lf\n", _atof_("-78"));
- printf("%lf\n", _atof_("3.14159_"));
- printf("%lf\n", _atof_("3.141.59"));
- printf("%lf\n", _atof_("-3."));
- printf("%lf\n", _atof_("-"));
- printf("%lf\n", _atof_("-b"));
- printf("%lf\n", _atof_("ABCDEFG"));
- std::cin.get();
- return 0;
- }
- double _atof_(const char *x)
- {
- double res = 0.0;
- double res2 = 0.0;
- bool neg = false;
- int help = -1;
- int dot = 0;
- if (x[0] == '-')
- {
- neg = true;
- }
- for (int i = 0 + neg; x[i] != '\0'; i++)
- {
- int val = (x[i] - 48);
- if ((val < 0) || (val > 9))
- {
- if (val == -2 && i != 0 && dot == 0 && x[i+1] != '\0')
- {
- dot = i;
- }
- else
- return 0.0;
- }
- else if (dot == 0)
- {
- res += val / _pow_(10, i);
- help += 1;
- }
- else
- {
- res2 += val / _pow_(10, i-dot);
- }
- }
- res *= _pow_(10, help + neg);
- res += res2;
- if (neg == true)
- {
- res -= res * 2;
- }
- return res;
- }
- double _pow_(int x, int y)
- {
- if (y == 0)
- return 1;
- else if (y == 1)
- return x;
- else
- {
- int res = x;
- for (int i = 1; i < y; i++)
- {
- res *= x;
- }
- return res;
- }
- return 0.0;
- }
Advertisement
Add Comment
Please, Sign In to add comment