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

This was in the ArrayStack class // Code based on: // Data Structures and Algori

ID: 3728468 • Letter: T

Question

This was in the ArrayStack class

// Code based on:
// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011.
//
#pragma once

#include <stdexcept>

using namespace std;

template <typename E>
class ArrayStack {
enum { DEF_CAPACITY = 100 }; // default stack capacity
public:
ArrayStack(int cap = DEF_CAPACITY); // constructor from capacity
int size() const; // number of items in the stack
bool empty() const; // is the stack empty?
const E& top() const; // get the top element
void push(const E& e); // push element onto stack
void pop(); // pop the stack
void printAll(); // print all elements on stack to cout
private:                                // member data
E* S; // array of stack elements
int capacity; // stack capacity
int t; // index of the top of the stack
};

template <typename E> ArrayStack<E>::ArrayStack(int cap)
: S(new E[cap]), capacity(cap), t(-1) { } // constructor from capacity

template <typename E> int ArrayStack<E>::size() const
{
return (t + 1);
} // number of items in the stack

template <typename E> bool ArrayStack<E>::empty() const
{
return (t < 0);
} // is the stack empty?

template <typename E> // return top of stack
const E& ArrayStack<E>::top() const {
if (empty()) throw length_error("Top of empty stack");
return S[t];
}

template <typename E> // push element onto the stack
void ArrayStack<E>::push(const E& e) {
if (size() == capacity) throw length_error("Push to full stack");
S[++t] = e;
}

template <typename E> // pop the stack
void ArrayStack<E>::pop() {
if (empty()) throw length_error("Pop from empty stack");
--t;
}

// print all elements on stack
template <typename E>
void ArrayStack<E>::printAll() {
if (empty()) throw length_error("Empty stack");
cout << "Elements in stack: ";
for (int i = t; i >= 0; i--)
cout << "{" << S[i] << "} ";
cout << endl;
}

b) Given the class ArrayStack, write a full program that would take in an arbitrary string of parentheses, curly braces, and square braces, and outputs if the string is balanced or not. The string is said to be balanced if all opening elements have a matching closing element of the same type. e.g. The string "( [ ] { } { } { ( )) )" would be considered balanced, but "( { ) [ ( ] )" would not. Refer to Section 5.1.7 (pg 204-205) in your textbook for the algorithm. Answer: #include #include #include "ArrayStack.h" int main() // set up the stack, string, and flag variables ArrayStack checker; string input = ""; bool unbalanced false // get the string cin >input; // run through the loop for (int i =0; i

Explanation / Answer

#include<iostream>

#include<stack>

#include<string>

using namespace std;

// This cheak whether two characters of opening and closing of same type

bool cheaking_pair(char opening,char closing)

{

if(opening == '(' && closing == ')') return true;

else if(opening == '{' && closing == '}') return true;

else if(opening == '[' && closing == ']') return true;

return false;

}

bool Cheaking_balance(string expresion)

{

stack<char> Strings;

for(int i =0;i<expresion.length();i++)

{

if(expresion[i] == '(' || expresion[i] == '{' || expresion[i] == '[')

Strings.push(expresion[i]);

else if(expresion[i] == ')' || expresion[i] == '}' || expresion[i] == ']')

{

if(Strings.empty() || !cheaking_pair(Strings.top(),expresion[i]))

return false;

else

Strings.pop();

}

}

return Strings.empty() ? true:false;

}

int main()

{

string expression;

cout<<"Enter an expression: "; // input expression from console

cin>>expression;

if(Cheaking_balance(expression))

cout<<"String is Balanced ";

else

cout<<"String is not Balanced ";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote