-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-2.c
More file actions
107 lines (97 loc) · 1.79 KB
/
5-2.c
File metadata and controls
107 lines (97 loc) · 1.79 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <stdio.h>
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void);
void ungetch(int c);
int getint(int *pn);
double getfloat(double *dptr);
void
main(void)
{
print("KR 5-2\n");
double d = 0;
double *pn;
pn = &d;
getfloat(pn);
print("value of d: %f\n", d);
}
int getch(void)
{
print("inside getch\n");
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
print("ungetch: too many chars\n");
else
buf[bufp++] = c;
}
int getint(int *pn)
{
int c, sign;
print("trim input\n");
while (isspace(c = getch()))
;
print("determine if digit\n");
if (!isdigit(c) && c != 0 && c != '+' && c != '-') {
print("is not a digit\n");
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getch();
if (!isdigit(c)) {
ungetch(c);
print("no input following sign...\n");
return -1;
}
print("store in pn\n");
for (*pn = 0; isdigit(c); c = getch()) {
print("inside for; pn is: %d\n", *pn);
*pn = 10 * *pn + (c - '0');
}
*pn *= sign;
if (c != 0)
ungetch(c);
return c;
}
double
getfloat(double *dptr)
{
double power;
int c, sign;
while (isspace(c = getch()))
;
if (!isdigit(c) && c != 0 && c != '+' && c!= '-') {
print("not a digit...\n");
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getch();
if (!isdigit(c)) {
ungetch(c);
print("no input following sign...\n");
return -1;
}
for (*dptr = 0; isdigit(c); c = getch()) {
*dptr = 10.0 * *dptr + (c - '0');
}
if (c == '.') {
c = getch();
}
/* handle cases where there are no numbers after '.'? */
for (power = 1.0; isdigit(c); c = getch()) {
*dptr = 10.0 * *dptr + (c - '0');
power *= 10.0;
}
*dptr = sign * *dptr / power;
return c;
}