Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <ctime>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- struct Car
- {
- int num;
- string brand;
- string ownerName;
- };
- Car createCar(int num, string brand, string ownerName)
- {
- Car c;
- c.num = num;
- c.brand = brand;
- c.ownerName = ownerName;
- return c;
- }
- class HashTable
- {
- int size;
- int countOfNotes = 0;
- list<Car>* table;
- public:
- HashTable(int size);
- int getCountOfNotes();
- void insertItem(Car c);
- string getItem(int key);
- void deleteItem(int key);
- int hashFunction(int key);
- void rehash();
- void displayHash();
- void readFile();
- void writeFile();
- };
- void HashTable::readFile() {
- int num;
- string brand, ownerName;
- ifstream in("file.txt");
- if (in.is_open())
- {
- while (in >> num >> brand >> ownerName)
- {
- insertItem(createCar(num, brand, ownerName));
- }
- }
- in.close();
- }
- void HashTable::writeFile() {
- ofstream out;
- out.open("file.txt");
- if (out.is_open())
- {
- for (int index = 0; index < size; index++) {
- for (Car c : table[index]) {
- string car = "";
- car = to_string(c.num) + " " + c.brand + " " + c.ownerName;
- out << car + "\n";
- }
- }
- }
- }
- HashTable::HashTable(int size)
- {
- this->size = size;
- table = new list<Car>[size];
- }
- int HashTable::getCountOfNotes() {
- return countOfNotes;
- }
- void HashTable::insertItem(Car c)
- {
- int index = hashFunction(c.num);
- table[index].push_back(c);
- countOfNotes++;
- if (countOfNotes / size > 0.75) {
- rehash();
- }
- writeFile();
- }
- string HashTable::getItem(int key)
- {
- int index = hashFunction(key);
- list<Car>::iterator i;
- for (i = table[index].begin(); i != table[index].end(); i++) {
- if (i->num == key)
- return to_string(i->num) + " " + i->brand + " " + i->ownerName;
- }
- }
- void HashTable::deleteItem(int key)
- {
- int index = hashFunction(key);
- list<Car>::iterator i;
- for (i = table[index].begin(); i != table[index].end(); i++) {
- if (i->num == key)
- break;
- }
- if (i != table[index].end())
- table[index].erase(i);
- countOfNotes--;
- writeFile();
- }
- int HashTable::hashFunction(int key)
- {
- return key % size;
- }
- void HashTable::rehash() {
- HashTable newHT(size * 2);
- for (int i = 0; i < size; i++) {
- for (Car c : table[i]) {
- newHT.insertItem(c);
- }
- }
- size = size * 2;
- table = newHT.table;
- }
- void HashTable::displayHash() {
- cout << "HashTable now:" << endl;
- for (int i = 0; i < size; i++) {
- cout << i;
- for (Car c : table[i])
- cout << " --> " << c.num;
- cout << endl;
- }
- cout << endl;
- }
- void generateFile(int count) {
- ofstream out;
- out.open("file.txt");
- for (int i = 0; i < count; i++) {
- string car;
- Car c;
- c.num = rand() % 100000 + 10000;
- c.brand = char(rand() % 26 + 65);
- c.ownerName = char(rand() % 26 + 65);
- car = to_string(c.num) + " " + c.brand + " " + c.ownerName;
- if (out.is_open())
- {
- out << car + "\n";
- }
- }
- }
- int main()
- {
- srand(time(0));
- generateFile(5);
- HashTable ht(10);
- ht.readFile();
- ht.displayHash();
- int key;
- cout << "Enter key to delete note: ";
- cin >> key;
- cout << endl;
- ht.deleteItem(key);
- ht.displayHash();
- cout << "Enter key to find note: ";
- cin >> key;
- cout << "You found this car: " + ht.getItem(key) << endl;
- cout << endl;
- int num;
- string brand, ownerName;
- cout << "Add new note" << endl;
- cout << "Enter num: ";
- cin >> num;
- cout << "Enter brand: ";
- cin >> brand;
- cout << "Enter owner's name: ";
- cin >> ownerName;
- cout << endl;
- ht.insertItem(createCar(num, brand, ownerName));
- ht.displayHash();
- cout << "Enter key to find note: ";
- cin >> key;
- cout << "You found this car: " + ht.getItem(key) << endl;
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement