Skip to content

Commit 7f68da5

Browse files
authored
Merge pull request #55 from rfhits/main
fix: support minus operator
2 parents 766dc02 + 91cf26f commit 7f68da5

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

chapter_4/exercise_4_03/calculator.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,19 @@ int getop(char s[])
143143
if (c == '-')
144144
{
145145
int next = getch();
146-
if (!isdigit(next) && next != '.')
146+
if (next == '\n' || next == ' ' || next == '\t')
147147
{
148-
return next;
148+
ungetch(next);
149+
return c; // return '-' as operator
150+
}
151+
else if (!isdigit(next) && next != '.')
152+
{
153+
return next; // not a number
154+
}
155+
else // number like "-5", "-.6" etc, next is digit or '.'
156+
{
157+
s[++i] = c = next;
149158
}
150-
151-
s[i] = c;
152-
ungetch(c = next);
153159
}
154160
else
155161
{

chapter_4/exercise_4_04/stack.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,19 @@ int getop(char s[])
201201
if (c == '-')
202202
{
203203
int next = getch();
204-
if (!isdigit(next) && next != '.')
204+
if (next == '\n' || next == ' ' || next == '\t')
205205
{
206-
return next;
206+
ungetch(next);
207+
return c; // return '-' as operator
208+
}
209+
else if (!isdigit(next) && next != '.')
210+
{
211+
return next; // not a number
212+
}
213+
else // number like "-5", "-.6" etc, next is digit or '.'
214+
{
215+
s[++i] = c = next;
207216
}
208-
209-
s[i] = c;
210-
ungetch(c = next);
211217
}
212218
else
213219
{

chapter_4/exercise_4_05/math.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,19 @@ int getop(char s[])
215215
if (c == '-')
216216
{
217217
int next = getch();
218-
if (!isdigit(next) && next != '.')
218+
if (next == '\n' || next == ' ' || next == '\t')
219219
{
220-
return next;
220+
ungetch(next);
221+
return c; // return '-' as operator
222+
}
223+
else if (!isdigit(next) && next != '.')
224+
{
225+
return next; // not a number
226+
}
227+
else // number like "-5", "-.6" etc, next is digit or '.'
228+
{
229+
s[++i] = c = next;
221230
}
222-
223-
s[i] = c;
224-
ungetch(c = next);
225231
}
226232
else
227233
{

chapter_4/exercise_4_06/variables.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,19 @@ int getop(char s[])
258258
if (c == '-')
259259
{
260260
int next = getch();
261-
if (!isdigit(next) && next != '.')
261+
if (next == '\n' || next == ' ' || next == '\t')
262262
{
263-
return next;
263+
ungetch(next);
264+
return c; // return '-' as operator
265+
}
266+
else if (!isdigit(next) && next != '.')
267+
{
268+
return next; // not a number
269+
}
270+
else // number like "-5", "-.6" etc, next is digit or '.'
271+
{
272+
s[++i] = c = next;
264273
}
265-
266-
s[i] = c;
267-
ungetch(c = next);
268274
}
269275
else
270276
{

chapter_4/exercise_4_10/calculator.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,19 @@ int getop(char s[])
267267
if (c == '-')
268268
{
269269
int next = line[line_i++];
270-
if (!isdigit(next) && next != '.')
270+
if (next == '\n' || next == ' ' || next == '\t' || next == '\0')
271+
{
272+
--line_i;
273+
return '-';
274+
}
275+
else if (!isdigit(next) && next != '.') // not a number
271276
{
272277
return next;
273278
}
274-
275-
s[i] = c;
276-
c = next;
277-
--line_i;
279+
else
280+
{
281+
s[++i] = c = next;
282+
}
278283
}
279284
else
280285
{

0 commit comments

Comments
 (0)