Massachusetts
Institute
of
Technology
Department
of
Electrical
Engineering
and
Computer
Science
6.087:
Practical
Programming
in
C
IAP
2010
Problem
Set
6
–
Solutions
Part
1:
Pointers
to
pointers.
Multidimensional
arrays.
Stacks
and
queues.
Out:
Wednesday,
January
20,
2010.
Due:
Friday,
January
22,
2010.
This
is
Part
1
of
a
twopart
assignment.
Part
2
will
be
released
Thursday.
Problem
6.1
In
this
problem,
we
will
implement
a
simple
“fourfunction”
calculator
using
stacks
and
queues.
This
calculator
takes
as
input
a
spacedelimited
in±x
expression
(e.g.
3 + 4 * 7
),
which
you
will
convert
to
post±x
notation
and
evaluate.
There
are
four
(binary)
operators
your
calculator
must
handle:
addition
(
+
),
subtraction
(

),
multiplication
(
*
),
and
division
(
/
).
In
addition,
your
calculator
must
handle
the
unary
negation
operator
(also

).
The
usual
order
of
operations
is
in
eﬀect:
•
the
unary
negation
operator

has
higher
precedence
than
the
binary
operators,
and
is
eval
uated
righttoleft
(
rightassociative
)
•
*
and
/
have
higher
precedence
than
+
and

•
all
binary
operators
are
evaluated
lefttoright
(
leftassociative
)
To
start,
we
will
not
consider
parentheses
in
our
expressions.
The
code
is
started
for
you
in
prob1.c
,
which
is
available
for
download
from
Stellar.
Read
over
the
±le,
paying
special
attention
to
the
data
structures
used
for
tokens,
the
stack,
and
queue,
as
well
as
the
functions
you
will
complete.
(a)
We
have
provided
code
to
translate
the
string
to
a
queue
of
tokens,
arranged
in
in±x
(natural)
order.
You
must:
–
±ll
in
the
infix
to
postfix()
function
to
construct
a
queue
of
tokens
arranged
in
post±x
order
(the
in±x
queue
should
be
empty
when
you’re
done)
–
complete
the
evaluate
postfix()
function
to
evaluate
the
expression
stored
in
the
post±x
queue
and
return
the
answer
may
assume
the
input
is
a
valid
in±x
expression,
but
it
is
good
practice
to
make
your
code
robust
by
handling
possible
errors
(e.g.
not
enough
tokens)
.
Turn
in
a
printout
of
your
code,
along
with
a
printout
showing
the
output
from
your
program
for
a
few
test
cases
(in±x
expressions
of
your
choosing)
to
demonstrate
it
works
properly.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentAnswer:
Here’s
one
possible
implementation:
(only
functions
infix
to
postfix()
and
evaluate
postfix()
shown)
/
∗
creates
a
queue
of
tokens
in
postfix
order
from
a
i n f i x
∗
/
/
∗
postcondition :
returned
contains
a l l
the
tokens ,
and
pqueue
infix
should
be
empty
∗
/
struct
token
i n f i x
t o
p o s t f i x
(
∗
)
{
/
∗
TODO:
construct
−
ordered
−
;
a l l
i n f i x
added
to
or
freed
∗
/
p
expr
stack
top
=
NULL,
ptoken
;
;
postfix . front
=
postfix . back
= NULL;
for
( ptoken
=
dequeue ( pqueue
infix ) ;
ptoken ;
=
infix ))
{
switch
−
>
type
)
{
case
OPERAND:
/
∗
operands
d i r e c t l y
∗
/
enqueue(&queue
,
) ;
break
;
OPERATOR:
/
∗
operator
,
after
operators
higher
precedence
are
moved
∗
/
while
(
&&
( op
precedences
[
−
>
value
.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '10
 DanielWeller
 Computer Science, Electrical Engineering

Click to edit the document details