0% found this document useful (0 votes)
27 views1 page

Parentheses Match

The document describes a problem of checking if parentheses in an arithmetic expression match. It proposes using a stack data structure to push left parentheses onto the stack and pop items to check against right parentheses. The user is prompted to input a string, which a method will check the parentheses in and return true if they match or false if they don't. Test cases with sample inputs and outputs are provided.

Uploaded by

justsayhello
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Parentheses Match

The document describes a problem of checking if parentheses in an arithmetic expression match. It proposes using a stack data structure to push left parentheses onto the stack and pop items to check against right parentheses. The user is prompted to input a string, which a method will check the parentheses in and return true if they match or false if they don't. Test cases with sample inputs and outputs are provided.

Uploaded by

justsayhello
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Parentheses Match

Problem Statement
A stack is data structure for temporary storage during a program. It is used whenever the data processing works according to a Last-In, First-Out (LIFO) order. As an example, we use a stack to help determine whether or not there are matching grouping symbols (parentheses, brackets, curly braces, etc.) in a given arithmetic expression.

Algorithm
Prompt the user to enter an expression. Ignoring all digits and operands, push the leftgrouping symbols on the stack. When a right-grouping symbol is encountered, pop the stack (what if the stack is empty?). Compare the two characters. If they don't match, return false. Somehow, determine the end of this process and return true if the expression has passed all the matches.

Assignment
Prompt the user to input a string and output whether the parentheses match. Test for matching <>, {}, [], and (). Use the stack methods in java.util.Stack<E>. Since the Java Collections classes are always classes of objects, not of primitives, you will likely be processing a stack of String objects.
public class ParenMatch { public static final String LEFT = "([{<"; public static final String RIGHT = ")]}>"; public static void main(String[] args) { } public static boolean check(String s) { } }

Test Data
input
5+7 (5+7) )5+7( ((5+7)*3) [(5+7)*]3 <{5+7}*3> (5+7)*3 5+(7*3) ((5+7)*3 [(5+7]*3) [(5+7)*3]) ([(5+7)*3]

output
5+7 is good (5+7) is good No. Bad. )5+7( ((5+7)*3) is good. [(5+7)*]3 is good. <{5+7}*3> is good. (5+7)*3 is good. 5+(7*3) is good. No. Bad. ((5+7)*3 No. Bad. [(5+7]*3) No. Bad. [(5+7)*3]) No. Bad. ([(5+7)*3]

oops! the algorithm isn't very strong

You might also like