Tải bản đầy đủ (.pdf) (8 trang)

Problem Set 6 – Solutions Part 1: Pointers to pointers. Multidimensional arrays. Stacks and queues.

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (264.15 KB, 8 trang )

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 two-part assignment. Part 2 will be released Thursday.
Problem 6.1
In this problem, we will implement a simple “four-function” calculator using stacks and queues.
This calculator takes as input a space-delimited 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 right-to-left (right-associative)
• * and / have higher precedence than + and ­
• all binary operators are evaluated left-to-right (left-associative)
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
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 tok en s in p o s t f i x o r d e r from a queue o f tok e ns in i n f i x o r d e r ∗/
/
∗ po s t c o n d i t i o n : re t ur n ed queue c o n t a i n s a l l t h e tokens , and pq u e u e i n f i x shou ld be
empty
∗/
struct tok e n q u eue i n f i x t o p o s t f i x ( str uct tok en q ueue ∗ p q u e u e i n f i x ) {
/∗ TODO: c o n s t r u c t p o s t f i x −o rd er ed queue from i n f i x −o r d e r e d queue ;
a l l toke ns from i n f i x queue shou ld be added to p o s t f i x queue or f r e e d
∗/
p ex p r to k e n s t a c k t o p = NULL, ptoken ;
struct tok e n q u eue q u e u e p o s t f i x ;
q u e u e p o s t f i x . f r o n t = q u e u e p o s t f i x . back = NULL;
for ( ptoken = dequeue ( p q u e u e i n f i x ) ; ptoken ; ptoken = dequeue ( p q u e u e i n f i x ) ) {
switch ( ptoken−>type ) {
case OPERAND:
/∗ o p e r a n d s added d i r e c t l y to p o s t f i x queue ∗/
enqueue(& q u e u e p o s t f i x , ptoken ) ;
break ;
case OPERATOR:
/∗ o p e r a t o r added to stac k , a f t e r o p e r a t o r s o f hi g h e r
pr ec ed en c e are moved t o queue
∗/
while ( s t a c k t o p &&
( o p p r e c e d e n c e s [ s t a c k t o p
−>v a l u e . o p c o de ] >
o p p r e c e d e n c e s [ ptoken−>v al ue . o p co de ] | |

( o p p r e c e d e n c e s [ s t a c k t o p −>v a lu e . o p co d e ] ==
o p p r e c e d e n c e s [ ptoken−>v al ue . o p co de ] &&
o p a s s o c i a t i v i t y [ op p r e c e d e n c e s [ ptoken−>v a l u e . o p code ] ] == LEFT) ) )
enqueue(& q u e u e p o s t f i x , pop(& s t a c k to p ) ) ;
push(& s t a c k t op , ptoken ) ;
break ;
defaul t : /∗ o th e r t o ke ns i g n o r e d ∗/
f r e e ( ptoken ) ;
break ;
}
}
while ( s t a c k to p ) /∗ pop r e main ing op e r a t o r s o f f s t a c k ∗/
enqueue(& q u e u e p o s t f i x , pop(& s t a c k to p ) ) ;
return q u e u e p o s t f i x ;
}
/∗ e v a l u t e s the p o s t f i x e x p r e s s i o n s t o r e d i n the queue ∗/
/
∗ p o s t c o n d i t i o n : r e t u r n e d v a l u e i s f i n a l answer , and p q u e u e p o s t f i x sho u ld be empty ∗/
double e v a l u a t e p o s t f i x ( struct to k en q ueu e ∗ p q u e u e po s t f i x ) {
/∗ TODO: p r o c e s s p o s t f i x −o r d e r e d queue and r e t u r n f i n a l answer ;
a l l toke ns from p o s t f i x
−o r d e r e d queue i s f r e e d ∗/
double ans = 0 . ;
p ex p r to k e n s t a c k v a l u e s = NULL, ptoken , p valu e ;
double operands [ 2 ] ; /
∗ max two o p e r a n d s ∗/
union t o k e n v a l u e va l u e ;
int i ;
while ( ( ptoken = dequeue ( p q u e u e p o s t f i x ) ) ) {
switch ( ptoken−>type ) {

case OPERAND:
/∗ o p e r a n d s alway s pushed to s t a c k ∗/
push(& s t a c k va l u e s , ptoken ) ;
break ;
case OPERATOR:
2
/∗ pop operands from s t a c k ∗/
for ( i = 0 ; i < op ope r and s [ p token−>v al ue . o p co de ] ; i ++) {
i f ( ( p v alue = pop(& s t a c k v a l u e s ) ) ) {
operands [ i ] = pvalue−>v a l u e . operand ;
f r e e ( pvalue ) ; /
∗ done with token ∗/
e l s e }
goto e r r o r ;
}
/∗ p r o c e s s operands ac c o r d i n g to opcode ∗/
/
∗ n o t e operands ar e popped i n r e v e r s e o rd e r ∗/
switch ( ptoken−>v a lu e . o p cod e ) {
case ADD:
va lu e . operand = operands [1 ]+ operands [ 0 ] ;
break ;
case SUBTRACT:
va lu e . operand = operands [1]
− operands [ 0 ] ;
break ;
case MULTIPLY:
va lu e . operand = operands [ 1 ]
∗ o p e r a n d s [ 0 ] ;
break ;

case DIVIDE :
va lu e . operand = operands [ 1 ] / operands [ 0 ] ;
break ;
case NEGATE:
va lu e . operand =
−operands [ 0 ] ;
}
/∗ push new token with op e r a t o r r e s u l t t o st a c k ∗/
push(& s t a c k va l u e s , new token (OPERAND, v a l u e ) ) ;
defaul t :
f r e e ( ptoken ) ; /
∗ f r e e token ∗/
break ;
}
}
/∗ r e t u r n va l u e i s on top o f st a c k ( shou l d be l a s t va l u e on s t a c k ) ∗/
i f ( s t a c k v a l u e s )
ans = st a c k v a l u e s
−>v a l u e . operand ;
cle a nup :
/∗ f r e e any r e mai n ing t o ke ns ∗/
while ( ( ptoken = dequeue ( p q u e u e p o s t f i x ) ) )
f r e e ( ptoken ) ;
while ( ( p valu e = pop(& s t a c k v a l u e s ) ) )
f r e e ( p value ) ;
return ans ;
e r r o r :
f p u t s ( "Error evaluating the expression .\ n " , s t d e r r ) ;
goto c l ean up ;
}

(b) Now, an infix calculator really is not complete if parentheses are not allowed. So, in this
part, update the function infix to postfix() to handle parentheses as we discussed in
class. Note: your postfix queue should contain no parentheses tokens. Turn in a printout of
your code, along with a printout showing the output from your program for a few test cases
utilizing parentheses.
3
Answer: Here’s an implementation: (only function infix to postfix() shown)
/∗ c r e a t e s a queue o f to k e n s i n p o s t f i x o r d er from a queue o f t o k en s i n i n f i x o r de r ∗/
/
∗ p o s t c o n d i t i o n : r et u rn e d queue c o n t a i n s a l l the t o kens , and p q u e u e i n f i x shou l d be
empty
∗/
struct tok e n q u eue i n f i x t o p o s t f i x ( str uct tok en q ueue ∗ p q u e u e i n f i x ) {
/∗ TODO: c o n s t r u c t p o s t f i x −o rd er ed queue from i n f i x −o r d e r e d queue ;
a l l toke ns from i n f i x queue shou ld be added to p o s t f i x queue or f r e e d
∗/
p ex p r to k e n s t a c k t o p = NULL, ptoken ;
struct tok e n q u eue q u e u e p o s t f i x ;
q u e u e p o s t f i x . f r o n t = q u e u e p o s t f i x . back = NULL;
for ( ptoken = dequeue ( p q u e u e i n f i x ) ; ptoken ; ptoken = dequeue ( p q u e u e i n f i x ) ) {
switch ( ptoken−>type ) {
case OPERAND:
/∗ o p e r a n d s added d i r e c t l y to p o s t f i x queue ∗/
enqueue(& q u e u e p o s t f i x , ptoken ) ;
break ;
case OPERATOR:
/∗ o p e r a t o r added to stac k , a f t e r o p e r a t o r s o f hi g h e r
pr ec ed en c e are moved t o queue
∗/
while ( s t a c k t o p && st a c k to p −>t ype == OPERATOR &&

( o p p r e c e d e n c e s [ s t a c k t o p
−>v a l u e . o p c o de ] >
o p p r e c e d e n c e s [ ptoken−>v al ue . o p co de ] | |
( o p p r e c e d e n c e s [ s t a c k t o p −>v a lu e . o p co d e ] ==
o p p r e c e d e n c e s [ ptoken−>v al ue . o p co de ] &&
o p a s s o c i a t i v i t y [ op p r e c e d e n c e s [ ptoken−>v a l u e . o p code ] ] == LEFT) ) )
enqueue(& q u e u e p o s t f i x , pop(& s t a c k to p ) ) ;
push(& s t a c k t op , ptoken ) ;
break ;
case LPARENS:
/∗ pushed to o p e r a t o r st a c k ∗/
push(& s t a c k t op , ptoken ) ;
break ;
case RPARENS:
/∗ pop o p e r a t o r s o f f s t a c k u n t i l l e f t p a r e n t h e s e s r e ach e d ∗/
f r e e ( ptoken ) ; /
∗ p a r e n t h e s e s not i n c l u d e d i n p o s t f i x queue ∗/
while ( ( ptoken = pop(& s t a c k to p ) ) )
{
i f ( ptoken−>t ype == LPARENS) {
f r e e ( ptoken ) ;
break ;
}
enqueue(& q u e u e p o s t f i x , ptoken ) ;
}
}
}
while ( s t a c k to p ) /∗ pop r e main ing op e r a t o r s o f f s t a c k ∗/
enqueue(& q u e u e p o s t f i x , pop(& s t a c k to p ) ) ;
return q u e u e p o s t f i x ;

}
Problem 6.2
A useful data structure for storing lots of strings is the “trie.” This tree structure has the special
property that a node’s key is a prefix of the keys of its children. For instance, if we associate a node
with the string “a,” that node may have a child node with the key “an,” which in turn may have a
child node “any.” When many strings share a common prefix, this structure is a very inexpensive
4
way to store them. Another consequence of this storage method is that the trie supports very fast
searching – the complexity of finding a string with m characters is O(m).
(root)
pointer array of childre



✠



❇◆
"a" "c"



✂✌



❇◆
"an" "ca"




✂✌



❅❘



❇◆
"and" "ant" "cat"
Figure 6.2-1: Trie structure (translations not shown). For each node, its key (e.g. “and”) is not
explicitly stored in the node; instead, the key is defined by the node’s position in the tree: the key
of its parent node + its index in that parent’s pointer array of children.
In this problem, you will utilize a trie structure and to implement a simple one-way English-
to-French dictionary. The trie structure, shown in Figure
6.2-1, consists of nodes, each of which
contains a string for storing translations for the word specified at that node and an array of pointers
to child nodes. Each node, by virtue of its position in the trie, is associated with a string; in the
dictionary context, this string is the word (or part of a word) in the dictionary. The dictionary may
contain multiple translations for the same word; in this case, words should be separated by commas.
For example: the word like, which has two meanings, could translate as comme, a preposition, or
as aimer, a verb. Thus, the translation string should be “comme,aimer.” To get you started, we’ve
provided code in prob2.c, which can be downloaded from Stellar. You will need to:
• fill in the helper functions new node(), delete node()
• complete the function add word(), which adds a word to the trie
• complete the function lookup word(), which searches the trie for a word and returns its
translation(s)
Once your code is working, run a few test cases. Hand in a copy of your code, and a printout of

your program running in gdb, with a few example translations to demonstrate functionality.
5

×