forked from Heatwave/The-C-Programming-Language-2nd-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.getop-with-static.c
More file actions
48 lines (40 loc) · 789 Bytes
/
5.getop-with-static.c
File metadata and controls
48 lines (40 loc) · 789 Bytes
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
#include <ctype.h>
#include <stdio.h>
#define BUFSIZE 100
#define MAX_LEN 1024
#define NUMBER '0'
int getop(char []);
int main()
{
while (1) {
char s[MAX_LEN];
int op = getop(s);
printf("op: %c\n", op);
}
}
int getop(char s[])
{
static int buffer = EOF;
printf("buffer: %d\n", buffer);
int i, c;
while ((s[0] = c = (buffer == EOF ? getchar() : buffer)) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c;
i = 0;
if (isdigit(c))
while (isdigit(s[++i] = c = (buffer == EOF ? getchar() : buffer)))
;
if (c == '.')
while (isdigit(s[++i] = c = (buffer == EOF ? getchar() : buffer)))
;
s[i] = '\0';
if (c != EOF) {
if (buffer != EOF)
printf("getop: cannot ungetch more char\n");
else
buffer = c;
}
return NUMBER;
}