Skip to content

Commit f989204

Browse files
authored
Fixed bugs in stack.c
Removed the else-block of "if (c == '-')" and restructured the clear command so the "stack is empty" message is not displayed an extra time after the c command has already been called. E.g if on the first line you type 'c', the message is displayed. On the next line, the message is displayed again even if you only typed "9 9+" because the previous code structure in the clear function forced the sp variable to go down to -1.
1 parent 933e245 commit f989204

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

chapter_4/exercise_4_04/stack.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,16 @@ void swap(void)
158158

159159
void clear(void)
160160
{
161-
do
161+
/* do
162162
{
163163
stack[sp] = 0.0;
164164
} while (sp--);
165+
*/
166+
// Replacing the above commented code with this one ensures messages of "stack empty" aren't displayed an extra time after c command is called in the program
167+
while (sp > 0)
168+
{
169+
stack[--sp] = 0.0;
170+
}
165171
}
166172

167173
int bufp = 0;
@@ -215,10 +221,11 @@ int getop(char s[])
215221
s[++i] = c = next;
216222
}
217223
}
218-
else
224+
// Remove this else block so that a value like 1200 in the program won't be mistaken is 100. This block will skip the second digit that is read in the number parsing process
225+
/* else
219226
{
220227
c = getch();
221-
}
228+
}*/
222229

223230
if (isdigit(c))
224231
{

0 commit comments

Comments
 (0)