-
Notifications
You must be signed in to change notification settings - Fork 3
/
5-2.c
62 lines (46 loc) · 1.38 KB
/
5-2.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
#include <ctype.h>
#include <stdio.h>
int remove_spaces(char string[], int *index) {
while (isspace(string[*index]) && string[*index] != '\n')
(*index)++;
return 0;
}
int get_float(double *pointer, char string[], int *index) {
if (!isdigit(string[*index]) && string[*index] != EOF && string[*index] != '+' && string[*index] != '-')
return 0;
int sign = (string[*index] == '-') ? -1 : 1;
if (string[*index] == '+' || string[*index] == '-') {
if (isdigit(string[*index + 1])) {
(*index)++;
} else
return 0;
}
for (*pointer = 0; isdigit(string[*index]); (*index)++)
*pointer = 10 * *pointer + (string[*index] - '0');
double power = 1;
if (string[*index] == '.') {
(*index)++;
for (power = 1.0; isdigit(string[*index]); (*index)++) {
*pointer = 10.0 * *pointer + (string[*index] - '0');
power *= 10.0;
}
}
*pointer = *pointer / power * sign;
return 1;
}
int main() {
char input[100];
double number;
int index = 0;
input[index] = getchar();
while (input[index] != '\n') {
input[++index] = getchar();
}
index = 0;
while (input[index] != '\n') {
remove_spaces(input, &index);
if (get_float(&number, input, &index))
printf("Float : %g\n", number);
}
return 0;
}