Question: 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]; }

Solve the problem using C++. Modifu the following code to complete the

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!