Question

Write a function that takes a string parameter and determines whether the string contains matching grouping...

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.

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;

// Stack template
template <class T>
class Queue
{
private:
   T *queueArray;
   int queueSize;
   int front;
   int rear;
   int numItems;
public:
   //Constructor
   Queue(int);

   //Copy constructor
   Queue(const Queue &);

   //Destructor
   ~Queue();

   //Queue operations
   void enqueue(T);
   void dequeue(T &);
   bool isEmpty() const;
   bool isFull() const;
   void clear();
};

//This constructor creates an empty queue of a specified size
template <class T>
Queue<T>::Queue(int s)
{
   queueArray = new T[s];
   queueSize = s;
   front = -1;
   rear = -1;
   numItems = 0;
}
//Copy constructor
template <class T>
Queue<T>::Queue(const Queue &obj)
{
   //Allocate the queue array.
   queueArray = new T[obj.queueSize];

   //Copy the other object's attributes.
   queueSize = obj.queueSize;
   front = obj.front;
   rear = obj.rear;
   numItems = obj.numItems;

   //Copy the other object's queue array.
   for (int count = 0; count < obj.queueSize; count++)
       queueArray[count] = obj.queueArray[count];
}

//Destructor
template <class T>
Queue<T>::~Queue()
{
   delete[] queueArray;
}
//Function enqueue inserts a value at the rear of the queue.

tmeplate <class T>
void Queue<T>::enqueue(T item)
{
   if (isFull())
       cout << "The queue is full.\n";
   else
   {
       //Calculate the new rear position
       rear = (rear + 1) % queueSize;
       //Insert new item
       queueArray[rear] = item;
       // Update item count
       numItems++;
   }
}

//Function dequeue removes the value at the front of the queue and copies t into num

template <class T>
void Queue<T>::dequeue(T &item)
{
   if (isEmpty())
       cout << "The queue is empy. \n";
   else
   {
       //Move front
       front = (front + 1) % queueSize;
       //Retrieve the front item
       item = queueArray[front];
       // Update item count
       numItems--;
   }
}

template <class T>
bool Queue<T>::isEmpty() const
{
   bool status;

   if (numItems)
       status = false;
   else
       status = true;

   return status;
}
template <class T>
bool Queue<T>::isFull() const
{
   bool status;

   if (numItems < queueSize)
       status = false;
   else
       status = true;

   return status;
}
template <class T>
void Queue<T>::clear()
{
   front + queueSize - 1;
   rear = queueSize - 1;
   numItems = 0;
}

//Driver program to test the above functions
public static void main(String[] args)

{

   String exp1 = "{a(b+ac)d[xy],}, kab * cd";

   if (areParenthesisBalanced(exp1.toCharArray()))

       System.out.println("Balanced ");

   else

       System.out.println("Not Balanced ");

   String exp2 = "ac)cd(e(k, xy{za(dx)k";

   if (areParenthesisBalanced(exp2.toCharArray()))
       System.out.println("Balanced ");
   else

       System.out.println("Not Balanced ");

}
}
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program1.cpp

---------------------------------------------------------------------------------------------------------

#include<iostream>
#include<stack>
#include<string>
using namespace std;
// Function to check whether two characters are opening
// and closing of same type.
bool ArePair(char opening,char closing)
{
   if(opening == '(' && closing == ')') return true;
   else if(opening == '{' && closing == '}') return true;
   else if(opening == '[' && closing == ']') return true;
   return false;
}

//Function to check having matching symbols
bool haveMatchingSymbol(string exp)
{
   stack<char> S; //Taking STL stack class as mentioned that we can take this too
   for(int i =0;i<exp.length();i++)
   {
       if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
           S.push(exp[i]);
       else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
       {
           if(S.empty() || !ArePair(S.top(),exp[i]))
               return false;
           else
               S.pop();
       }
   }
   return S.empty() ? true:false;
}


//Driver function to check the conditions
int main()
{
   string expression;
   cout<<"Enter an expression: "; // input expression from STDIN/Console
   cin>>expression;
   if(haveMatchingSymbol(expression))
       cout<<"Balanced and having balancing condition\n";
   else
       cout<<"Not Balanced\n";
}

-----------------------------------------------------------------------------------------------------------------------------------------

Screenshots of test driver program:-

98 %. Activities XTerm Thu Jun 13, 01:23 /home/aakash/pr Enter an expression: a(biac) d[ xylg] Balanced and having balancing

Activities XTerm Thu Jun 13, 01:22 /home/aakash/pr Cnter an exprossion: kabxc Balanced and having balancing condition executi

4 98 %- Activities XTerm Thu Jun 13, 01:29 /home/aakash/pr nter an expronsion: oc) cd(o(k.xy(za (dx)k. Not. Balancnd ProconsActivities XTerm Thu Jun 13, 01:30 /home/aakash/pr Lnter an expronsion: (o (blac) d) Not. Balancnd Process returned Press ENT

Add a comment
Know the answer?
Add Answer to:
Write a function that takes a string parameter and determines whether the string contains matching grouping...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines...

    Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly braces { }, and brackets [].  For example, the string {a(b+ac)d} 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). Write...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of...

    PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of the queue- please add comments EXISTING CODE: #include // this allows you to declare and use strings #include using namespace std; struct qNode {   string data;   qNode* next;   qNode* prev; }; class CBQueue {   public:     CBQueue(); int CBQueue::getSize( ); bool CBQueue::isEmpty( );   private:     qNode* front;     qNode* rear;     int size; }; #include "CBQueue.h" CBQueue::CBQueue() { front = NULL; rear = NULL; size = 0; }...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT