C++ First, create a structure (named Student ) that will describe each of Genghi
ID: 3831364 • Letter: C
Question
C++
First, create a structure (named Student) that will describe each of Genghis Khent's 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 (like "help me"). 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:
1234 hello world
1444 Help Me
7722 need help
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
#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
class Product
{
private:
char idNum[12];
char Pname[46];
double Price;
int Quantity;
public:
Product();
Product(char [], char [],double,int);
double getPrice();
int getQuantity();
void setProductCode( char []);
void setName(char[]);
void setPrice(double);
void setQuantity(int);
int fulfillOrder(int);
void print();
};
Product::Product()
{
idNum[12]={''};
Pname[46]={''};
}
Product::Product(char ID[],char PN[], double Pr,int Qu )
{
setProductCode( ID);
setName(PN);
setPrice(Pr);
setQuantity(Qu);
}
double Product::getPrice()
{
cout<<"Price: "<<Price<<endl;
return Price;
}
int Product::getQuantity()
{
cout<<"Quantitiy: "<<Quantity<<endl;
return Quantity;
}
void Product::setProductCode(char ID[])
{
strcpy(ID,ID);
}
void Product::setName(char PN[])
{
strcpy(PN,PN);
}
void Product::setPrice( double newPrice)
{
if(newPrice>=0)
{
Price=newPrice;
}
else
{
Price=0;
}
}
void Product::setQuantity(int newQuantity)
{
if(newQuantity>=0)
{
Quantity=newQuantity;
}
else
{
Quantity=0;
}
}
int Product::fulfillOrder( int orderq)
{
if( orderq<0)
{
cout<<"Error"<<endl;
orderq=0;
cout<<"Shipped: "<<orderq<<endl;
}
else if( orderq<=Quantity)
{
orderq=Quantity;
Quantity-=orderq;
cout<<"Shipped: "<<orderq<<endl;
}
else
{
orderq=Quantity;
orderq=0;
cout<<"Shipped: "<<orderq<<endl;
}
}
void Product::print()
{
cout<<"Product ID: "<<idNum<<" Product Name: "<<Pname<<" Price: "<<Price<<" Quantity: "<<Quantity<<endl;
}
int main()
{
Product product1=Product();
Product product2=Product{"22222222222","Virtual Education Pack",0.99,31};
cout<<"Product 1"<<endl;
product1.print();
Product product3=Product{"33333333333","Dehydrated Water Bed",-12.99,-6};
product2.print();
cout<<"Product 1"<<endl;
product1.setProductCode("11111111111");
product1.setName("Flowbee Pet Groomer");
product1.setPrice(399.99);
product1.setQuantity(28);
product1.print();
product2.getPrice();
product2.getQuantity();
product1.print();
product1.fulfillOrder(-5);
product1.print();
product1.print();
product1.fulfillOrder(12);
product1.print();
product2.print();
product2.fulfillOrder(4);
product2.print();
product2.print();
product2.fulfillOrder(45);
product2.print();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.