Question
I'm working in C++ and the project im'm working on involves a heap. Below is what the assignment have to do. Below that is what
I'm working in C++ and the project im'm working on involves a heap.
Below is what the assignment have to do. Below that is what code I have already. Where i'm stuck is i don't know how store the input from the file correctly. I don't know what data structure to use. I don't want you to finish this for me, I just need help getting passed menu item number 2. How do I store the input, right now i have each stored individually, but how do i keep them together so i can put on the heap where they are linked?
PS: I don't want generic code using a heap implemenatation(i got that on a previous quesition), i want the data strucutres to use here.
Problem: Write a menu-driven C++ program to implement operations on a heap and binary file I/O.
Inputs: 1. Initial input file is stored in an external text file named as cs316p2.dat.
Each line in the file contains a record with the following format:
FirstName LastName ID GPA Where FirstName and LastName are strings of at most 20 characters, ID is an integer, and GPA is a float.
2. Interactive inputs: (1)
(2) (3) (4)
(5) (6) (7) (8)
your program should start with the following menu.
(1)Read data from external text file.
/* Prompt the user to enter an external input file name or use default file name. */
(2) Build a max heap in terms of ID or LastName. /* prompt for selection */ (3)Add a new record to max heap. /* prompt for entering new record */ (4)Delete a record from the max heap.
/* Prompt for ID or LastName*/ (5)Print sorted list from max heap in ascending order based on ID or LastName.
(6)Save a sorted list to external binary file named as 316p2.rec.
(7) Load the binary file and print the list by one record per line.
(8) Quit.
Outputs: print all warning or error messages to screen.
Testing: create your testing data file.
Other Requirements: Define and implement a heap ADT. Your program should be fail-safe.
main.cpp
#include "heap.hpp"
#include
#include
#include
using std::cout;
using std::cin;
using std::endl;
// declaring functions
int menu();
void readFile();
void buildMaxHeap();
void buildMaxHeapLastName();
void buildMaxHeapID();
void addRecord();
void deleteRecord();
void printHeap();
int main(){
// a start label to goto so when they pick a task, they can continue to choose other options until they break
start:
while(true)
{
int option = menu();
switch(option){
case 1 :
readFile();
cout << "You've read in the file ";
goto start;
case 2 :
buildMaxHeap();
cout << "You've built the max heap! ";
goto start;
case 3 :
addRecord();
cout << "You've added a new record to the max heap ";
goto start;
case 4 :
deleteRecord();
cout << "You've deleted a record from the heap ";
goto start;
case 5 :
printHeap();
cout << " You've printed the sorted heap ";
goto start;
case 6 :
cout << " ";
goto start;
case 7 :
cout << " ";
goto start;
default :
break;
}
break;
}
return 0;
}
void buildMaxHeap()
{
start:
cout << "Do you want to build heap on (1)LastName or (2)ID? ";
int option;
cin >> option;
if(option == 1)
{
cout<< "Congrats, you choose LastName ";
buildMaxHeapLastName();
}
else if(option == 2)
{
cout<< "you choose ID ";
buildMaxHeapID();
}
else
{goto start;}
}
void buildMaxHeapLastName()
{
std::ofstream infile;
infile.open ("cs316p2.dat");
//Check for Error
if (infile.fail())
{
std::cerr << "Error Opening File" << endl;
exit(1);
}
std::string line, firstName, lastName;
int ID;
float GPA;
while(!infile.eof(std::getline(cin, line, " "))
{
infile >> firstName >> LastName >> ID >> GPA;
push(LastName);
}
infile.close();
}
void buildMaxHeapID()
{
std::ofstream infile;
infile.open ("cs316p2.dat");
//Check for Error
if (infile.fail())
{
std::cerr << "Error Opening File" << endl;
exit(1);
}
std::string line, firstName, lastName;
int ID;
float GPA;
while(!infile.eof(std::getline(cin, line, " "))
{
infile >> firstName >> LastName >> ID >> GPA;
push(ID);
}
infile.close();
}
//uses default name, not allowing user to enter
void readFile()
{
std::ofstream infile;
infile.open ("cs316p2.dat");
//Check for Error
if (infile.fail())
{
std::cerr << "Error Opening File" << endl;
exit(1);
}
infile.close();
}
int menu()
{
cout << "Please choose one of the following ";
cout << "(1) Read data from external text file. ";
cout << "(2) Build a max heap in terms of ID or LastName. ";
cout << "(3) Add a new record to max heap.. ";
cout << "(4) Delete a record from the max heap. ";
cout << "(5) Print sorted list from max heap in ascending order based on ID or LastName. ";
cout << "(6) Save a sorted list to external binary file named as 316p2.rec. ";
cout << "(7) Load the binary file and print the list by one record per line. ";
cout << "(8) Quit ";
// get what number they entered, not worried about anything else
int option;
cin.ignore();
cin >> option;
return option;
}
heap.hpp
#include
#include
using std::vector;
using std::cout;
using std::out_of_range;
using std::swap;
class Heap
{
private:
// vector to store heap elements
vector A;
// return Parent of A[i]
// don't call this function if it is already a root node
int Parent(int i)
{
return (i - 1) / 2;
}
// return left child of A[i]
int Left(int i)
{
return (2 * i + 1);
}
// return right child of A[i]
int Right(int i)
{
return (2 * i + 2);
}
// Recursive Heapify-down algorithm
// the node at index i and its two direct children
// violates the heap property
void heapify_down(int i)
{
// get left and right child of node at index i
int left = Left(i);
int right = Right(i);
int largest = i;
// compare A[i] with its left and right child
// and find largest value
if (left < size() && A[left] > A[i])
largest = left;
if (right < size() && A[right] > A[largest])
largest = right;
// swap with child having greater value and
// call heapify-down on the child
if (largest != i) {
swap(A[i], A[largest]);
heapify_down(largest);
}
}
// Recursive Heapify-up algorithm
void heapify_up(int i)
{
// check if node at index i and its Parent violates
// the heap property
if (i && A[Parent(i)] < A[i])
{
// swap the two if heap property is violated
swap(A[i], A[Parent(i)]);
// call Heapify-up on the Parent
heapify_up(Parent(i));
}
}
public:
// return size of the heap
unsigned int size()
{
return A.size();
}
// function to check if heap is empty or not
bool empty()
{
return size() == 0;
}
// insert key into the heap
void push(int key)
{
// insert the new element to the end of the vector
A.push_back(key);
// get element index and call heapify-up procedure
int index = size() - 1;
heapify_up(index);
}
// function to remove element with highest priority (present at root)
void pop()
{
try {
// if heap has no elements, throw an exception
if (size() == 0)
throw out_of_range("Vector::at() : "
"index is out of range(Heap underflow)");
// replace the root of the heap with the last element
// of the vector
A[0] = A.back();
A.pop_back();
// call heapify-down on root node
heapify_down(0);
}
// catch and print the exception
catch (const out_of_range& oor) {
cout << " " << oor.what();
}
}
// function to return element with highest priority (present at root)
int top()
{
try {
// if heap has no elements, throw an exception
if (size() == 0)
throw out_of_range("Vector::at() : "
"index is out of range(Heap underflow)");
// else return the top (first) element
return A.at(0); // or return A[0];
}
// catch and print the exception
catch (const out_of_range& oor) {
cout << " " << oor.what();
}
}
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started