assignment6a

Assignment6a

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s ∗ / w hile ( ( ptoken = d equeue ( p q u e u e p o s t f i x ) ) ) f r e e ( ptoken ) ; while ( ( p v a l u e = p op(& s t a c k v a l u e s ) ) ) f r e e ( p v a l u e ) ; return a ns ; e r r o r : f p u t s ( " Error e valuating t he e xpression .\ n" , s t d e r r ) ; goto c l e a n u p ; } (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 q ueue o f t o k e n s i n p o s t f i x o r d e r f rom a q ueue o f t o k e n s i n i n f i x o r d e r ∗ / / ∗ p o s t c o n d i t i o n : r e t u r n e d q ueue c o n t a i n s a l l t h e t o k e n s , a nd p q u e u e i n f i x s h o u l d b e e mpty ∗ / s truct t o k e n q u e u e i n f i x t o p o s t f i x ( s truct t o k e n q u e u e ∗ 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 r d e r e d q ueue f rom i n f i x −o r d e r e d q ueue ; a l l t o k e n s f rom i n f i x q ueue s h o u l d b e a dded t o p o s t f i x q ueue o r f r e e d ∗ / p e x p r t o k e n s t a c k t o p = NULL, p token ; struct t o k e n q u e u e 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; f o r ( ptoken = d equeue ( p q u e u e i n f i x ) ; p token ; p token = d equeue ( p q u e u e i n f i x ) ) { switch ( ptoken −>type ) { case OPERAND: / ∗ o p e r a n d s a dded d i r e c t l y t o p o s t f i x q ueue ∗ / e nqueue(& q u e u e p o s t f i x , p token ) ; break ; case OPERATOR: / ∗ o p e r a t o r a dded t o s t a c k , a f t e r o p e r a t o r s o f h i g h e r p r e c e d e n c e a r e moved t o q ueue ∗ / while ( s t a c k...
View Full Document

This document was uploaded on 03/17/2014 for the course EECS 6.087 at MIT.

Ask a homework question - tutors are online