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

Write a C++ program prog.cpp along with Stack.h and Stack.cpp. * In Stack.h and

ID: 3634737 • Letter: W

Question

Write a C++ program prog.cpp along with Stack.h and Stack.cpp.

* In Stack.h and Stack.cpp, create a stack that stores char data. Doesn’t matter whether you implement static stack or dynamic stack.

* In prog.cpp, write a main function that asks the user to enter a series of parentheses and/or braces, then indicates whether or not they’re properly nested:

Example 1:

Enter parentheses and/or braces: ((){}{()})
Parentheses/braces are nested properly

Example 2:

Enter parentheses and/or braces: {{{()}}
Parentheses/braces are NOT nested properly

The Hint:

As the program reads characters, have it push each left parenthesis or left brace. When it reads a right parenthesis or brace, have it pop the stack and check if the popped item is a matching parenthesis or brace (If not, the parentheses/braces aren’t nested properly). When there is no more character to read, check if the stack is empty; if so, the parentheses/braces are matched.

Explanation / Answer

stack.h: header file class Stack { int MaxStack; int EmptyStack; int top; char* items; public: Stack(int); ~Stack(); void push(char); char pop(); int empty(); int full(); }; ------------------------------- // stack.cpp: stack functions #include "stack.h" Stack::Stack(int size) { MaxStack = size; EmptyStack = -1; top = EmptyStack; items = new char[MaxStack]; } Stack::~Stack() {delete[] items;} void Stack::push(char c) { items[++top] = c; } char Stack::pop() { return items[top--]; } int Stack::full() { return top + 1 == MaxStack; } int Stack::empty() { return top == EmptyStack; } ------------------------------- // stackmain.cpp: use stack #include #include "stack.h" int main() { Stack s(10); // 10 chars char ch; while ((ch = cin.get()) != ' ') if (!s.full()) s.push(ch); while (!s.empty()) cout
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