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

Write a C++ program that will implement a stack class using the list class provi

ID: 663532 • Letter: W

Question

Write a C++ program that will implement a stack class using the list class provided below. Test your stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { } and/or < >. The following example of data:

                                ( )

                                [ ] ( ) { }

                                [ { ( ) } ]

                                < > <<< >>>

                                [ [ ) )

                                { ( ) [ ( ) ] }

Print out the input string and if the string is matched or mismatched. You will ignore characters that are not in the above set of matching characters. Use the input file [Stack2.txt] below to test the code. The files must be read in and not by user input.

List Class:

#include <iostream>
using namespace std;

class List
{
public:
   int CAPACITY;
   int *arr;
   int currentPos, size1;
   List()
   {
       CAPACITY = 20;
       arr = (int *)malloc(CAPACITY * sizeof(int *));
       size1 = 0;
       currentPos = 0;
   }
   List(const List &obj)
   {
       arr = obj.arr;
       CAPACITY = obj.CAPACITY;
       size1 = obj.size1;
   }

   bool empty()
   {
       return size1 > 0;
   }
   void front()
   {
       currentPos = 0;
   }
   void end()
   {
       currentPos = size1 - 1;
   }
   void prev()
   {
       if(currentPos == 0) cout << "Previous element is empty. ";
       else{
           currentPos--;
       }
   }
   void next()
   {
       if(currentPos == CAPACITY - 1) cout << "Next element is empty. ";
       else{
           currentPos++;
       }
   }
   int getPos()
   {
       return currentPos;
   }
   void setPos(int n)
   {
       currentPos = n;
   }
   void insertBefore(int ele)
   {
       if(currentPos == CAPACITY)
       {
           cout << "The list has reached max capacity. Cannot add more elements. ";
           return;
       }
       for(int i = currentPos - 1; i < size1; i++)
       {
           arr[i + 1] = arr[i];
       }
       arr[currentPos - 1] = ele;
       size1++;
   }

   void insertAfter(int ele)
   {
       if(currentPos == CAPACITY)
       {
           cout << "The list is full. Elements have NOT been added. ";
           return;
       }
       for(int i = currentPos + 1; i < size1; i++)
       {
           arr[i + 1] = arr[i];
       }
       arr[currentPos + 1] = ele;
       size1++;
   }
   int getElement()
   {
       return arr[currentPos];
   }
   int size()
   {
       return size1;
   }
   void replace(int n)
   {
       arr[currentPos] = n;
   }
   void erase()
   {
       for(int i = currentPos; i < size1; i++)
       {
           arr[i] = arr[i + 1];
       }
       size1--;
   }
   void clear()
   {
       for(int i = 0; i < size1; i++)
       {
           arr[i] = 0;
       }
       currentPos = 0;
       size1 = 0;
   }
   void reverse()
   {
       for(int i = 0; i < size1 / 2; i++)
       {
           int tmp = arr[i];
           arr[i] = arr[size1 - i];
           arr[size1 - i] = tmp;
       }
   }
   void swap(List a)
   {
       int *tmp = arr;
       arr = a.arr;
       a.arr = tmp;
   }

   friend ostream &operator<<(ostream &out, List &l)
   {
       for(int i = 0; i < l.size(); i++)
       {
           l.setPos(i);
           out << l.getElement() << " ";
       }
       out << " ";
       return out;
   }
   bool operator ==(List &l)
   {
       if(l.size() != size()) return false;
       for(int i = 0; i < size(); i++)
       {
           setPos(i);
           l.setPos(i);
           if(getElement() != l.getElement()) return false;
       }
       return true;
   }
   void operator=(List &l)
   {
       arr = l.arr;
   }
};

Stack2.txt:

(setq class '(((name Seymore) (eng 3 4.0) (mat 3 3.0) (his 3 4.0) (bio 3 2.0) (biol 1 4.0))
((name Ichahbod) (csc 3 3.0) (mat 3 4.0) (spe 2 4.0) (che 3 4.0) (chel 1 3.0) (lit 3 3.0))
((name Zackery) (mat 5 3.0) (eng 3 3.0) (jou 2 3.0) (phy 3 3.0) (phyl 1 4.0) (lit 2 4.0))
((name Tukerville) (soc 4 3.0) (mus 2 4.0) (jou 3 4.0) (geo 4 4.0) (geol 1 3.0) (eng 3 3.0))
((name Simonsays) (csc 3 3.0) (ast 3 4.0) (spe 3 4.0) (css 3 4.0) (spe 2 3.0) (dan 4 4.0))
((name Snicker) (eng 3 4.0) (phy 4 4.0) (csc 3 2.0) (cssl 1 4.0) (ped 2 3.0) (mat 3 3.0))
((name Glass) (mat 3 1.0) (eng 3 1.0) (ped 1 1.0) (bio 3 1.0) (biol 1 0.0) (che 3 1.0) (chel 1 1.0))))

Explanation / Answer

// Stack_Class.cpp

// store Stack2.txt in same folder in which Stack_Class.cpp is saved

// also the ouput file

#include<iostream>
#include<cstdlib>
#include <fstream>
using namespace std;
template< class T >
class Stack {

private:
int MAX;
int top;
T* items;

public:
   Stack(int size){
       MAX = size;
       top = -1;
       items = new T[MAX];
   }

   void push(T c){
       if(full()){
           cout << "Stack Full!" << endl;
           exit(1);
       }

       items[++top] = c;
   }

   T pop(){
       if(empty()){
           cout << "Stack Empty!" << endl;
           exit(1);
       }

       return items[top--];
   }

   int empty(){ return top == -1; }

   int full(){ return top+1 == MAX; }
};

int main()
{
   Stack<char> st(10000);
  
   string line;
   ifstream mfile("Stack2.txt");
   ofstream ofile("stack1.txt");
   if(mfile.is_open())
   {
       while(getline(mfile,line))
       {
           for(int i=0;i<line.length();i++)
           {
               if(line[i]=='('||line[i]==')'||line[i]=='{'||line[i]=='}'||line[i]=='['||line[i]==']'||line[i]=='<'||line[i]=='>')
               {
                   ofile<<line[i];
               }
           }
           ofile<<" ";
       }
   }
   ofile.close();
   ifstream printfile("stack1.txt");
   if(printfile.is_open())
   {
       while(getline (printfile, line))
       {
           cout<<line;
       }
   }
   printfile.close();
   ifstream outfile("stack1.txt");
   if(outfile.is_open())
   {
       while ( getline (outfile,line) )
   {
      
       for(int i=0;i<line.length();i++)
       {
           if(line[i]=='{'||line[i]=='('||line[i]=='['||line[i]=='<')
           {
               st.push(line[i]);  
           }
           else if(line[i]=='}'||line[i]==')'||line[i]==']'||line[i]=='>')
           {
               char temp;
               if(line[i]=='}')
               {
                   temp=st.pop();
                   if(temp=='{')
                   {
                  
                   }
                  
                          
               }
               else if(line[i]==')')
               {
                   temp=st.pop();
                   if(temp=='(')
                   {
                      
                   }
               }
               else if(line[i]=='>')
               {
                   temp=st.pop();
                   if(temp=='<')
                   {
                   }
               }
               else if(line[i]==']')
               {
                   temp=st.pop();
                   if(temp=='[')
                   {
                      
                   }
               }
               else
                   {
                   cout<<"MisMatched";
                   return 0;
                   }
          
              
          
           }
       }
  
   }
   ofile.close();
   cout<<"matched";
  
   }
}

/////////////////////////////////////////////////////

output:

(<{[]}>)

matched

/////////////////////////////////////

output:

((({{{{{}}}}}

mismatched

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