-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise4-11.c
44 lines (40 loc) · 970 Bytes
/
exercise4-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
#include <ctype.h>
#include "calc.h"
int getch(void);
/*getop: get next character or numeric operand */
int getop(char s[])
{
int i, c;
static int lastc = 0;
if (lastc == 0)
c = getch();
else {
c = lastc;
lastc = 0;
}
while ((s[0] = c) == ' ' || c == '\t')
c = getch();
s[1] = '\0';
if (!isdigit(c) && c != '.' && c != '-')
return c; /* not a number */
i = 0;
if (c == '-') {
if (isdigit(c = getch()) || c == '.')
s[++i] = c; /* negative number */
else {
if (c != EOF)
lastc = c;
return '-'; /* minus operator */
}
}
if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
lastc = c;
return NUMBER;
}