A.
It has no predetermined size. Since it is to be implemented as a
linked list, it should grow as big as there is memory.
A.
You must do it with a singly-linked list as specified.
A.
Your assumption is correct.
A.
The Stack functions don't return any sentinels on errors. The main
program must handle all errors before they reach the stack.
sufficient?stackT s1;
A.
Yes. Remember we have provided a sample main program that demonstrates
how to use the stack.
A.
Print an error message and exit the program, i.e., the same as we did
with the array implementation in lab when space could not be allocated.
A.
You don't read in all the arguments as a single string. You get each
argument as a separate string, in an array of strings (see Lab 5 -
Program Arguments).
A.
You'll have to look at the characters in the string to distinguish the
two. For example, a string that begins with "n" is obviously the "neg"
operator (or an error). Something that starts with a digit or "." is
a number (of course, there are other valid starts to numbers)....and so
on.
A.
You can't move it to the stack module either since we only allow the
functions we asked you to implement to be part of the interface (you
can, of course, have helper functions in the implementation file that
are not accessible in the main program).
What you'll need to do is either rewrite EnoughElements() so that it only uses the stack functions we asked you to write (or just get rid of it and do it in terms of the stack functions). It's really not difficult to determine whether there are enough elements for a unary or binary operator using the stack functions we are allowing.
argv
?
A.
You don't have to write any code to put the arguments into
argv
(and their count in argc
). Once you
declare them as parameters in main(), then when you run the program,
they will automatically have the arguments. You just need to
access them.
Try running a simple test program with some arguments and then print
out what's in argc, argv[0]
, etc. (Be careful though.
Obviously, you can't access argv[3]
if there are less than
4 arguments. Remember the 1st argument is the program name.)
argv[i]
is a single argument (as a string), how do I
access a single character in one of those arguments?
A.
Normally, if you have a string variable, you just use subscripts to access
an individual character, so add another subscript to argv[i].
A.
Returning from main() has the same effect as calling exit() from any other
function in the program--they both terminate the program and return a value
back to the operating system.
You should return the value 0 when all went well. Return a non-zero number (positive) when an error occurs. Either exit with the same error code for all errors (usually 1) or come up with different numbers for different classes of errors.
?% h4calc 7 5 / .....
A.
Assume all values are doubles (i.e., convert the strings to doubles),
even if they don't have a fractional part.
h4stack.c: In function `StackPush': h4stack.c:36: warning: implicit declaration of function `malloc' h4stack.c: In function `StackPop': h4stack.c:66: warning: implicit declaration of function `free'
A.
They indicate that you have not included the header file
stdlib.h
for the prototypes of malloc/free.
A.
You must free them. Setting the pointer to NULL without deallocating
the nodes would leave them sitting out in memory (until the program
ends on most systems). If fact, some weird systems might not even free
allocated memory when the program ends. That's why it is important you
do it.
We'll at least expect you to destroy all stacks before the program ends successfully. In reality, you should destroy stacks when the program ends due to errors too (i.e., under all conditions), but we won't be that picky this time.
gcc -ansi -pedantic -Wall -lm -g -c h4calc.c gcc: -lm: linker input file unused since linking not done gcc -ansi -pedantic -Wall -lm -g -o h4calc h4calc.o h4stack.o
A.
It's complaining that it's useless to "link in the math library" (-lm)
for commands that "only compile" (-c). -lm should only be with the
command that does the linking.
isdigit
, as in:
I get a "warning: subscript has type 'char'"?if (isdigit(argv[i][0])) ..
A.
It might be that there is a weird definition of isdigit() that requires
an int. Store the character in an int and then pass that to isdigit().
Should the above line print an error message saying too many operands or should it just output 4.2?h4calc 20.0 22.0 + 4.2
A.
Whatever is at the top of the stack at the end is the result. You need
not do anything with extra operands on the stack.
A.
Your program should do something reasonable under that case, but you
need not demonstrate it as one of your test runs.
when I use my make file?h4makefile:5: *** missing separator. Stop.
A.
Do the compile/link lines not begin with Tab's? Remember spaces won't
work.