C++ What You Need to Do You have some freedom on this assignment. First, create
ID: 3826992 • Letter: C
Question
C++
What You Need to Do
You have some freedom on this assignment.
First, create a structure (named Student) that will describe each of students. The structure contains the following member variables:
An ID consisting of 4 digits. The data type should be a string or c-string instead of an int.
A name, which may have embedded spaces . The data type is a string or c-string.
Second, create a text file which lists students (say 25) in the format ID number, space, name. For example:
6666 Mini Me
2333 Help Me
7722 Yogi Beria
Note: There should be no duplicate IDs. However, so there will be some collisions, for about 12 of the 25 students, have 2 students have the same last 2 digits of the ID (e.g., no ID will duplicate, but the last 2 digits of the ID will).
Third, create a hash table class (using .h and .cpp files preferably) whose one member variable points to an array of 100 student structures. The placement of a student structure within the array is based on the last 2 digits of a student's ID. Thus, a student with an ID ending in 45 will go in the 45th subscript ([45]) of the array, unless there's a collision. The hash table class also would include the following member functions:
Constructor - Assigns to each element of the array the value NULL for the ID and the name. (Those values are how you determine if a slot is "empty".
Destructor - If you're using dynamic memory allocation. Otherwise probably not necessary.
insert - Has 2 parameters, representing ID and name, which are assigned to the member variables of the structure instance Assigns a student's information (ID and name) to the proper element of the array. Important: Uses hashing to determine the proper array element, and resolves collisions.
retrieve - Has 1 parameter, the ID, and returns the student's name, or NULL if no student with that ID exists. Important: Uses hashing to find that student in the array, and deals with collisions.
Fourth, create a driver or test file. This code will:
Create an instance of the hash table class.
Load the hash table from the text file, using the insert member function.
Display a menu:
Find a student by ID. If chosen, the user is prompted to enter a 4 digit ID (no input validation necessary). Using the retrieve member function, the program either will output the name of the student associated with that ID, or report that no student has that ID.
Exit. If chosen, the program will end.
Explanation / Answer
//hash.h
#ifndef HASH_H
#define HASH_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Student
{
string ID;
string name;
Student* next;
};
class hasht {
public:
hasht();
int Hash(string key);
void insert(string ID, string name);
string retrieve(string id);
int NumItemsIndex(int index);
void PrintTable();
private:
static const int tableSize = 100;
Student* HashTable[tableSize];
};
bool die(const string &msg);
void show(string a[100][2]);
#endif
//hash.cpp
#include "hash.h"
hasht::hasht()
{
for (int i = 0; i < tableSize; i++)
{
HashTable[i] = new Student;
HashTable[i]->name = "empty";
HashTable[i]->name = "empty";
HashTable[i]->next = NULL;
}
}
void hasht::insert(string ID, string name)
{
int index = Hash(ID);
if (HashTable[index]->ID == "empty")
{
HashTable[index]->ID = ID;
HashTable[index]->name = name;
}
else
{
for (int i = 1;; i++)
{
if (HashTable[index + i]->ID == "empty")
{
HashTable[index + i]->ID = ID;
HashTable[index + i]->name = name;
break;
}
}
}//else
}
string hasht::retrieve(string id)
{
int index = Hash(id);
string sname = "";
if (HashTable[index]->ID == id)
{
sname = HashTable[index]->name;
}
else
{
for (int i = 1; i <100 - (index + i); i++)
{
if (HashTable[index + i]->ID == id)
{
sname = HashTable[index + i]->name;
break;
}
}
}
return sname;
}
int hasht::NumItemsIndex(int index)
{
int count = 0;
if (HashTable[index]->ID == "empty")
{
return count;
}
else
{
count++;
Student* Ptr = HashTable[index];
while (Ptr->next != NULL)
{
count++;
Ptr = Ptr->next;
}
}
return count;
}
void hasht::PrintTable()
{
int number;
for (int i = 0; i < tableSize; i++)
{
number = NumItemsIndex(i);
cout << "----------------------- ";
cout << "index = " << i << endl;
cout << HashTable[i]->ID << endl;
cout << HashTable[i]->name << endl;
cout << "# of items = " << number << endl;
cout << "----------------------- ";
}
}
int hasht::Hash(string key)
{
string value = key.substr(2, 2);
int index = stoi(value);
return index;
}
main.cpp
#include "hash.h"
int main()
{
hasht hasha;
string id;
string NAME;
string line;
string a[25][2];
int b = 0;
ifstream myfile("C:\Users\aleXD\Desktop\hw4data.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
id = line.substr(0, 4);
NAME = line.substr(5);
//cout << id << endl << NAME << endl << endl;
a[b][0] = id;
a[b][1] = NAME;
b++;
//hasha.insert(id, NAME);
}
myfile.close();
}
else cout << "Unable to open file";
//show(a);
//system("pause");
string cont;
string cant;
for (int i = 0; i < 25; ++i)
{
cont = a[i][0];
cant = a[i][1];
cout << cont << endl << cant << endl << endl;
hasha.insert(a[i][0], a[i][1]);
}
//hasha.PrintTable();
int num;
string sid, sname;
cout << "1. Find student by ID." << endl << "2. Exit." << endl;
cin >> num;
if (num == 1)
{
cout << "Enter student ID: ";
cin >> sid;
sname = hasha.retrieve(sid);
if (sname == "")
cout << "There is no student with that ID." << endl;
else
cout << sname << endl;
}
else if (num == 2)
{
exit(1);
}
else
{
die("Invalid Input");
}
return 0;
system("pause");
}
bool die(const string &msg)
{
cout << "Fatal error : " << msg << endl;
exit(EXIT_FAILURE);
}
void show(string a[25][2])
{
for (int i = 0; i < 25; ++i)
{
for (int j = 0; j < 2; ++j)
{
cout << a[i][j] << endl;
}
cout << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.