Question
Write a C++ program hw6.cpp that does the following: The program first creates an STL stack that stores char data. Then ask user to enter
Write a C++ program hw6.cpp that does the following:
The program first creates an STL stack that stores char data.
Then ask user to enter a series of parentheses and/or braces, then report whether or not theyre properly nested:
Example run:
Enter parentheses and/or braces: ((){}{()})
Parentheses/braces are nested properly
Continue? (y/n)
Enter parentheses and/or braces: {{{()}}
Parentheses/braces are NOT nested properly
Continue? (y/n)
Enter parentheses and/or braces: {({{()(){}}))}
Parentheses/braces are NOT nested properly
Continue? (y/n)
After each round, ask user if he/she wants to continue. If yes, go back to the previous step and take another string of parentheses/braces. If no, terminate program.
Hint: As the program reads characters, have it push each left parenthesis or left brace into the STL stack. When it reads a right parenthesis or brace, have it pop the stack and check if the popped item is a matching parenthesis or brace (If not, the parentheses/braces arent nested properly). When there is no more character to read, check if the stack is empty; if so, the parentheses/braces are matched.
If the user enters any character other than parenthesis or brace, simply ignore it. Therefore, entering a wrong character shouldnt lead to program malfunction.
Note: If you implement it without using STL stack, there will be a huge loss of points (even if the program works).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started