Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use a stack data structure to determine whether the string parameter satisfies the condition described above. You may define your own stack class or use the STL stack class. Write a driver program (i.e. a function main()) to test your function.

Deliverables:

1. a copy of your function and the test driver program source codes in a text file

2. screen shots of your test driver program runs showing it meets the problem's specifications. This is what I've done so far: I've copied the material from the book as well as from some sources on the internet. Could you edit my code and write it in C++. Some of my code is in Java.

Respuesta :

Answer:

See attachment below

Program to check for matching symbols

// Program is written in C++

// Comments are used for explanatory purposes

// Program Starts here

#include<bits/stdc++.h>

using namespace std;

bool CheckParenthesis(string word)

{

int pos = 0;

char singlechar;

stack<char> stackchar;

for (pos=0; pos<word.length(); pos++)

{

//Check for opening paranthesis

if (word[pos]=='('||word[pos]=='['||word[pos]=='{')

{

stackchar.push(word[pos]);

continue;

}

if (stackchar.empty())

return false;

switch (word[pos])

{

//Check for closing paranthesis

case ')':

singlechar = stackchar.top();

stackchar.pop();

if (singlechar=='{' || singlechar=='[') {

return false; }

break;

case ']':

singlechar = stackchar.top();

stackchar.pop();

if (singlechar =='(' || singlechar == '{') {

return false; }

break;

}

case '}':

singlechar = stackchar.top();

stackchar.pop();

if (singlechar=='(' || singlechar=='[') {

return false; }

break;

}

return (stackchar.empty());

}

// Main Program Starts Here

int main()

{

//Declare String

string word;

cout<<"Enter some string texts:";

cin>>word;

//Check for matching symbols

if (CheckParenthesis(word))

cout << "Matching Symbols";

else

cout << "No Matching Symbols";

return 0;

}

Ver imagen MrRoyal
Ver imagen MrRoyal