Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structures using C++: Write a program that takes an input a set of grouping

ID: 3572117 • Letter: D

Question

Data Structures using C++:

Write a program that takes an input a set of grouping symbols i.e. ( { [. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions { [ () ] } contains matching grouping symbols. However, the expression {() does not contain matching grouping symbols. Assume character input and the = as an indication of the end of the input.

Explanation / Answer

#include #include using namespace std; bool isMatchingPair(char character1, char character2) { if (character1 == '(' && character2 == ')') return 1; else if (character1 == '{' && character2 == '}') return 1; else if (character1 == '[' && character2 == ']') return 1; else return 0; } bool isBalanced(char exp[]) { int i = 0; stack myStack; while (exp[i]) { if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[') myStack.push(exp[i]); if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']') { if (myStack.empty()) return 0; char top=myStack.top(); myStack.pop(); if ( !isMatchingPair(top, exp[i]) ){ return 0; } } i++; } if (myStack.empty()) return 1; else return 0; } int main() { char input_character; char string[100000]; int counter=0; cin>>input_character; while(input_character!='='){ string[counter++]=input_character; cin>>input_character; } if(isBalanced(string)) cout