{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

assignment6a - Massachusetts Institute of Technology...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}