-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex4_11.c
45 lines (35 loc) · 930 Bytes
/
ex4_11.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
#include <stdio.h>
#include <ctype.h>
#define NUMBER '0'
#define MAXGETCH 100
// ((ung_pos > 0) ? (ungetched[--ung_pos]) : (getchar()))
int getop(char s[])
{
static int ungetched[MAXGETCH], ung_pos = 0;
int c, i = 0;
while ((c = s[0] = ((ung_pos > 0) ? (ungetched[--ung_pos]) : (getchar()))) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c;
if (isdigit(c))
while (isdigit(c = s[++i] = ((ung_pos > 0) ? (ungetched[--ung_pos]) : (getchar()))))
;
if (c == '.')
while (isdigit(c = s[++i] = ((ung_pos > 0) ? (ungetched[--ung_pos]) : (getchar()))))
;
s[i] = '\0';
if (c != EOF)
ungetched[ung_pos++] = c;
return NUMBER;
}
int main()
{
int type;
char s[MAXGETCH];
// try input 123 123+ yes, without space. The + gets swallowed up without static ungetched
while ((type = getop(s)) != EOF) {
printf("%s %d\n", s, type);
}
return 0;
}