bagADT.h
/*
* File: bagADT.h — Bag ADT for CSCI 215 Assignment
* Author: Prof Terri Sipantzi
*
* Created on July 14, 2012, 11:46 AM
* Modified on 9/4/2014 — Changed “const E& inspectTop() const” to “bool inspectTop(const E& item) const”.
* inspectTop now returns false if the bag is empty and true otherwise.
* Modified on 2/4/2015 — updated the comments for remove(), find(), removeTop(),
* and inspectTop() to make them clearer.
*/
#ifndef BAGADT_H
#define BAGADT_H
#include
#include “book.h”
template
class Bag {
public:
Bag() {} // base constructor
virtual ~Bag() {} // base destructor
// Insert a new item into the bag — return false if fails and true if
// successful
virtual bool addItem(const E& item) = 0;
// Looks for ‘item’ in the bag and if found updates ‘item’ with the
// bag value and returns true. Otherwise ‘item’ is left unchanged
// and the method returns false.
virtual bool remove(E& item) = 0;
// Removes the top record from the bag, puts it in returnValue, and
// returns true if the bag is not empty. If the bag is empty the
// function returns false and returnValue remains unchanged.
virtual bool removeTop(E& returnValue) = 0;
// Finds the record using returnValue and if the record is found updates
// returnValue based on the contents of the bag and returns true. If the
// record is not found the function returns false. Works just like remove()
// except that the found record is not removed from the bag.
virtual bool find(E& returnValue) const = 0;
// Inspect the top of the bag. If the bag is empty return
// false and leave ‘item’ unchanged; otherwise, return true and update
// ‘item’ with the contents of the bag.
virtual bool inspectTop(E& item) const = 0;
// empties the bag
virtual void emptyBag() = 0;
// use the += operator to add an item to the bag
virtual bool operator+=(const E& addend) = 0;
// get the size of the bag
virtual int size() const = 0;
// get the capacity of the bag
virtual int bagCapacity() const = 0;
};
#endif /* BAGADT_H */
dictionaryADT.h
// From the software distribution accompanying the textbook
// “A Practical Introduction to Data Structures and Algorithm Analysis,
// Third Edition (C++)” by Clifford A. Shaffer.
// Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.
#ifndef DICTIONARYADT_H
#define DICTIONARYADT_H
// The Dictionary abstract class.
template
class Dictionary {
public:
Dictionary() {} // Default constructor
virtual ~Dictionary() {} // Base destructor
// Reinitialize dictionary
virtual void clear() = 0;
// Insert a record
// k: The key for the record being inserted.
// e: The record being inserted.
// Return true if insert is successful and false otherwise
virtual bool insert(const Key& k, const E& e) = 0;
// Looks for a record using the key and if found does the following:
// – updates the E& rtnVal
// – removes the record from the dictionary
// – returns true
// If the record is not found the function returns false.
virtual bool remove(const Key& k, E& rtnVal) = 0;
// Takes an arbitrary record from the dictionary and does the following:
// – updates the E& returnValue
// – removes the record from the dictionary
// – returns true
// If the dictionary is empty the function returns false.
virtual bool removeAny(E& returnValue) = 0;
// Looks for a record using the key and if found does the following:
// – updates the E& returnValue
// – returns true
// If the record is not found the function returns false.
virtual bool find(const Key& k, E& returnValue) const = 0;
// Return the number of records in the dictionary.
virtual int size() = 0;
};
#endif
kvpair.h
// From the software distribution accompanying the textbook
// “A Practical Introduction to Data Structures and Algorithm Analysis,
// Third Edition (C++)” by Clifford A. Shaffer.
// Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.
#ifndef KVPAIR_H
#define KVPAIR_H
// Container for a key-value pair
// Key object must be an object for which the == operator is defined.
// For example, int and string will work since they both have == defined,
// but Int will not work since it does not have == defined.
template
class KVpair {
private:
Key k;
E e;
public:
// Constructors
KVpair() {}
KVpair(Key kval, E eval)
{
k = kval; e = eval;
}
KVpair(const KVpair& o) // Copy constructor
{
k = o.k; e = o.e;
}
void operator =(const KVpair& o) // Assignment operator
{
k = o.k; e = o.e;
}
bool operator==(const KVpair& o) const {
if (o.k == k) {
return true;
}
return false;
}
// Data member access functions
Key key() { return k; }
void setKey(Key ink) { k = ink; }
E value() { return e; }
};
#endif
book.h
// From the software distribution accompanying the textbook
// “A Practical Introduction to Data Structures and Algorithm Analysis,
// Third Edition (C++)” by Clifford A. Shaffer.
// Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.
// A collection of various macros, constants, and small functions
// used for the software examples.
// First, include all the standard headers that we need
#ifndef BOOK_H
#define BOOK_H
#include
#include
#include
#include
// Now all the standard names that we use
using std::cout;
using std::endl;
using std::string;
using std::ostream;
const int defaultSize = 10; // Default size
// Return true iff “x” is even
inline bool EVEN(int x) { return (x % 2) == 0; }
// Return true iff “x” is odd
inline bool ODD(int x) { return (x % 2) != 0; }
// Assert: If “val” is false, print a message and terminate
// the program
void Assert(bool val, string s) {
if (!val) { // Assertion failed — close the program
cout << "Assertion Failed: " << s << endl;
exit(-1);
}
}
// Swap two elements in a generic array
template
inline void swap(E A[], int i, int j) {
E temp = A[i];
A[i] = A[j];
A[j] = temp;
}
// Random number generator functions
inline void Randomize() // Seed the generator
{ srand(1); }
// Return a random value in range 0 to n-1
inline int Random(int n)
{ return rand() % (n); }
// Swap two integers
inline void swap(int& i, int& j) {
int temp = i;
i = j;
j = temp;
}
// Swap two char*’s
inline void swap(char* i, char* j) {
char* temp = i;
i = j;
j = temp;
}
// Big enough for simple testing
#define INFINITY 9999
// Timing variables and functions
unsigned tstart = 0; // Time at beginning of timed section
// Initialize the program timer
void Settime() { tstart = (unsigned) clock(); }
// Return the elapsed time since the last call to Settime
double Gettime() {
unsigned tcurr = (unsigned) clock();
return (double)(tcurr – tstart)/(double)CLOCKS_PER_SEC;
}
// Your basic int type as an object.
class Int {
private:
int val;
public:
Int(int input=0) { val = input; }
// The following is for those times when we actually
// need to get a value, rather than compare objects.
int key() const { return val; }
// Overload = to support Int foo = 5 syntax
Int operator= (int input) { val = input; return val; }
};
// Let us print out Ints easily
ostream& operator<<(ostream& s, const Int& i)
{ return s << i.key(); }
ostream& operator<<(ostream& s, const Int* i)
{ return s << i->key(); }
#endif //end of BOOK_H
Programming Assignment 1.docx
Programming Assignment 1 –
Bag-based Dictionary
Implement a dictionary using a Bag—Project 4.7 in the text (modified)
Use the bag ADT provided to create an array-based implementation for bags. Then use your bag to implement the dictionary ADT provided you. This means you have to implement your dictionary using the bag functions.
Test your bag and dictionary implementations with the bagtestmain.cpp file I’ve provided for you. If you are not able to fully implement all the functions of the dictionary and/or bag, you may modify the tests in bagtestmain.cpp to only exercise those functions you were able to complete.
You will only get credit for those methods you test and display, so be sure you don’t leave any out.
Also, you cannot add any public functions to your bag and dictionary implementations beyond those specified in the ADTs, though you may add private functions if you want.
Put the following files into a zip file named student_name_Bag_Assignment and submit them to Blackboard:
· ABag.h // Your bag-array implementation which must inherit the Bag class
· BDictionary.h // Your Dictionary-bag implementation which must inherit the // Dictionary class
· bagtestmain.cpp // The test driver I have provided for you
· bagADT.h // The bag ADT I gave you – it should be unchanged
· dictionaryADT.h // The dictionaryADT I gave you – it should be unchanged
· kvpair.h // The kvpair class I gave you – it should be unchanged
· Screen Shots // Word document with screen shot(s) and integrity statements showing
// all of your program’s output.
Approach.doc // This is a Word document where you explain how you implemented
// your solution and how you tested each required function.
· Any other .cpp and/or .h files that comprise your project (I need all the .cpp and .h files used in your project).
· Your_dictionary.exe //Your executable file.***
Note: If your ABag does not inherit Bag and/or BDictionary does not inherit Dictionary, you will not receive any credit for your work. If you use the templates I’ve provided (ABag.h and BDictionary.h) the inheritance is already done for you.
*** If you completed your assignment using Visual Studios you must use Visual Studios 2017 and I would like you to submit your entire VS project directory.
Your test program must exercise every function of the dictionary. For any function whose functionality is not obvious you must explain in your Word document how your test output demonstrates that function. See me if you have questions.
See Blackboard for the assignment due date and the syllabus for the late policy.
Rubrics (for the 70% content portion):
· Program must run in order to get any points. By “run” I mean that you must at least get one or more of the bag methods working (and your program must demonstrate that functionality).
Tips for Success
Start by working on your “Approach” first. Once you are satisfied with your approach, then start building your program incrementally. Start with the bag and increment one feature at a time (you’ll have to stub out the features the ADT requires that you are not ready to implement yet) starting with the constructors and then working your way down the feature list using common sense to figure out which features need to be implemented first. Try your bag out with the various parameter combinations I want you to test with (
Don’t wait until the last minute. You’ll find that many of your problems you will solve while you are away from your computer and have a chance to think about the error you are seeing. This takes time.
Note: KVpair, which uses the == operator for comparing the key values, will only accept objects that have also implemented the == operator. This class has been tested with the following types:
· string
· int
It specifically does not work with the Int type (at least not in the version of C++ I am working with).
Debugging your code
A big part of this assignment is debugging your code, so do not expect your instructor to do this for you. Having completed the pre-requisite courses for this class, you should already have some experience finding coding errors and fixing them. This class will give you plenty of opportunities to further refine those skills and you can only do that by wrestling with the problem. Here are some debugging tips:
· Build a little, test a little. Don’t write all your code before you start debugging it. Break your work into logical chunks that can be compiled and debugged and build them one at a time. For this project, I would start by building the Bag class and implementing the addItem() function first. Once I get that function working properly, then I would move on to another Bag function. The idea is you build and test a function one function at a time. That way, if you run into an error, you know where to look.
· Learn to use the debugger if you haven’t already. The debugger allows you to step through your code one step and a time and see what happens in memory while you’re doing it. When students come to me with problems, I first try to isolate where the problem is logically based on what the program is doing and then I use the debugger to find the actual fault. Here is an excellent video tutorial on the Visual Studios debugger: How to DEBUG C++ in VISUAL STUDIO.
· Be willing to walk away from your computer and give your mind a rest. I find that the solution to a problem often comes to me while I am doing something else. I have never regretted stepping away from my computer to let the problem “percolate” in my mind, but I have often regretted not doing this. This is one of the reasons you should never wait till the last minute to start working on your program; by doing that you are not giving yourself the time to walk away.
Name:
Bag Rubrics100points
MethodValueEarnedPointsMethodsValueEarnedPoints
addItem15%0clear20%0
remove15%0insert13%0
removeTop5%0remove25%0
find20%0removeAny13%0
inspectTop15%0find25%0
emptyBag5%0size5%0
+= operator10%0Total100%0
size8%0
bagCapacity8%0
Total100%0
Submitted Correctly (10%)0
Total Points0
Bag Methods (55%)Dictionary Methods (35%)
Programming Assignment 1 — Additional Information on Bags.pptx
Programming Assignment 1 (1:7)
Build a Dictionary on top of a
Bag built on top of an
Array of objects KVpairs (key/value pairs)
Your dictionary should be able to hold at least 10 items.
I have given you the templates for a
Bag (ADT and implementation)
Dictionary (ADT and implementation)
KV pair (implementation)
Do not modify the ADT templates or the KVPair class.
Programming Assignment 1 (2:7)
Start by writing your Approach document first. I recommend you use an incremental approach
Build the Bag first and get it working
Then build the Dictionary using the Bag and get it working.
Build your program one function at a time.
When you finish your Bag and then Dictionary show that it works using the bagtestmain.cpp file I’ve provided for you.
Programming Assignment 1 (3:7)
What is a Bag
Think of a grocery bag
Bags fill up from bottom to top (like a stack)
Unlike a stack, a bag provides access to all of the contents, not just the items at the top
To find something in a bag you start looking from the top
See BagADT.h for Bag operations
Programming Assignment 1 (4:7)
Dictionary
Used for storing key/value records
Provides operations for
Storing,
Finding, and
Removing records from a collection
Comparable objects
Since Find is the fundamental operation of a dictionary we need a key to search on
For the English dictionary the key is the word itself (Key) and the definition consists of the word and its definition (Value)
Dictionaries use a Key/Value pair; the key provides the “comparable object” used by Find
Programming Assignment 1 (5:7)
In implementing your Dictionary you must use the available bag operations to implement your dictionary operations.
Bag Operations
AddItem(E)
Remove(E)
RemoveTop(E)
Find(E)
EmptyBag()
+= operator overload (adds an item)
BagCapicity()
Dictionary Operations:
Clear()
Insert(E)
Remove(E)
RemoveAny(E)
Find(E)
Size()
Note: You only get credit for the methods you demonstrate in your testing.
1/26/2015: Note that you’ve changed the Bag ADT so that remove(const &E) is no longer a constant to make the method similar to find().
5
Programming Assignment 1 (6:7)
KVPair.h provides the template (this is an implementation and not an ADT) for creating a key/value pair used by both the Bag and Dictionary. You do not need (nor are you allowed to) make any changes to it.
You are to use the following KV pair types for this assignment:
Programming Assignment 1 (7:7)
Example
dictionary
test run
In this example I
am doing
You must do
tests.
My test run only shows my Dictionary run, but if you cannot get your dictionary to work you must show your Bag test runs that demonstrate every working function. You only get credit for the functions that work and to get any credit at all you need to get one or more of the Bag functions working.
7
Dictionaries & KVPairs
For more information on Dictionaries and the use of KVPairs read section 4.4 of your text
There you will find both details and usage examples.
Assignment 1 Comments
You must implement the ADTs by inheriting them – you cannot modify them
class ABag : public bagADT { }
class BDictionary : public dictionaryADT { }
As a minimum you must
Demonstrate one or more working bag operations to get any credit for this assignment and
You will not get credit, even if one or more ADT operations are implemented, if your implementation does not inherit the ADTs.
If you use the implementation files I’ve provided, the inheritance issue is taken care of for you.
Data Structures and Algorithm Analysis by Cliffor A. Shaffer
Why do you think inheritance is so important here?
So that users can declare their types to be of the ADT type, which keeps their program loosely coupled to the implementation details.
The ADT acts as a contract that your implementation is required to implement. The compiler will tell you if you are not upholding your end of the contract.
9
Style Comments
You do not have to separate declaration and implementation into header and .cpp files
Put declaration and implementation both in the header file
I expect to see
Self-documenting code
Good use of white space and indentation
Liberal use of comments
Easy to read and understand
Style and your ability to follow instructions is worth 30% of the assignment.
ABag.h
/*
* File: ABag.h
* Author: Prof Terri Sipantzi
*
* You may use this template to implement your ABag.
*/
#ifndef ABAG_H
#define ABAG_H
#include “book.h”
#include “bagADT.h”
template
class ABag : public Bag
public:
// constructors/destructor
// bag methods: addItem, remove, operator+=, size, etc.
private:
E *data; // an array of items
int used; // number of items the data array currently holds
int capacity; // the number of items the bag can hold (size of array)
};
#endif /* ABAG_H */
BDictionary.h
/*
* File: BDictionary.h — implement a dictionary use an array-based bag
* Author: Prof Terri Sipantzi
*
* You may use this template to implement your Dictionary
*/
#ifndef BDICTIONARY_H
#define BDICTIONARY_H
#include “ABag.h”
#include “dictionaryADT.h”
#include “kvpair.h”
template
class BDictionary : public Dictionary
public:
// constructors/destructor
// methods: clear, insert, remove, removeAny, find, size, etc.
private:
//Pointer to a ABag object. You’ll need to instantiate the bag in your constructor:
// dictionary = new ABag
// you’ve implemented your ABag constructor(s).
//This pointer gives you access to the bag which stores your data and provides the
//functions you need to build your dictionary.
ABag
};
#endif /* BDICTIONARY_H */
bagtestmain.cpp
bagtestmain.cpp
/*
* File: bagtestmain.cpp
* Author: Prof Sipantzi — CSIS 215 — Programming Assignment 1 — Bag Dictionary
*
* Created on July 14, 2012, 11:45 AM
* Updated pointers to smart pointers in ABag and BDictionary on 12/14/2018
*/
#include
<
string
>
#include
<
sstream
>
#include
“ABag.h”
#include
“BDictionary.h”
using
namespace
std
;
const
size_t DICTIONARY_SIZE
=
20
;
void
PauseScreen
();
//Used to pause screen output
/*
* Tests BDictionary with int and string objects only.
*/
int
main
(
int
argc
,
char
**
argv
)
{
cout
<<
"
<<
endl
<<
endl
;
BDictionary
<
int
,
string
>
myIntStrDict
(
DICTIONARY_SIZE
);
BDictionary
<
string
,
int
>
myStrIntDict
(
DICTIONARY_SIZE
);
// myIntStrDict tests
// INSERT: myIntStrDict
cout
<<
"Testing dictionary with
;
for
(
int
i
=
1
;
i
<=
DICTIONARY_SIZE
;
i
++
)
{
stringstream sn
;
sn
<<
"Beth "
<<
i
*
10
;
myIntStrDict
.
insert
(
i
*
10
,
sn
.
str
());
}
cout
<<
"INSERT: Size of myIntStrDict is "
<<
myIntStrDict
.
size
()
<<
endl
;
// REMOVEANY: myIntStrDict
string strData
;
if
(
myIntStrDict
.
removeAny
(
strData
)
==
true
)
{
cout
<<
"REMOVEANY: My string data is "
<<
strData
<<
endl
;
}
else
{
cout
<<
"RemoveAny() failed -- dictionary is empty.n"
;
}
cout
<<
"Size of myIntStrDict is "
<<
myIntStrDict
.
size
()
<<
"n"
;
// FIND: test for myIntStrDict.find
int
intKey
=
40
;
if
(
myIntStrDict
.
find
(
intKey
,
strData
)
==
true
)
{
cout
<<
"FIND: My data at key=="
<<
intKey
<<
" is: "
<<
strData
<<
"n"
;
cout
<<
"Size of myIntStrDict is "
<<
myIntStrDict
.
size
()
<<
"n"
;
}
else
{
cout
<<
"Could not find a record at "
<<
intKey
<<
"n"
;
}
// REMOVE: myIntStrDict
intKey
=
60
;
if
(
myIntStrDict
.
remove
(
intKey
,
strData
)
==
true
)
{
cout
<<
"REMOVE: Removed key "
<<
intKey
<<
" which was "
<<
strData
<<
"n"
;
}
else
{
cout
<<
"Could not find key "
<<
intKey
<<
"n"
;
}
cout
<<
"Size of my dictionary is "
<<
myIntStrDict
.
size
()
<<
"n"
;
// CLEAR: myIntStrDict
myIntStrDict
.
clear
();
cout
<<
"CLEAR: Size of myIntStrDict is "
<<
myIntStrDict
.
size
()
<<
"nn"
;
/* end myIntStrDict tests ---------------------------------------------*/
// myStrIntDict tests
// INSERT: myStrIntDict
cout
<<
"Testing dictionary with
;
myStrIntDict
.
insert
(
“Terri”
,
57
);
myStrIntDict
.
insert
(
“Beth”
,
53
);
myStrIntDict
.
insert
(
“Jeremy”
,
19
);
myStrIntDict
.
insert
(
“Nathan”
,
17
);
myStrIntDict
.
insert
(
“Zipper”
,
2
);
myStrIntDict
.
insert
(
“Button”
,
1
);
myStrIntDict
.
insert
(
“Kiwi”
,
7
);
myStrIntDict
.
insert
(
“Masoala”
,
10
);
cout
<<
"INSERT: Size of myStrIntDict is "
<<
myStrIntDict
.
size
()
<<
endl
;
// REMOVEANY: myStrIntDict
int
intData
;
if
(
myStrIntDict
.
removeAny
(
intData
)
==
true
)
{
cout
<<
"REMOVEANY: My int data is "
<<
intData
<<
endl
;
}
else
{
cout
<<
"RemoveAny() failed -- dictionary is empty.n"
;
}
cout
<<
"Size of myIntStrDict is "
<<
myStrIntDict
.
size
()
<<
"n"
;
// FIND: myStrIntDict.find
string strKey
=
"Kiwi"
;
if
(
myStrIntDict
.
find
(
strKey
,
intData
)
==
true
)
{
cout
<<
"FIND: "
<<
strKey
<<
"'s age is "
<<
intData
<<
endl
;
cout
<<
"Size of myStrIntDict is "
<<
myStrIntDict
.
size
()
<<
"n"
;
}
else
{
cout
<<
"Could not find a record at "
<<
strKey
<<
"n"
;
}
// REMOVE: myStrIntDict
strKey
=
"Button"
;
if
(
myStrIntDict
.
remove
(
strKey
,
intData
)
==
true
)
{
cout
<<
"REMOVE: Removed key "
<<
strKey
<<
" which was "
<<
intData
<<
"n"
;
}
else
{
cout
<<
"Could not find key "
<<
strKey
<<
"n"
;
}
cout
<<
"Size of my dictionary is "
<<
myStrIntDict
.
size
()
<<
"n"
;
// CLEAR: myStrIntDict
myStrIntDict
.
clear
();
cout
<<
"CLEAR: Size of myStrIntDict is "
<<
myStrIntDict
.
size
()
<<
"nn"
;
/* end myStrIntDict tests ---------------------------------------------*/
/* Demonstrate any Bag functions that were not used/demonstrated in the implemention
of your BDictionary and ABag using a Bag of KVpairs
ABag
<
KVpair
<
int
,
string
>>
myBag
;
//Used to test bag functions not previously demonstrated
PauseScreen
();
return
0
;
}
//Used to pause the screen wherever desired
void
PauseScreen
()
{
char
ch
;
cout
<<
"nPress the Enter key to continue ... "
;
cin
.
get
(
ch
);
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more