Question
(C++) 1) Using STL stack class, implement in C++ the following pseudocode function checkBraces(aString: string) that checks for balanced braces { } in a given
(C++)
1) Using STL stack class, implement in C++ the following pseudocode function checkBraces(aString: string) that checks for balanced braces { } in a given string / arithmetic expressions. Write a main() program to test the function.
2) Modify the given pseudocode to check if different types of braces, such as { }, ( ), [ ], match correspondently in order. And write an enhanced function checkAllTypeBraces(aString: string) in C++, including a main() function to test it.
// Checks the string aString to verify that braces match.
// Returns true if aString contains matching braces, false otherwise.
checkBraces(aString: string): boolean
{
aStack = a new empty stack
balancedSoFar = true
i = 0 // Tracks character position in string
while (balancedSoFar and i < length of aString)
{
ch = character at position i in aString i++ // Push an open brace
if (ch is a '{') aStack.push('{') // Close brace
else if
(ch is a '}') {
if (!aStack.isEmpty())
aStack.pop() // Pop a matching open brace else
// No matching open brace
balancedSoFar = false }
// Ignore all characters other than braces
}
if (balancedSoFar and aStack.isEmpty())
aString has balanced braces
else
aString does not have balanced braces
}
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