Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a C++ program to implement Stack ADT using array and apply Stack to check whether the parentheses are matched. The program includes the following:
Write a C++ program to implement Stack ADT using array and apply Stack to check whether the parentheses are matched. The program includes the following:
- Define the Stack class template in the header file Stack.h.
// Stack.h #ifndef STACK_H #define STACK_H #include using namespace std; template class Stack { public: // Constructor Stack(); //Desctructor ~Stack(); // Makes the stack to the empty state. void make_empty(); // Checks if the stack is empty. bool empty() const; // Insert item in the stack. void push(const T& item); // Return the top element. const T& top() const; // Removes the element fron the front. void pop(); static const int CAPACITY = 50; private: int topOfStack;// -1 for empty stack T* theArray; }; #endif
- Please read the comments carefully and implement the Stack class template.
You can implement the Stack class template in the seperate file Stack.cpp.
// Stack.cpp #include "Stack.h" template Stack::Stack() { topOfStack = -1; theArray = new T[CAPACITY]; } // add other member functions // ....
You also can put the implementation of the Stack class template in Stack.h.
// Stack.h #ifndef STACK_H #define STACK_H #include using namespace std; template class Stack { public: // Constructor Stack() { topOfStack = -1; theArray = new T[CAPACITY]; } // add other member functions // .... static const int CAPACITY = 50; private: int topOfStack; T* theArray; }; #endif
The main function is contained in the file lab04.cpp.
// lab04.cpp #include #include "Stack.h" #include "Stack.cpp" // add if the interface and implementation are seperate int main() { .... }
- The main function checks whether the righ or opening parentheses are correspond to the left or closing parentheses.
- Declare a stack which stores charactors.
- Prompty the user to enter a character, stop entering the character when the user enter x.
- If the character entered by user is a left parenthesis '(', push it onto the stack.
- If the character entered by user is a right parenthesis ')',
- if the stack is not empty, pop the stack;
- if the stack is empty, report the unbalance information and break (exit while loop).
- After the user completes entering, if the stack is empty, report the balance information, otherwise, report the unbalance information.
The expected result:
- Stack.h: the header file.
- Stack.cpp if the interface and implementation are seperate: the implementation file of the class Course.
- lab04.cpp: the client test file containing main() funcion.
- lab04result: the script file which captures the result.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started