-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchapt4.c
74 lines (61 loc) · 1.33 KB
/
chapt4.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
#include<ctype.h>
#include<stdio.h>
#include<string.h>
double atof(char s[]);
int strindex(char s[], char t[]);
int main(int argc, char *argv[])
{
printf("Exercice 4.1\n");
char *s = "montfiergolfiere";
char *t = "fier";
printf("%s %s %d\n", s, t, strindex(s,t));
printf("Exercice 4.2\n");
printf("%f\n", atof("13.45"));
return 0;
}
double atof(char s[])
{
double val, power, val_exponent;
int i, sign, exponent, sign_exponent, j;
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;
}
exponent = (lower(s[i]) == 'e')? 1 : 0;
if(exponent == 1)
i++;
sign_exponent = (s[i] == '-')? -1 : 1;
if(sign_exponent == 1)
i++;
for(j = i+1; isdigit(s[j]); j++)
{
val_exponent = 10.0 * val_exponent + (s[j] - '0');
}
val_exponent = (sign_exponent == -1)? val_exponent = 1 / val_exponent : val_exponent;
return (sign * val / power) * val_exponent;
}
int strindex(char s[], char t[])
{
int i, j, k, rightmost;
rightmost = -1;
for(i=0; s[i] != '\0'; i++)
{
for(j=i, k=0; t[k] != '\0' && s[j] == t[k]; j++, k++)
;
if(k > 0 && t[k] == '\0')
rightmost = i;
}
return rightmost;
}