Fund. CS II (CS152 2006S)

Homework 12: Matching Parens

Assigned: Wednesday, March 8, 2006
Due: 8:00 a.m., Tuesday, March 14, 2006

Summary: In this assignment, you will apply stacks to the problem of matching parentheses. vectors.

Purposes: To get you used to using stacks.

Contents:

The Assignment

In the lab on stacks, you wrote a program that used a stack to ensure that the parentheses in an expression are matched. Extend that program to print out the matches for a single-line expression. For example,

(oh (boy) (I am having) ((so) much) fun matching (parens))
    +---+
          +-----------+
                         +--+
                        +---------+
                                                 +------+
+--------------------------------------------------------+

Your program should also handle erroneous inputs. For example,

())((opened closed) (opened (opened closed) (opened
++
  ?EXTRA CLOSE
    +-------------+
                            +-------------+
                                            +------MISSING CLOSE
                    +------------------------------MISSING CLOSE
   +-----------------------------------------------MISSING CLOSE

Hints

Keep track of your position within the string. When you encounter an opening paren, push the position. When you encounter a closing paren, pop the position and print the string.

You will benefit from a helper method to print the string. You can use a for loop or build a really long string of spaces (and a really long string of dashes), and use substring to extract the proper number of spaces or dashes.

Submitting Your Work

Email me your program.

 

History

Wednesday, 8 March 2006 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 9 08:31:12 2006.
The source to the document was last modified on Wed Mar 8 09:11:55 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Homework/hw.12.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu