CS112_14_assignment 2A

CS112_14_assignment 2A - CS112 Handout #14 Spring 2003...

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

View Full Document Right Arrow Icon
Spring 2003 Yinglin Wang CS112 Homework 2A Due Thursday, March 27. Problem 1: Bracket Matching In the syntax of most programming languages, there are some characters that occur only in nested pairs, which are called bracketing operators. ANSI C, for example, defines the following bracketing operators: ( . . . ) [ . . . ] { . . . } In a properly formed program, these characters will be properly nested and matched. To determine whether this condition holds for a particular program, you can ignore all the other characters and look simply at the pattern formed by the parentheses, brackets, and braces. In a legal configuration, all the operators match up correctly, as shown in the following example: The following configurations, however, are illegal for the reasons stated: ( ( [ ] ) The line is missing a close parenthesis. ) ( The close parenthesis comes before the open parenthesis. { ( } ) The parentheses and braces are improperly nested. For this problem, your task is to write a recursive function bool IsBalanced(string str) that takes a string str from which all characters except the bracketing operators have been removed. The function should return TRUE if the bracketing operators in str are balanced, which means that they are correctly nested and aligned. If the string is not balanced, IsBalanced should return FALSE . Although there are many other ways to implement this operation, you should code your solution so that it embodies the recursive insight that a string consisting only of bracketing characters is balanced if and only if one of the following conditions holds: 1. The string is empty.
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 04/06/2010 for the course CS 112 taught by Professor Yinlinwang during the Spring '08 term at Shanghai Jiao Tong University.

Page1 / 4

CS112_14_assignment 2A - CS112 Handout #14 Spring 2003...

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