Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------
- //MAIN.CPP
- //---------
- #include <iostream>
- #include "hashTable.h"
- #include "stateData.h"
- using namespace std;
- void main()
- {
- hashTable<stateData> myTable;
- stateData CA("California", "Sacramento", 1850, 31);
- stateData OR("Oregon", "Salem", 1859, 33);
- stateData WA("Washington", "Olympia", 1889, 42);
- myTable.insert(WA);
- myTable.insert(OR);
- myTable.insert(CA);
- myTable.insert(OR);
- //cout << (WA == OR) //Should return false, and does
- //cout << (WA == WA) //Should return true, and does
- myTable.display();
- system("pause");
- }
- //------------
- //STATEDATA.H
- //------------
- #include <iostream>
- using namespace std;
- #ifndef STATEDATA
- #define STATEDATA
- class stateData
- {
- private:
- char name[15];
- char capital[100];
- int admissionYear; //Year admitted to the Union
- int admissionOrder; //What order was it admitted? (i.e. Delaware is 1, Hawaii is 50)
- public:
- stateData()
- {
- name[0] = '\0';
- capital[0] = '\0';
- admissionYear = -1;
- admissionOrder = -1;
- }
- stateData(stateData &other)
- {
- memcpy(this->name, other.name, 15);
- memcpy(this->capital, other.capital, 100);
- this->admissionYear = other.admissionYear;
- this->admissionOrder = other.admissionOrder;
- }
- stateData(char* name, char* capital, int year, int order)
- {
- memcpy(this->name, name, 15);
- memcpy(this->capital, capital, 100);
- admissionYear = year;
- admissionOrder = order;
- }
- char* getName() { return name; }
- char* getCapital() { return capital; }
- int getYear() { return admissionYear; }
- int getOrder() { return admissionOrder; }
- void setName(char* name) { memcpy(this->name, name, 100); }
- void setCapital(char* capital) { memcpy(this->name, name, 100); }
- void setYear(int year) { admissionYear = year; }
- void setOrder(int order) { admissionOrder = order; }
- bool operator<(stateData &other) { return this->name < other.name; }
- bool operator<=(stateData &other) { return this->name <= other.name; }
- bool operator>(stateData &other) { return this->name > other.name; }
- bool operator>=(stateData &other) { return this->name >= other.name; }
- bool operator==(stateData &other) { return this->name == other.name; }
- bool operator!=(stateData &other) { return this->name != other.name; }
- friend ostream& operator<<(ostream &out, stateData &pState)
- {
- out << pState.name;
- /*
- out << pState.name << " was admitted to the United States in " << pState.admissionYear << " as the " << pState.admissionOrder;
- switch (pState.admissionOrder)
- {
- case 1:
- case 21:
- case 31:
- case 41:
- out << "st";
- break;
- case 2:
- case 22:
- case 32:
- case 42:
- out << "nd";
- break;
- case 3:
- case 23:
- case 33:
- case 43:
- out << "rd";
- break;
- default:
- out << "th";
- }
- out << " state. It's capital is " << pState.capital << "." << endl;
- */
- return out;
- }
- };
- #endif
- //------------
- //HASHTABLE.H
- //------------
- #include <iostream>
- #include "stateData.h"
- using namespace std;
- #ifndef HASH_TABLE
- #define HASH_TABLE
- template<class T>
- class hashTable
- {
- private:
- T** list;
- int length;
- const int TABLE_SIZE = 101;
- public:
- hashTable()
- {
- //Creates a new array of pointers of type T
- length = 0;
- list = new T*[TABLE_SIZE];
- for (int i = 0; i < TABLE_SIZE; i++)
- list[i] = NULL;
- }
- ~hashTable()
- {
- //Deletes the memory allocated to the hash table
- for (int i = 0; i < TABLE_SIZE; i++)
- {
- if (list[i] != NULL)
- delete list[i];
- }
- delete[] list;
- }
- void display()
- {
- if (!isEmpty())
- {
- for (int i = 0; i < TABLE_SIZE; i++)
- {
- if (list[i] != NULL)
- cout << "ID: " << i << "; Value: " << *list[i] << endl;
- }
- }
- else
- {
- cout << "Hash table is currently empty" << endl;
- }
- }
- int hashFunc(stateData state)
- {
- string name = string(state.getName());
- int i = 0, sum = 0;
- int len = name.length();
- for (int j = 0; j < (15 - len); j++)
- {
- name = name + ' ';
- }
- for (int j = 0; j < 5; j++)
- {
- sum += (static_cast<int>(name[i]) * 128 * 128 + static_cast<int>(name[i + 1]) * 128 + static_cast<int>(name[i + 2]));
- i += 3;
- }
- return sum % TABLE_SIZE;
- }
- void insert(T &what)
- {
- int where = hashFunc(what);
- bool added = false;
- if (!isFull())
- {
- for (int i = 0; i <= sqrt(TABLE_SIZE); i++)
- {
- where = (where + i*i) % TABLE_SIZE;
- if (*list[where] == what)
- {
- cout << "Duplicate entries are not allowed in the hash table.";
- break;
- }
- if (list[where] == NULL)
- {
- list[where] = new T(what);
- length++;
- added = true;
- break;
- }
- }
- }
- if (!added)
- cerr << "Data could not be added to hash table." << endl;
- }
- bool isEmpty()
- {
- return length == 0;
- }
- bool isFull()
- {
- return length == (TABLE_SIZE / 2);
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement