Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Class Definition
- class partnum {
- public: // public members and methods are listed first for ease of use. Public methods are defined later in the file
- partnum(int tparnumber, int tquantity) { //////////////////////////////////////////////////
- partnumber = tparnumber; // This constructor is used to create the //
- quantity = tquantity; // root node and leave the pointer to parent //
- parent = nullptr; // set as a nullptr. This is called directly //
- left = nullptr; // from main. //
- right = nullptr; //////////////////////////////////////////////////
- }
- partnum(int tparnumber, int tquantity, partnum* tparent) { //////////////////////////////////////////////////
- partnumber = tparnumber; // This is the primary constructor that is //
- quantity = tquantity; // called from the root node rather than //
- parent = tparent; // the main function. It has a pointer to //
- left = nullptr; // its parent node. //
- right = nullptr; //////////////////////////////////////////////////
- }
- void addnewpart(int, int); // This is the function that calls the primary constructor after the root has been established.
- void build(int, int); // This node also calls the primary constructor but is only used to buld the database when initially loaded from a file.
- void search(); // This function displays the information about a given node. (only quantity at the current time)
- void initdel(); // This function calls the private delete function.
- void initsave(); // This function calls the private save function as well as building the .txt file for storage.
- void change(); // This is used to update the quantity of a part in the system.
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private:
- int partnumber;
- int quantity;
- partnum* left;
- partnum* right;
- partnum* parent;
- bool search(int tpartnumber, partnum* temp) {
- if (temp->partnumber == tpartnumber) {
- cout << "partnumber: " << temp->partnumber << "\nquantity: " << temp->quantity << endl;
- return true;
- }
- else if (tpartnumber < temp->partnumber) { ///////////////////////////////////////////////////
- //
- if (temp->left == nullptr) // This is the private side of the public search
- return false; // function and has access to the private class
- else
- temp->search(tpartnumber, temp->left); // members and functions that do not need to be seen
- } // by somebody simply implementing the class.
- //
- else if (tpartnumber > temp->partnumber) { //
- if (temp->right == nullptr) //
- return false; //
- else { //
- temp->search(tpartnumber, temp->right); //
- } //
- } //
- }
- void del(partnum* temp) { //////////////////////////////////////////////////
- // the private aspect of the del function
- partnum* hold = nullptr; // is where deleting each node of the tree
- while (true) { // is taken care of.
- if (temp->left != nullptr) //
- temp = temp->left; // It uses postorder traversal and updates the
- else { // nodes as it goes through and deletes them to ensure
- if (temp->right != nullptr) // that there are no hanging pointers and that all
- temp = temp->right; // memory dynamically allocatedon the heap is freed up.
- else { //
- if (temp->parent != nullptr) { //
- hold = temp; //
- temp = temp->parent; //
- if (temp->left == hold) //
- temp->left = nullptr; //
- if (temp->right == hold) //
- temp->right = nullptr; //
- delete hold; //
- hold = nullptr; //
- } //
- else { ////////////////////////////////////////////////////
- return;
- }
- }
- }
- }
- }
- int save(partnum* temp, vector<int>& logpn, vector<int>& logqt) { //////////////////////////////////////////////////
- partnum* hold = nullptr; //
- vector<partnum*> track; // The private save function uses iteration
- bool check; // to build the two vectors that will be used
- while (true) { // to build the save file/
- if (temp->left != nullptr && temp->ismember(track, temp->left) == false) //
- temp = temp->left; // I used this function to build the vectors rather
- else { // than write to the file directly because this
- if (temp->ismember(track, temp) == false) { // allowed me to find the centermost number on the
- logpn.push_back(temp->partnumber); // tree which should become the new root node when
- logqt.push_back(temp->quantity); // the tree is rebuilt from the file.
- track.push_back(temp); //
- } // Unlike the del function this one uses inorder
- if (temp->right != nullptr && temp->ismember(track, temp->right) == false) // traversal so that the vectors go from the smallest
- temp = temp->right; // (numerically) to the largest part number.
- else { //
- if (temp->parent != nullptr) { //
- temp = temp->parent; //
- } //
- else
- break; //
- } //
- } //////////////////////////////////////////////////
- }
- return track.size();
- }
- bool change(int tpartnumber, int tquantity, partnum* temp) { //////////////////////////////////////////////////
- while (true) { //
- if (temp->partnumber == tpartnumber) { // The change function uses iteration to
- temp->quantity += tquantity; // find the correct node to update its quantity.
- cout << " PN: " << tpartnumber << "quantity changed from " << temp->quantity - tquantity << //
- "to " << temp->quantity << endl; // it traverses the tree with postorder traversal
- return true ; //
- } //
- //
- else if (tpartnumber < temp->partnumber) { //
- //
- if (temp->left == nullptr) //
- return false; //
- else
- temp = temp->left; //
- } //
- //
- else if (tpartnumber > temp->partnumber) { //
- if (temp->right == nullptr) //
- return false; //
- else { //
- temp = temp->right; //
- }
- }
- }
- }
- bool ismember(vector<partnum*> track, partnum* temp) { ////////////////////////////////////////////////////
- int size = track.size(); // This version of the ismember() function is used
- // by the save() function to ,ake sure that the
- for (int i = 0; i < size; i++) { // same node isn't entered into the save file
- if (track[i] == temp) // multiple times.
- return true; //
- } //
- return false; ////////////////////////////////////////////////////
- }
- bool ismember(int tpartnumber, partnum* temp) { ////////////////////////////////////////////////////
- if (temp->partnumber == tpartnumber) { //
- return true; // This version of the is used by the build()
- } // function to ensure that the same node is
- // not accidentally added to the tree twice.
- else if (tpartnumber < temp->partnumber) { //
- // This is a hack because I didn't want to
- if (temp->left == nullptr) // fine tune how the while loop iterates through
- return false; // save file yet. In the future I will remove
- else
- temp->ismember(tpartnumber, temp->left); // this function as well as the build() function
- } // and rely only on the addnewpart() function
- // to rebuild the tree on startup.
- else if (tpartnumber > temp->partnumber) { //
- if (temp->right == nullptr) //
- return false; //
- else { //
- temp->ismember(tpartnumber, temp->right); ////////////////////////////////////////////////////
- }
- }
- }
- };
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Public Method Definitions
- void partnum::addnewpart(int tpartnumber, int tquantity) { // this function adds a new part node to the tree.
- partnum* temp = this;
- char choice;
- if (temp->ismember(tpartnumber, this) == true) {
- cout << "that part is already in the database, would you like to adjust its quantity?\ny/n: ";
- cin >> choice; ///////////////////////////////////////
- tolower(choice); //
- switch (choice) { // This check prevents a node from
- case 'y': { // being duplicated. If the part
- this->change(); // number already exists in a node
- break; // then the function passes to the
- } // change() function to change quantity
- case 'n': // instead.
- return; //
- default: ///////////////////////////////////////
- cout << "please use only \"y\" or \"n\" for yes or no." << endl;
- }
- return;
- }
- while (true) {
- if (tpartnumber < temp->partnumber) { /////////////////////////////////////
- if (temp->left == nullptr) { //
- temp->left = new partnum(tpartnumber, tquantity, temp); // If the part number doesn't already
- return; // exist in the tree then this
- } // function uses iteration to place
- // the new node in the correct location
- else if (temp->left != nullptr) // on the tree.
- temp = temp->left; //
- } //
- //
- //
- else if (tpartnumber > temp->partnumber) { //
- if (temp->right == nullptr) { //
- temp->right = new partnum(tpartnumber, tquantity, temp); //
- return; //
- }
- else
- temp = temp->right;
- }
- }
- }
- void partnum::search() { ///////////////////////////////////////
- int tpartnumber; //
- bool check; // The public interface to the search()
- cout << "You chose to look up a part\n\nenter the part number: "; // function gathers the required user input
- cin >> tpartnumber; // and passes the the correct information to
- check = this->search(tpartnumber, this); // the private search() function.
- if (check == false) //
- cout << "That part is not in the database." << endl; ///////////////////////////////////////
- }
- void partnum::initdel() { // This triggers the more in depth del() function in the private portion
- this->del(this); // of the class.
- }
- void partnum::initsave() { ///////////////////////////////////////
- remove("partdatabase.txt"); //
- vector<int> logpn; // the initsave() removes the old save file,
- vector<int> logqt; // and passes the correct data to the save()
- int size; // function to build the vectors that this
- ofstream save; // function uses to format and creat the
- size = this->save(this, logpn, logqt); // partdatabase.txt file that is used to
- save.open("partdatabase.txt"); // rebuild the database on the next start up.
- if (size > 1) { //
- size = size / 2; //
- save << logpn[size] << " " << logqt[size] << endl; //
- logpn.erase(logpn.begin() + size); //
- logqt.erase(logqt.begin() + size); //
- size = logpn.size(); //
- for (int i = 0; i < size; i++) //
- save << logpn[i] << " " << logqt[i] << endl; //
- //
- } //
- else if (size == 1) //
- save << logpn[0] << " " << logqt[0] << endl; //
- else
- cout << "error saving file" << endl; //
- save.close(); ///////////////////////////////////////
- }
- void partnum::change() { /////////////////////////////////////
- int tpartnumber, tquantity; //
- bool check; // The public change() function
- cout << "You have chosen to change a part's quantity\n\nWhat part would you like to change: "; // gathers the proper user input
- cin >> tpartnumber; // and sends the needed data to
- cout << "Enter the change in quantity: "; // the private change() function
- cin >> tquantity; // to make the changes.
- check = this->change(tpartnumber, tquantity, this); //
- if (check == false) //
- cout << "That part was not in the database." << endl; /////////////////////////////////////
- }
- void partnum::build(int tpartnumber, int tquantity) { /////////////////////////////////////
- partnum* temp = this; //
- // The build() functio works almost
- if (temp->ismember(tpartnumber, this) == true) // identically to the addnewpart()
- return; // function. The difference is that
- while (true) { // if this function finds that the
- if (tpartnumber < temp->partnumber) { // part being entered into the tree
- if (temp->left == nullptr) { // already exists it simply terminates.
- temp->left = new partnum(tpartnumber, tquantity, temp); //
- return; // This function will be streamlined out
- } // of the class later on.
- //
- else if (temp->left != nullptr) //
- temp = temp->left; //
- } //
- //
- //
- else if (tpartnumber > temp->partnumber) { //
- if (temp->right == nullptr) { //
- temp->right = new partnum(tpartnumber, tquantity, temp); //
- return; //
- } //
- else
- temp = temp->right;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement