Advertisement
Limited_Ice

partnum with comments

May 16th, 2021
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.21 KB | None | 0 0
  1. #pragma once
  2. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. //  Class Definition
  4.  
  5. class partnum {
  6. public: //  public members and methods are listed first for ease of use.  Public methods are defined later in the file
  7.  
  8.     partnum(int tparnumber, int tquantity) {    //////////////////////////////////////////////////
  9.         partnumber = tparnumber;                //  This constructor is used to create the      //
  10.         quantity = tquantity;                   //  root node and leave the pointer to parent   //
  11.         parent = nullptr;                       //  set as a nullptr.   This is called directly //
  12.         left = nullptr;                         //  from main.                                  //
  13.         right = nullptr;                        //////////////////////////////////////////////////
  14.     }
  15.  
  16.     partnum(int tparnumber, int tquantity, partnum* tparent) {  //////////////////////////////////////////////////
  17.         partnumber = tparnumber;                                //  This is the primary constructor that is     //
  18.         quantity = tquantity;                                   //  called from the root node rather than       //
  19.         parent = tparent;                                       //  the main function.  It has a pointer to     //
  20.         left = nullptr;                                         //  its parent node.                            //
  21.         right = nullptr;                                        //////////////////////////////////////////////////
  22.     }
  23.  
  24.     void addnewpart(int, int);  //  This is the function that calls the primary constructor after the root has been established.
  25.     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.
  26.     void search();              //  This function displays the information about a given node.  (only quantity at the current time)
  27.     void initdel();             //  This function calls the private delete function.
  28.     void initsave();            //  This function calls the private save function as well as building the .txt file for storage.
  29.     void change();              //  This is used to update the quantity of a part in the system.
  30.    
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. private:
  33.  
  34.     int partnumber;
  35.     int quantity;
  36.  
  37.     partnum* left;
  38.     partnum* right;
  39.     partnum* parent;
  40.  
  41.     bool search(int tpartnumber, partnum* temp) {
  42.         if (temp->partnumber == tpartnumber) {
  43.             cout << "partnumber: " << temp->partnumber << "\nquantity: " << temp->quantity << endl;
  44.             return true;
  45.         }                                          
  46.  
  47.         else if (tpartnumber < temp->partnumber) {          ///////////////////////////////////////////////////
  48.                                                             //
  49.             if (temp->left == nullptr)                      //  This is the private side of the public search
  50.                 return false;                               //  function and has access to the private class
  51.             else
  52.                 temp->search(tpartnumber, temp->left);      //  members and functions that do not need to be seen
  53.         }                                                   //  by somebody simply implementing the class.
  54.                                                             //
  55.         else if (tpartnumber > temp->partnumber) {          //
  56.             if (temp->right == nullptr)                     //
  57.                 return false;                               //
  58.             else {                                          //
  59.                 temp->search(tpartnumber, temp->right);     //
  60.             }                                               //
  61.         }                                                   //
  62.     }
  63.  
  64.     void del(partnum* temp) {                               //////////////////////////////////////////////////
  65.                                                             //  the private aspect of the del function
  66.         partnum* hold = nullptr;                            //  is where deleting each node of the tree
  67.         while (true) {                                      //  is taken care of.
  68.             if (temp->left != nullptr)                      //
  69.                 temp = temp->left;                          //  It uses postorder traversal and updates the
  70.             else {                                          //  nodes as it goes through and deletes them to ensure
  71.                 if (temp->right != nullptr)                 //  that there are no hanging pointers and that all
  72.                     temp = temp->right;                     //  memory dynamically allocatedon the heap is freed up.
  73.                 else {                                      //
  74.                     if (temp->parent != nullptr) {          //
  75.                         hold = temp;                        //
  76.                         temp = temp->parent;                //
  77.                         if (temp->left == hold)             //
  78.                             temp->left = nullptr;           //
  79.                         if (temp->right == hold)            //
  80.                             temp->right = nullptr;          //
  81.                         delete hold;                        //
  82.                         hold = nullptr;                     //
  83.                     }                                       //
  84.                     else {                                  ////////////////////////////////////////////////////
  85.                         return;
  86.                     }
  87.  
  88.                 }
  89.             }
  90.         }
  91.     }
  92.  
  93.     int save(partnum* temp, vector<int>& logpn, vector<int>& logqt) {                       //////////////////////////////////////////////////
  94.         partnum* hold = nullptr;                                                            //
  95.         vector<partnum*> track;                                                             //  The private save function uses iteration
  96.         bool check;                                                                         //  to build the two vectors that will be used
  97.         while (true) {                                                                      //  to build the save file/
  98.             if (temp->left != nullptr && temp->ismember(track, temp->left) == false)        //
  99.                 temp = temp->left;                                                          //  I used this function to build the vectors rather
  100.             else {                                                                          //  than write to the file directly because this
  101.                 if (temp->ismember(track, temp) == false) {                                 //  allowed me to find the centermost number on the
  102.                     logpn.push_back(temp->partnumber);                                      //  tree which should become the new root node when
  103.                     logqt.push_back(temp->quantity);                                        //  the tree is rebuilt from the file.
  104.                     track.push_back(temp);                                                  //
  105.                 }                                                                           //  Unlike the del function this one uses inorder
  106.                 if (temp->right != nullptr && temp->ismember(track, temp->right) == false)  //  traversal so that the vectors go from the smallest
  107.                     temp = temp->right;                                                     //  (numerically) to the largest part number.
  108.                 else {                                                                      //
  109.                     if (temp->parent != nullptr) {                                          //
  110.                         temp = temp->parent;                                                //
  111.                     }                                                                       //
  112.                     else
  113.                         break;                                                              //
  114.                 }                                                                           // 
  115.             }                                                                               //////////////////////////////////////////////////
  116.                
  117.  
  118.         }
  119.         return track.size();
  120.     }
  121.  
  122.     bool change(int tpartnumber, int tquantity, partnum* temp) {                                            //////////////////////////////////////////////////
  123.         while (true) {                                                                                      // 
  124.             if (temp->partnumber == tpartnumber) {                                                          //  The change function uses iteration to
  125.                 temp->quantity += tquantity;                                                                //  find the correct node to update its quantity.
  126.                 cout << " PN: " << tpartnumber << "quantity changed from " << temp->quantity - tquantity << //
  127.                     "to " << temp->quantity << endl;                                                        //  it traverses the tree with postorder traversal
  128.                 return true ;                                                                               //
  129.             }                                                                                               //
  130.                                                                                                             //
  131.             else if (tpartnumber < temp->partnumber) {                                                      //
  132.                                                                                                             //
  133.                 if (temp->left == nullptr)                                                                  //
  134.                     return false;                                                                           //
  135.                 else
  136.                     temp = temp->left;                                                                      //
  137.             }                                                                                               //
  138.                                                                                                             //
  139.             else if (tpartnumber > temp->partnumber) {                                                      //
  140.                 if (temp->right == nullptr)                                                                 //
  141.                     return false;                                                                           //
  142.                 else {                                                                                      //
  143.                     temp = temp->right;                                                                     //
  144.                 }
  145.             }
  146.         }
  147.     }
  148.  
  149.     bool ismember(vector<partnum*> track, partnum* temp) {          ////////////////////////////////////////////////////
  150.         int size = track.size();                                    //  This version of the ismember() function is used
  151.                                                                     //  by the save() function to ,ake sure that the
  152.         for (int i = 0; i < size; i++) {                            //  same node isn't entered into the save file
  153.             if (track[i] == temp)                                   //  multiple times.
  154.                 return true;                                        //
  155.         }                                                           //
  156.         return false;                                               ////////////////////////////////////////////////////
  157.  
  158.     }
  159.  
  160.     bool ismember(int tpartnumber, partnum* temp) {             ////////////////////////////////////////////////////
  161.         if (temp->partnumber == tpartnumber) {                  //
  162.             return true;                                        //  This version of the is used by the build()
  163.         }                                                       //  function to ensure that the same node is
  164.                                                                 //  not accidentally added to the tree twice.
  165.         else if (tpartnumber < temp->partnumber) {              //
  166.                                                                 //  This is a hack because I didn't want to
  167.             if (temp->left == nullptr)                          //  fine tune how the while loop iterates through
  168.                 return false;                                   //  save file yet.  In the future I will remove
  169.             else
  170.                 temp->ismember(tpartnumber, temp->left);        //  this function as well as the build() function
  171.         }                                                       //  and rely only on the addnewpart() function
  172.                                                                 //  to rebuild the tree on startup.
  173.         else if (tpartnumber > temp->partnumber) {              //
  174.             if (temp->right == nullptr)                         //
  175.                 return false;                                   //
  176.             else {                                              //
  177.                 temp->ismember(tpartnumber, temp->right);       ////////////////////////////////////////////////////
  178.             }
  179.         }
  180.     }
  181. };
  182.  
  183.  
  184. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  185. //  Public Method Definitions
  186.  
  187.  
  188. void partnum::addnewpart(int tpartnumber, int tquantity) {  //  this function adds a new part node to the tree.
  189.  
  190.     partnum* temp = this;
  191.     char choice;
  192.     if (temp->ismember(tpartnumber, this) == true) {
  193.         cout << "that part is already in the database, would you like to adjust its quantity?\ny/n: ";
  194.         cin >> choice;              ///////////////////////////////////////
  195.         tolower(choice);            // 
  196.         switch (choice) {           //  This check prevents a node from
  197.         case 'y': {                 //  being duplicated.  If the part
  198.             this->change();         //  number already exists in a node
  199.             break;                  //  then the function passes to the
  200.         }                           //  change() function to change quantity
  201.         case 'n':                   //  instead.
  202.             return;                 //
  203.         default:                    ///////////////////////////////////////
  204.             cout << "please use only \"y\" or \"n\" for yes or no." << endl;
  205.         }
  206.         return;
  207.     }
  208.        
  209.  
  210.     while (true) {
  211.  
  212.         if (tpartnumber < temp->partnumber) {                               /////////////////////////////////////
  213.             if (temp->left == nullptr) {                                    //
  214.                 temp->left = new partnum(tpartnumber, tquantity, temp);     //  If the part number doesn't already
  215.                 return;                                                     //  exist in the tree then this
  216.             }                                                               //  function uses iteration to place
  217.                                                                             //  the new node in the correct location
  218.             else if (temp->left != nullptr)                                 //  on the tree.
  219.                 temp = temp->left;                                          //
  220.         }                                                                   //
  221.                                                                             //
  222.                                                                             //
  223.         else if (tpartnumber > temp->partnumber) {                          //
  224.             if (temp->right == nullptr) {                                   //
  225.                 temp->right = new partnum(tpartnumber, tquantity, temp);    //
  226.                 return;                                                     //
  227.             }
  228.             else
  229.                 temp = temp->right;
  230.         }
  231.     }
  232. }
  233.  
  234. void partnum::search() {                                                    ///////////////////////////////////////
  235.     int tpartnumber;                                                        //
  236.     bool check;                                                             //  The public interface to the search()
  237.     cout << "You chose to look up a part\n\nenter the part number: ";       //  function gathers the required user input
  238.     cin >> tpartnumber;                                                     //  and passes the the correct information to
  239.     check = this->search(tpartnumber, this);                                //  the private search() function.
  240.     if (check == false)                                                     //
  241.         cout << "That part is not in the database." << endl;                ///////////////////////////////////////
  242. }
  243.  
  244. void partnum::initdel() {   //  This triggers the more in depth del() function in the private portion
  245.     this->del(this);        //  of the class.
  246. }
  247.  
  248. void partnum::initsave() {                                      ///////////////////////////////////////
  249.     remove("partdatabase.txt");                                 //
  250.     vector<int> logpn;                                          //  the initsave() removes the old save file,
  251.     vector<int> logqt;                                          //  and passes the correct data to the save()
  252.     int size;                                                   //  function to build the vectors that this
  253.     ofstream save;                                              //  function uses to format and creat the
  254.     size = this->save(this, logpn, logqt);                      //  partdatabase.txt file that is used to
  255.     save.open("partdatabase.txt");                              //  rebuild the database on the next start up.
  256.     if (size > 1) {                                             //
  257.         size = size / 2;                                        //
  258.         save << logpn[size] << " " << logqt[size] << endl;      //
  259.         logpn.erase(logpn.begin() + size);                      //
  260.         logqt.erase(logqt.begin() + size);                      //
  261.         size = logpn.size();                                    //
  262.         for (int i = 0; i < size; i++)                          //
  263.             save << logpn[i] << " " << logqt[i] << endl;        //
  264.                                                                 //
  265.     }                                                           //
  266.     else if (size == 1)                                         //
  267.         save << logpn[0] << " " << logqt[0] << endl;            //
  268.     else
  269.         cout << "error saving file" << endl;                    //
  270.     save.close();                                               ///////////////////////////////////////
  271. }
  272.  
  273. void partnum::change() {                                                                                /////////////////////////////////////
  274.     int tpartnumber, tquantity;                                                                         //
  275.     bool check;                                                                                         //  The public change() function
  276.     cout << "You have chosen to change a part's quantity\n\nWhat part would you like to change: ";      //  gathers the proper user input
  277.     cin >> tpartnumber;                                                                                 //  and sends the needed data to
  278.     cout << "Enter the change in quantity: ";                                                           //  the private change() function
  279.     cin >> tquantity;                                                                                   //  to make the changes.
  280.     check = this->change(tpartnumber, tquantity, this);                                                 //
  281.     if (check == false)                                                                                 //
  282.         cout << "That part was not in the database." << endl;                                           /////////////////////////////////////
  283.  
  284. }
  285.  
  286. void partnum::build(int tpartnumber, int tquantity) {                       /////////////////////////////////////
  287.     partnum* temp = this;                                                   //
  288.                                                                             //  The build() functio works almost
  289.     if (temp->ismember(tpartnumber, this) == true)                          //  identically to the addnewpart()
  290.         return;                                                             //  function.  The difference is that
  291.     while (true) {                                                          //  if this function finds that the
  292.         if (tpartnumber < temp->partnumber) {                               //  part being entered into the tree
  293.             if (temp->left == nullptr) {                                    //  already exists it simply terminates.
  294.                 temp->left = new partnum(tpartnumber, tquantity, temp);     //
  295.                 return;                                                     //  This function will be streamlined out
  296.             }                                                               //  of the class later on.
  297.                                                                             //
  298.             else if (temp->left != nullptr)                                 //
  299.                 temp = temp->left;                                          //
  300.         }                                                                   //                                 
  301.                                                                             //
  302.                                                                             //
  303.         else if (tpartnumber > temp->partnumber) {                          //
  304.             if (temp->right == nullptr) {                                   //
  305.                 temp->right = new partnum(tpartnumber, tquantity, temp);    //
  306.                 return;                                                     //
  307.             }                                                               //
  308.             else
  309.                 temp = temp->right;
  310.         }
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement