-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.c
88 lines (67 loc) · 1.49 KB
/
1.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Program demonstrating atof(char s[]).The function which converts the string to a floating
point value */
#include<stdio.h>
#include<ctype.h>
#include<math.h>
#define MAXLINE 100
double myatof(char s[]);
int mgetline(char line[],int maxline);
int main(void)
{
char str[MAXLINE];
double num;
mgetline(str,MAXLINE);
num=myatof(str);
printf("%f",num);
return 0;
}
double myatof(char s[])
{
double val,power;
int sign,i,esign,exp;
int powerer(int base,int exp);
for(i=0;isspace(s[i]);i++)
;
sign=(s[i]=='-')?-1:1;
if(s[i]=='+' || s[i] == '-')
i++;
for(val=0.0;isdigit(s[i]);i++)
val = 10.0 * val + (s[i] - '0');
if(s[i]=='.')
i++;
for(power=1.0;isdigit(s[i]);i++)
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
if(s[i]=='e' || s[i] =='E')
i++;
if(s[i]=='+' || s[i] =='-')
{
esign=s[i];
i++;
}
for(exp=0;isdigit(s[i]);i++)
exp=10.0 * exp + (s[i] - '0');
if( esign == '-')
return sign * (val / power) / pow(10,exp);
else
return sign * (val / power) * pow(10,exp);
}
int mgetline(char s[],int lim)
{
int i,c;
for(i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';++i)
s[i] = c;
if(c=='\n')
s[i++]=c;
s[i]='\0';
}
int powerer(int base,int exp)
{
int powerer;
powerer=1;
while(exp-- > 0)
powerer *=base;
return powerer;
}