Implement stack using STL by extending Bag Interface class. Also use exception handling (for error handling) and file handling (to read in the inputs)
DS/.DS_Store
__MACOSX/DS/._.DS_Store
DS/ArrayBagTest.cpp
#include
#include
#include “ArrayBag.h”// For ADT bag
#include “ArrayBag.cpp”
#include “StackSTL.h”
#include “StackSTL.cpp”
#include
#include “BagInterface.h”
using namespace std;
int main()
{
/*ArrayBag
bags.add(‘a’);
bags.print();
bags.add(‘b’);
bags.print();
cout<<(bags.contains('a') ? "YES" : "NO")<
stack
bag->add(‘a’);
//bag->print();
return 0;
} // end main
__MACOSX/DS/._ArrayBagTest.cpp
DS/BagInterface.h
#ifndef BAG_INTERFACE_H
#define BAG_INTERFACE_H
#include
#include
#include
using namespace std;
template
class BagInterface {
public:
~BagInterface() {}
virtual int getCurrentSize(void) const = 0;
virtual bool isEmpty(void) const = 0;
virtual bool add(const ItemType& item) = 0;
virtual bool remove(const ItemType& item) = 0;
virtual void clear(void) = 0;
void print() const;
virtual int getFrequencyOf(const ItemType& item) const = 0;
virtual bool contains(const ItemType& item) const = 0;
virtual vector
};
#endif
__MACOSX/DS/._BagInterface.h
DS/ArrayBag.cpp
#include
#include “ArrayBag.h”
template
ArrayBag
{
} // default constructor
template
int ArrayBag
return itemCount;
} // end getCurrentSize
template
bool ArrayBag
return itemCount == 0;
} // end isEmpty
template
bool ArrayBag
bool hasRoomToAdd(itemCount < maxItems);
if (hasRoomToAdd) {
items[itemCount] = newEntry;
itemCount++;
}
return hasRoomToAdd;
} // end add function
template
bool ArrayBag
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem) {
itemCount–;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
} // end remove
template
void ArrayBag
itemCount = 0;
} // end clear
template
bool ArrayBag
int curIndex(0);
while (curIndex < itemCount) {
if (items[curIndex] == anEntry) {
return true;
}
curIndex++;
}
return false;
} // end contains
template
int ArrayBag
int frequency(0);
int curIndex(0);
while (curIndex < itemCount) {
if (items[curIndex] == anEntry) {
frequency++;
}
curIndex++;
}
return frequency;
} // end getFrequency
template
vector
vector
for (int i=0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end vector
template
void ArrayBag
{
for(int i = 0; i < itemCount; i++)
cout << items[i] << " ";
cout << endl;
} // end print
template
int ArrayBag
bool found = false;
int result = -1;
int searchIndex = 0;
while(!found && (searchIndex < itemCount))
{
if(items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
}
}
return result;
} // end getIndexOf
__MACOSX/DS/._ArrayBag.cpp
DS/ArrayBag.h
#ifndef ARRAY_BAG_H
#define ARRAY_BAG_H
#include
#include “BagInterface.h”
template
class ArrayBag : public BagInterface
public:
ArrayBag();
~ArrayBag(){};
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& item);
bool remove(const ItemType& item);
bool contains(const ItemType& anEntry) const;
void clear();
void print() const;
int getFrequencyOf(const ItemType& item) const;
vector
private:
static const int DEFAULT_CAPACITY = 6;
ItemType items[DEFAULT_CAPACITY];
int itemCount;
int maxItems;
int getIndexOf(const ItemType& item) const;
};
#endif
__MACOSX/DS/._ArrayBag.h
DS/StackSTL.h
#ifndef STACKSTL_H
#define STACKSTL_H
#include “BagInterface.h”
#include
template
class StackSTL : public BagInterface
{
public:
StackSTL();
bool add(const ItemType& newEntry);
/*bool remove(const ItemType& anEntry);
virtual ~StackSTL() { }
void print() const;*/
private:
stack
};
#include “StackSTL.cpp”
#endif
__MACOSX/DS/._StackSTL.h
DS/a.out
DS/StackSTL.cpp
#ifndef STACKSTL_CPP
#define STACKSTL_CPP
#include
#include
#include
#include
using namespace std;
template
StackSTL
template
bool StackSTL
{
stackBag.push(newEntry);
} //end push function
/*template < class ItemType >
bool StackSTL
{
stackBag.pop(anEntry);
} *///end pop
#endif
__MACOSX/DS/._StackSTL.cpp