Question: C++ 14 Program You are to write a function to determine if the brackets ([ ] ( ) { }) are properly nested, i. e.

C++ 14 Program

You are to write a function to determine if the brackets ([ ] ( ) { }) are properly nested, i. e. , balanced.The function will have a string as a parameter that can contain bracket and other characters. If the brackets are properly nested, the function will print the message The brackets in your string are properly matched Otherwise, it will print a message like this: Bracket mismatch at index : and or Unmatched bracket at index : Thus, if the string argument is "this is ( and has been { adjective here } )", then the output will be The brackets in your string are properly matched. On the other hand, if the input is "[({}])", then the output will be Bracket mismatch at index 4: ( and ] And if the input is "[({})]{(", then the output will be Unmatched bracket at index 6: {

You will be provided the files bracketMatcher.h and bracketMatcher.cpp. Do not modify the (very simple) .h file. The .cpp file is partially filled; your job is to fill in the remaining code for the function balanced(string str). You should, of course, create a test program that repeatedly inputs a string and outputs the bracket matcher's message until the user enters an empty string. The only file you will submit is bracketMatcher.cpp.

Here are the situations where a mismatch occurs. 1. The next character is a right bracket and the stack is empty; or 2. The next character is a right bracket and the top of the stack is not a match 3. You are at the end of the string and the stack is not empty; in this case the mismatch index is the length of the string.

*********************BracketMatcher.cpp*****************

#include #include #include #include "bracketMatcher.h"

string left_brackets("[({"); string right_brackets("])}");

bool is_left(char c) { return left_brackets.find(c) != string::npos; }

bool is_right(char c) { return right_brackets.find(c) != string::npos; }

bool matches(char L, char R) { assert(is_left(L) && is_right(R)); return left_brackets.find(L) == right_brackets.find(R); }

bool balanced(string str) {

// COMPLETE CODE HERE stack S;

}

*********************bracketMatcher.h***************************

# Ifndef _MBRACKET_

# define _MBRACKET_

#include

using namespace std;

bool is_left(char c);

bool is_right(char c);

bool matches(char L, char R);

bool balanced(string str);

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!