Skip to content

Commit 7121845

Browse files
committed
minor tweaks
1 parent 4e3551a commit 7121845

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

chapter07/7-5.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
#include <stdio.h>
12-
#include <ctype.h>
12+
#include <string.h>
1313

1414
#define MAXOP 100 /* max size of operand or operator */
1515
#define NUMBER '0' /* signal that a number was found */
@@ -49,20 +49,20 @@ double pop(void)
4949
int getop(double *number)
5050
{
5151
char c;
52+
int match;
5253

53-
if (scanf("%c", &c) != 1)
54+
if (scanf("%c", &c) < 0)
5455
return EOF;
55-
56-
while (isblank(c))
57-
scanf("%c", &c);
58-
59-
if (!isdigit(c) && c != '.')
60-
return c; /* not a number */
61-
62-
if (isdigit(c) || c == '.') {
63-
ungetc(c, stdin); /* push back */
64-
scanf("%lf", number);
65-
}
56+
if (c == '\n')
57+
return c;
58+
ungetc(c, stdin);
59+
match = scanf("%lf ", number); /* scan number, consumed trailing whitespace */
60+
if (!match) { /* not a number */
61+
if (c == '+' || c == '-') /* check for consumed leading op on failed match */
62+
ungetc(c, stdin);
63+
scanf("%c", &c); /* scan op */
64+
return c;
65+
}
6666
return NUMBER;
6767
}
6868

chapter07/7-5a.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
void push(double);
2121
double pop(void);
2222
int getop(double *);
23-
void ungetns(char *, int );
23+
void ungets(char *);
2424

2525
/* globals */
2626
int sp = 0; /* next free stack position */
@@ -50,8 +50,9 @@ double pop(void)
5050
int getop(double *number)
5151
{
5252
char c;
53-
int n; /* number of reaf characters */
54-
static char s[MAXOP];
53+
int n; /* number of consumed characters */
54+
int match;
55+
char s[MAXOP];
5556

5657
if (scanf("%c", &c) < 0)
5758
return EOF;
@@ -61,26 +62,27 @@ int getop(double *number)
6162

6263
ungetc(c, stdin);
6364
scanf("%s", s); /* parse string from input stream */
64-
if (!sscanf(s, "%lf %n", number, &n)) { /* match number from string */
65-
sscanf(s, "%c", &c); /* not a number */
65+
match = sscanf(s, "%lf %n", number, &n); /* scan number, consume trailing witespace */
66+
if (!match) { /* not a number */
67+
sscanf(s, "%c", &c);
6668
if (strlen(s) > 1) /* check for unread characters */
67-
ungetns(s, 1);
69+
ungets(s + 1);
6870
return c;
6971
}
7072

7173
if (n != (int) strlen(s)) /* check unread char from match */
72-
ungetns(s, n);
74+
ungets(s + n);
7375

7476
return NUMBER;
7577
}
7678

77-
/* ungetns: push back a string starting at position n onto input stream */
78-
void ungetns(char *s, int n)
79+
/* ungets: push back s on input */
80+
void ungets(char *s)
7981
{
80-
int i;
82+
int len;
8183

82-
for (i = strlen(s) - 1 ; i >= n; --i)
83-
ungetc(s[i], stdin);
84+
for (len = strlen(s); len >= 0; --len)
85+
ungetc(s[len], stdin);
8486
}
8587

8688
/* reverse polish calculator - scanf/sscanf version */

0 commit comments

Comments
 (0)