Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix the code! #include #include #define DefaultListSize 100 using namespace std; template class Stack { public: virtual Stack() {} virtual void clear() = 0;

Please fix the code!

#include #include

#define DefaultListSize 100

using namespace std;

template class Stack { public: virtual Stack() {} virtual void clear() = 0; virtual void push(const Elem&) = 0; virtual Elem pop() = 0; virtual const Elem& topValue() const = 0; virtual int length() const = 0; };

template class AStack : public Stack { private: int maxSize; int top; Elem* listArray;

public: AStack(int size = DefaultListSize) { maxSize = size; top = 0; listArray = new Elem[size]; }

AStack() { delete[] listArray; }

void clear() { top = 0; }

void push(const Elem& it) { assert(top != maxSize); cout << "Stack is full"; listArray[top++] = it; }

Elem pop() { assert(top != 0); cout << "Stack is empty"; return listArray[--top]; }

const Elem& topValue() const { assert(top != 0); cout << "Stack is empty"; return listArray[top - 1]; }

int length() const { return top; } };

bool checkPair(char opening, char closing) { if (opening == '(' && closing == ')') return true; else if (opening == '{' && closing == '}') return true; else if (opening == '[' && closing == ']') return true; return false; }

bool areParenthesisBalanced(string exp) { AStack stack;

for (int i = 0; i < exp.length(); i++) { if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[') stack.push(exp[i]); else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') { if (stack.length() == 0 || !checkPair(stack.pop(), exp[i])) return false; } }

return (stack.length() == 0); }

int main() { string exp1 = "[()]{}{[()()]()}"; if (areParenthesisBalanced(exp1)) cout << "The expression is balanced" << endl; else cout << "The expression is not balanced" << endl;

string exp2 = "[(])"; if (areParenthesisBalanced(exp2)) cout << "The expression is balanced" << endl; else cout << "The expression is not balanced" << endl;

return 0; }

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

Step: 3

blur-text-image

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

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

What are the roles and responsibilities of an external auditor?

Answered: 1 week ago

Question

Evaluate long-term care insurance.

Answered: 1 week ago

Question

5. Identify three characteristics of the dialectical approach.

Answered: 1 week ago

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago