Bag-based Dictionary

profileorange.rt
bagADTfiles.zip

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 <stdlib.h> #include "book.h" template <typename E> 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 <typename Key, typename E> 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 <typename Key, typename E> 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 <iostream> #include <cstdlib> #include <string> #include <time.h> // Used by timing functions // 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<typename E> 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 ( do not use the C++ vector library) implementation for bags. Then use your bag to implement the dictionary ADT provided you.

Test your bag and dictionary implementations with the following template combinations:

· <int, string> -- all functions

· <string, int> -- insert and find functions only

You will only get credit for those methods you test and display, so be sure you don’t leave any out.

Put the following files into a zip file named student_name_Bag_Assignment and submit them to Blackboard:

· ABag.h // Your bag-array implementation

· BDictionary.h // Your Dictionary-bag implementation

· dictionaryTest.cpp // Your test program

· screen_shots.doc/txt // Word document with screen shot(s) showing // program in action or a text file showing the test // 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.

· Your_dictionary.exe //Your executable file.***

*** If you completed your assignment using Visual Studios you must use Visual Studios 2010 or 2013 and I recommend you run your executable on another computer to insure it will run as a stand-alone executable. Depending on how you initialized your project it may not run in the stand-alone mode. If that is the case, you will have to zip up your entire project directory and submit that to me. See me if you have any questions.

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.

Your assignment is due Wednesday of Week 4. Late assignments will not be accepted.

Rubrics:

· Program must run in order to get any points. By “run” I mean that at 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 (<int, int>, <string, string>, etc.). When you are satisfied the bag is working then move on to the dictionary, again implementing and testing function by function.

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).

image1.emf

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 (not a vector) 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)

Dictionary (ADT)

KV pair (implementation)

Do not modify the templates or the KVPair class unless you talk to me first (I’ll probably say no).

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 with the following key/value types:

<int, string>

<string, int>

Programming Assignment 1 (3:7)

Bag

Think of a grocery bag

Bag fills 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 are to use the following KV pair types for this assignment:

<int, string>

<string, int>

Programming Assignment 1 (7:7)

Example dictionary test run

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.

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

image2.tmp