-
Notifications
You must be signed in to change notification settings - Fork 0
/
getop.c
43 lines (36 loc) · 1.08 KB
/
getop.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
#include <ctype.h>
#include <stdio.h>
#include "calc.h"
/* getop: get next character or numeric operand */
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.' && c != '-') // Check for '-' as well
return c; /* not a number */
i = 0;
if (c == '-') { // Handle negative sign
c = getch(); // Get the next character
if (!isdigit(c) && c != '.') { // If it's not a digit or '.', it's not a number
ungetch(c); // Put the character back
return '-'; // Return '-' as the operator
} else {
s[++i] = c;
while(isdigit(s[++i] = c = getch()))
;
printf("debug: %s", s);
}
}
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)
ungetch(c);
return NUMBER;
}