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: 663412 • 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. There are two input files that have been provided to test your code with, StackTestInput.txt and Stack2.txt.

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;
   }
};

StackTestInput.txt File:
#include <stdio.h>
#include <iostream.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int getword( char *line, char *word);
int getline( char *line );

int getline( char *line )
{
    char *tline = line;
    int c;

    while (( (c = getchar()) != ' ') && ( c != EOF ))
          *tline++ = c;

    *tline = '';
    if ( c == EOF ) return 0;
  
    return (strlen(line));
}

int getword( char *line, char *word)
{
    char *tline=line, tword = word;

    while ( !isalnum( *tline ) )
          tline++;

    while ( isalnum( *tline ) )
    {
       *tword++ = *tline;
       tline++;
    }
    *tword = '';

    return strlen(word) > 0 ? strlen(word) : 0;
}

void main()
{
   char *line, *word, *tline;

   line = (char *) malloc(80);
   word = (char *) malloc(20);

   while ( getline(line) )
   {
       tline = line;
       while ( getword(tline,word))
       {
          tline += strlen(word);
          cout << word << endl;
       }
   }
}

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

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <string>

using namespace std;

vector<int> v1;
bool iserror = false;
void check(string s){
   for (int i=0; i<s.length(); i++){
       char find = s[i];
       if (find == '(' || find == '{' || find =='['){
           v1.push_back(find);
       }
       else if (find == ')' || find == '}' || find == ']') {
           char found = v1[v1.size()-1];
           if (v1.size()==0){
               cout << "INVALID DELIMITERS IN SEQUENCE AT " << i+1 << " ";
               iserror=true;
               break;
           }
           else if (find==')'){
               if (found!='('){
                   cout << "INVALID DELIMITERS IN SEQUENCE AT " << i+1 << " ";
                   iserror=true;
                   break;
               }
               else {
                   v1.pop_back();
               }
           }
           else if (find == '}'){
               if (found!='{'){
                   cout << "INVALID DELIMITERS IN SEQUENCE AT " << i+1 << " ";
                   iserror=true;
                   break;
               }
               else {
                   v1.pop_back();
               }
           }
           else if (find == ']'){
               if (found!='['){
                   cout << "INVALID DELIMITERS IN SEQUENCE AT " << i+1 << " ";
                   iserror=true;
                   break;
               }
               else {
                   v1.pop_back();
               }
           }
       }
   }
   if (v1.size()!=0){
       cout << "MORE NUMBER OF LIMITS THAN DELIMITS " << endl;
       iserror=true;
   }
   if (iserror==true){
       cout << "INCORRECT SEQUENCE!!!!! " << endl; return;
   }
   else {
       cout << "OK!!!!! CORRECT SEQUENCE "; return;
   }
}

int main(){
   string note;
   int option;
   while (true){
       cout << "Choose Desire Option " << endl;
       cout << "For Delimiter Matching : 1 " << endl;
       cout << "QUIT : 2 " << endl;
       cin >> option;
       if (option==1){
           cout << "TYPE A SEQUENCE CONTAINING DELIMITERS (DO NOT PROVIDE ANY SPACE IN SEQUENCE) " << " ";
           cout << endl;
           cin >> note;
           check(note);
       }
       else
           break;
   }
   return 0;
}

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