/** Header file for an array-based implementation of the ADT bag. @file ArrayBag.h */
#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include \"BagInterface.h\"
template class ArrayBag : public BagInterface
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that // contains the given target or -1,
if the array does not contain // the target.
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector toVector() const;
}; // end ArrayBag
//#include \"ArrayBag.cpp\" // /** Implementation file for the class ArrayBag. @file
ArrayBag.cpp */
#include \"ArrayBag.h\"
#include template
ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{ } // end default constructor
template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
template bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry; itemCount++;
} // end if return hasRoomToAdd;
} // end add
/* // STUB template bool ArrayBag::remove(const ItemType& anEntry) { return false; // STUB
} // end remove
*/ template bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if return
canRemoveItem;
} // end remove /*
// STUB template void ArrayBag::clear() { // STUB } // end clear
*/ template void ArrayBag::clear() { itemCount = 0; } // end clear
template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1: First version
template bool ArrayBag::contains(const ItemType& target) const
{
return getFrequencyOf(target) > 0;
} // end contains
// ALTERNATE 2: Second version
template bool ArrayBag::contains(const ItemType& anEntry) const
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount))
{
if (anEntry == items[curIndex])
{
found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains
*/ temp.
Header file for an array-based implementation of the ADT bag. @f.pdf
1. /** Header file for an array-based implementation of the ADT bag. @file ArrayBag.h */
#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include "BagInterface.h"
template class ArrayBag : public BagInterface
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that // contains the given target or -1,
if the array does not contain // the target.
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector toVector() const;
}; // end ArrayBag
//#include "ArrayBag.cpp" // /** Implementation file for the class ArrayBag. @file
ArrayBag.cpp */
#include "ArrayBag.h"
#include template
ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{ } // end default constructor
template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
template bool ArrayBag::add(const ItemType& newEntry)
{
2. bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry; itemCount++;
} // end if return hasRoomToAdd;
} // end add
/* // STUB template bool ArrayBag::remove(const ItemType& anEntry) { return false; // STUB
} // end remove
*/ template bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if return
canRemoveItem;
} // end remove /*
// STUB template void ArrayBag::clear() { // STUB } // end clear
*/ template void ArrayBag::clear() { itemCount = 0; } // end clear
template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template bool ArrayBag::contains(const ItemType& anEntry) const
3. {
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1: First version
template bool ArrayBag::contains(const ItemType& target) const
{
return getFrequencyOf(target) > 0;
} // end contains
// ALTERNATE 2: Second version
template bool ArrayBag::contains(const ItemType& anEntry) const
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount))
{
if (anEntry == items[curIndex])
{
found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains
*/ template vector ArrayBag::toVector() const
{
vector bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found =
false; int result = -1;
int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target)
{
found = true;
4. result = searchIndex; }
else
{
searchIndex++;
} // end if
}
// end while return result;
} // end getIndexOf
#endif
/** Listing 1-1. @file BagInterface.h */
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include using namespace std;
template class BagInterface
{
public:
/** Gets the current number of entries in this bag. @return The integer number of entries
currently in the bag. */
virtual int getCurrentSize() const = 0;
/** Sees whether this bag is empty. @return True if the bag is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this bag. @post If successful, newEntry is stored in the bag and the
count of items in the bag has increased by 1. @param newEntry The object to be added as a new
entry. @return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this bag, if possible. @post If successful,
anEntry has been removed from the bag and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed. @return True if removal was successful, or false if
not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this bag. @post Bag contains no items, and the count of items is 0.
*/
virtual void clear() = 0;
/** Counts the number of times a given entry appears in bag. @param anEntry The entry to be
counted. @return The number of times anEntry appears in the bag. */ virtual int
getFrequencyOf(const ItemType& anEntry) const = 0;
5. /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return
True if bag contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Empties and then fills a given vector with all entries that are in this bag. @return A vector
containing all the entries in the bag. */
virtual vector toVector() const = 0;
}; // end BagInterface
#endif
#include #include
#include "ArrayBag.h" using namespace std;
void displayBag(ArrayBag& bag)
{
cout << "The bag contains " << bag.getCurrentSize() << " items:" << endl;
vector bagItems = bag.toVector();
int numberOfEntries = (int)bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
} // end for
cout << endl << endl;
} // end displayBag
void bagTester(ArrayBag& bag)
{
cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
displayBag(bag);
string items[] = {"one", "two", "three", "four", "five", "one"};
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty() << ";should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl;
cout << "Try to add another entry: add("extra") returns " << bag.add("extra") << endl;
} // end bagTester
int main()
6. {
ArrayBag bag;
cout << "Testing the Array-Based Bag:" << endl;
cout << "The initial bag is empty." << endl;
bagTester(bag);
cout << "All done!" << endl;
system("pause");
return 0;
} // end main
#include "DateClass.h"
#include using namespace std;
int main()
{
Date date1;
Date date2(31, 12, 2015);
Date date3(29, 2, 2016);
Date date4(28, 2, 2016);
date1.printDate();
cout << " advanced by a day gives ";
date1.advanceDay();
date1.printDate();
cout << endl;
date2.printDate();
cout << " advanced by a day gives ";
date2.advanceDay();
date2.printDate();
cout << endl;
date3.printDate();
cout << " advanced by a day gives ";
date3.advanceDay();
date3.printDate();
cout << endl;
date4.printDate();
cout << " advanced by a day gives ";
date4.advanceDay();
date4.printDate();
7. cout << endl;
}
#include
#include "DateClass.h" using namespace std;
Date::Date()
{
day = 1;
month = 1;
year = 2016;
}
Date::Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
int Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
bool IsLeap(int y)
{
return ((!(y % 4) && y % 100) || !(y % 400));
}
void Date::advanceDay()
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month
== 12)
8. {
//numOfDays = 31;
if (day == 31)
{
day = 1;
month++;
if (month == 13)
{
year++;
month = 1;
}
}
else day++;
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
//numOfDays = 30;
if (day == 30)
{
day = 1;
month++;
}
else
day++;
}
if (month == 2)
{
if (IsLeap(year))
{
//numOfDays = 29;
if (day == 29)
{
day = 1;
month++;
}
else day++;
9. }
else
{
//numOfDays = 28;
if (day == 28)
{
day = 1;
month++;
}
else
day++;
}
}
}
void Date::printDate()
{
cout << day << "/" << month << "/" << year;
}
#include using namespace std;
class Date
{
int day;
int month;
int year;
public:
Date();
Date(int d, int m, int y);
int getDay();
int getMonth();
int getYear();
}
Void main()
{
Date.getDay(); //Get the day from the bag
Date.getMonth(); //Get the month from the bag
Date.getYear();//Get the year from the bag
10. cout<
Solution
/** Header file for an array-based implementation of the ADT bag. @file ArrayBag.h */
#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include "BagInterface.h"
template class ArrayBag : public BagInterface
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that // contains the given target or -1,
if the array does not contain // the target.
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector toVector() const;
}; // end ArrayBag
//#include "ArrayBag.cpp" // /** Implementation file for the class ArrayBag. @file
ArrayBag.cpp */
#include "ArrayBag.h"
#include template
ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{ } // end default constructor
template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
11. template bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry; itemCount++;
} // end if return hasRoomToAdd;
} // end add
/* // STUB template bool ArrayBag::remove(const ItemType& anEntry) { return false; // STUB
} // end remove
*/ template bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if return
canRemoveItem;
} // end remove /*
// STUB template void ArrayBag::clear() { // STUB } // end clear
*/ template void ArrayBag::clear() { itemCount = 0; } // end clear
template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
12. } // end getFrequencyOf
template bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1: First version
template bool ArrayBag::contains(const ItemType& target) const
{
return getFrequencyOf(target) > 0;
} // end contains
// ALTERNATE 2: Second version
template bool ArrayBag::contains(const ItemType& anEntry) const
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount))
{
if (anEntry == items[curIndex])
{
found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains
*/ template vector ArrayBag::toVector() const
{
vector bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found =
false; int result = -1;
int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target)
13. {
found = true;
result = searchIndex; }
else
{
searchIndex++;
} // end if
}
// end while return result;
} // end getIndexOf
#endif
/** Listing 1-1. @file BagInterface.h */
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include using namespace std;
template class BagInterface
{
public:
/** Gets the current number of entries in this bag. @return The integer number of entries
currently in the bag. */
virtual int getCurrentSize() const = 0;
/** Sees whether this bag is empty. @return True if the bag is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this bag. @post If successful, newEntry is stored in the bag and the
count of items in the bag has increased by 1. @param newEntry The object to be added as a new
entry. @return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this bag, if possible. @post If successful,
anEntry has been removed from the bag and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed. @return True if removal was successful, or false if
not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this bag. @post Bag contains no items, and the count of items is 0.
*/
virtual void clear() = 0;
/** Counts the number of times a given entry appears in bag. @param anEntry The entry to be
14. counted. @return The number of times anEntry appears in the bag. */ virtual int
getFrequencyOf(const ItemType& anEntry) const = 0;
/** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return
True if bag contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Empties and then fills a given vector with all entries that are in this bag. @return A vector
containing all the entries in the bag. */
virtual vector toVector() const = 0;
}; // end BagInterface
#endif
#include #include
#include "ArrayBag.h" using namespace std;
void displayBag(ArrayBag& bag)
{
cout << "The bag contains " << bag.getCurrentSize() << " items:" << endl;
vector bagItems = bag.toVector();
int numberOfEntries = (int)bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
} // end for
cout << endl << endl;
} // end displayBag
void bagTester(ArrayBag& bag)
{
cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
displayBag(bag);
string items[] = {"one", "two", "three", "four", "five", "one"};
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty() << ";should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl;
cout << "Try to add another entry: add("extra") returns " << bag.add("extra") << endl;
15. } // end bagTester
int main()
{
ArrayBag bag;
cout << "Testing the Array-Based Bag:" << endl;
cout << "The initial bag is empty." << endl;
bagTester(bag);
cout << "All done!" << endl;
system("pause");
return 0;
} // end main
#include "DateClass.h"
#include using namespace std;
int main()
{
Date date1;
Date date2(31, 12, 2015);
Date date3(29, 2, 2016);
Date date4(28, 2, 2016);
date1.printDate();
cout << " advanced by a day gives ";
date1.advanceDay();
date1.printDate();
cout << endl;
date2.printDate();
cout << " advanced by a day gives ";
date2.advanceDay();
date2.printDate();
cout << endl;
date3.printDate();
cout << " advanced by a day gives ";
date3.advanceDay();
date3.printDate();
cout << endl;
date4.printDate();
cout << " advanced by a day gives ";
16. date4.advanceDay();
date4.printDate();
cout << endl;
}
#include
#include "DateClass.h" using namespace std;
Date::Date()
{
day = 1;
month = 1;
year = 2016;
}
Date::Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
int Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
bool IsLeap(int y)
{
return ((!(y % 4) && y % 100) || !(y % 400));
}
void Date::advanceDay()
{
17. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month
== 12)
{
//numOfDays = 31;
if (day == 31)
{
day = 1;
month++;
if (month == 13)
{
year++;
month = 1;
}
}
else day++;
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
//numOfDays = 30;
if (day == 30)
{
day = 1;
month++;
}
else
day++;
}
if (month == 2)
{
if (IsLeap(year))
{
//numOfDays = 29;
if (day == 29)
{
day = 1;
month++;
18. }
else day++;
}
else
{
//numOfDays = 28;
if (day == 28)
{
day = 1;
month++;
}
else
day++;
}
}
}
void Date::printDate()
{
cout << day << "/" << month << "/" << year;
}
#include using namespace std;
class Date
{
int day;
int month;
int year;
public:
Date();
Date(int d, int m, int y);
int getDay();
int getMonth();
int getYear();
}
Void main()
{
Date.getDay(); //Get the day from the bag