Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve the problem using C++. Modifu the following code to complete the task. Create the main.cpp file by yourself. Stack.h #ifndef STACK_H #define STACK_H const

Solve the problem using C++. Modifu the following code to complete the task. Create the main.cpp file by yourself.

Stack.h

#ifndef STACK_H #define STACK_H

const int MAX_ITEMS = 5;

using namespace std; class FullStack{

}; class EmptyStack{

public: string thrown_from; int errorID;

};

template class Stack{

private: int top; ItemType items[MAX_ITEMS];

public: Stack();

bool IsFull(); bool IsEmpty();

void MakeEmpty(); void Push(ItemType); void Pop(); ItemType Top();

};

#include "Stack.tpp" #endif

Stack.tpp

template Stack::Stack() { top = -1; }

template bool Stack::IsFull() { return (top == (MAX_ITEMS - 1)); }

template bool Stack::IsEmpty() { return (top == -1); }

template void Stack::MakeEmpty() { top = -1; }

template void Stack::Push(ItemType item) { if(IsFull()) throw FullStack();

items[top+1] = item; top++; }

template void Stack::Pop() { if(IsEmpty()){ EmptyStack e; e.thrown_from = "Top function"; e.errorID = 1; throw e; } top--; }

template ItemType Stack::Top() { if(IsEmpty()){ EmptyStack e; e.thrown_from = "Top function"; e.errorID = 2; throw e; } return items[top]; }

image text in transcribed

3. Use a stack to determine whether a string of parenthesis is balanced or not? If the string is not balanced, then print the longest substring that is balanced. If the length of the longest substring is less than 2, print Totally unbalanced. Test case 1: Input: () (((((() () () Output: () () () Test case 2: Input: 0 (0 ()) Output: Balanced Test case 3: Output: ) Output: Totally unbalanced

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions