-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatof.cpp
66 lines (62 loc) · 831 Bytes
/
atof.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<string>
using namespace std;
double f(string num)
{
if(num.length()<1)
return 0;
int i=0;
double result=0;
bool deci=true;
int base=0;
while(i<num.length() && num[i]!='E')
{
if(num[i]=='.')
deci=false;
else
{
result=result*10+num[i]-'0';
if(!deci)
base++;
}
i++;
}
if(!deci)
base=-base;
int base2=0;
bool positive=true;
i++;
while(i<num.length())
{
if(num[i]=='-')
{
positive=false;
}
else
{
base2=base2*10+num[i]-'0';
}
i++;
}
if(!positive)
base2=-base2;
base+=base2;
positive=base>=0;
if(base<0)base=-base;
double radix=1;
while(base>0)
{
radix*=10;
base--;
}
cout<<base2<<" "<<radix<<" "<<result<<endl;
if(positive)
result*=radix;
else
result/=radix;
return result;
}
main()
{
cout<<f("3.23E-12")<<endl;
}