-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.2.c
50 lines (45 loc) · 812 Bytes
/
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
#include <stdio.h>
#include <ctype.h>
char getch();
void ungetch(char c);
int getfloat(float *pf) {
int c;
int sign = 1;
int power = 0;
while (isspace((c = getch()))) {
;
}
if (c == '-') {
sign = -1;
}
if (c == '-' || c == '+') {
c = getch();
}
*pf = 0.0;
while(isdigit(c)) {
*pf = *pf * 10 + c - '0';
c = getch();
printf("debug %f\n", *pf);
}
if (c == '.') {
c = getch();
}
while(isdigit(c)) {
*pf = *pf * 10 + c - '0';
power++;
c = getch();
}
while (power > 0) {
*pf = *pf / 10;
power--;
}
if (c != EOF) {
ungetch(c);
}
return c;
}
int main() {
float f;
printf("%d\n", getfloat(&f));
printf("%f\n", f);
}