CS2134_HW5

CS2134_HW5 - Homework #5 Due 11:59 p.m., Tuesday Nov 6 In...

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

View Full Document Right Arrow Icon
Homework #5 Due 11:59 p.m., Tuesday Nov 6 In class we studied an algorithm that converts infix expressions to postfix and an algorithm that evaluates postfix expressions. In this programming assignment, you’ll write a program to convert postfix expressions to (fully parenthesized) infix. It is closely related to the algorithm to evaluate postfix expressions. Rather than maintaining a stack of numbers, the program will maintain a stack of strings, each representing an infix expression. Scan the input from left to right, performing the following actions: 1. Read next token and determine whether it represents a number or an operator symbol 2. if the token is a number, create a string that represents the number and push it onto the stack 3. if the token is a binary operator symbol (+,-,*,/) 1. pop two strings (representing sub-expressions) from the stack 2. create a string of the form “( s1 op s2)”, where s1 and s2 are the popped strings and op is the new operator symbol 3. push the resulting string back onto the stack 4. if the token is a period, it denotes the end of the input When you’ve finished scanning the input, the stack should contain a single string representing the infix expression, provided that the input was a valid postfix expression. If the input expression is not a valid post-fix expression, your program should detect the problem, print an error message, and terminate. Implementation details: The input will come from a file “postfix.txt”. You may assume that the input file exists and that it contains a single expression.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2008 for the course CS 2134 taught by Professor Hellerstein during the Fall '07 term at NYU Poly.

Page1 / 4

CS2134_HW5 - Homework #5 Due 11:59 p.m., Tuesday Nov 6 In...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online