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
infix
expression
(e.g.
3 + 4 * 7
),
which
you
will
convert
to
postfix
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
effect:
•
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
file,
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
infix
(natural)
order.
You
must:
–
fill
in
the
infix
to
postfix()
function
to
construct
a
queue
of
tokens
arranged
in
postfix
order
(the
infix
queue
should
be
empty
when
you’re
done)
–
complete
the
evaluate
postfix()
function
to
evaluate
the
expression
stored
in
the
postfix
queue
and
return
the
answer
You
may
assume
the
input
is
a
valid
infix
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
(infix
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 Document
Answer:
Here’s
one
possible
implementation:
(only
functions
infix
to
postfix()
and
evaluate
postfix()
shown)
/
∗
c r e a t e s
a
queue
of
tokens
in
p o s t f i x
order
from
a
queue
of
tokens
in
i n f i x
order
∗
/
/
∗
postcondition :
returned
queue
contains
a l l
the
tokens ,
and
pqueue
infix
should
be
empty
∗
/
struct
token
queue
i n f i x
t o
p o s t f i x
(
struct
token
queue
∗
pqueue
infix
)
{
/
∗
TODO:
construct
postfix
−
ordered
queue
from
i n f i x
−
ordered
queue
;
a l l
tokens
from
i n f i x
queue
should
be
added
to
p o s t f i x
queue
or
freed
∗
/
p
expr
token
stack
top
=
NULL,
ptoken
;
struct
token
queue
queue
postfix
;
queue
postfix . front
=
queue
postfix . back
= NULL;
for
( ptoken
=
dequeue ( pqueue
infix ) ;
ptoken ;
ptoken
=
dequeue ( pqueue
infix ))
{
switch
( ptoken
−
>
type
)
{
case
OPERAND:
/
∗
operands
added
d i r e c t l y
to
p o s t f i x
queue
∗
/
enqueue(&queue
postfix
,
ptoken
) ;
break
;
case
OPERATOR:
/
∗
operator
added
to
stack
,
a f t e r
operators
of
higher
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