Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1.  
  2. #include "BinomialQueue.h"
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <vector>
  8. #include <string>
  9. #include <queue>
  10. #include <stack>
  11. #include <unordered_map>
  12.  
  13. using namespace std;
  14.  
  15.  
  16. //class to test insert and delete
  17. //holds an instance of Binomial Queue
  18. class TestInsertAndDelete
  19. {
  20. public:
  21.     TestInsertAndDelete() {};
  22.     ~TestInsertAndDelete() {};
  23.    
  24.     //function to load each item in the file into the queue and hash table
  25.     void load(const string &queue_filename)
  26.     {
  27.         int input_number_;
  28.         ifstream infile;
  29.         //counter to keep track of elements that were inserted
  30.         int number_of_elements_ = 0;
  31.        
  32.         //opens input file
  33.         infile.open(queue_filename);
  34.        
  35.         //checks if input file was opened or not
  36.         if(infile.fail())
  37.         {
  38.             cout<<"Queue file could not be found."<<endl;
  39.             return;
  40.         }
  41.         //reads each number from the input file and adds them into the queue and table
  42.         while(infile >> input_number_)
  43.         {
  44.             //inserts the number
  45.             object.insert(input_number_);
  46.             //increases counter by 1
  47.             number_of_elements_++;
  48.         }
  49.         //close queue file
  50.         infile.close();
  51.        
  52.         cout<<"Success inserting " << number_of_elements_ << " into the queue. The minimum element is "<< object.findMin() << endl;
  53.        
  54.     }
  55.    
  56.     //function to read numbers from a file and delete it from the table and queue
  57.     void deleteNumber(const string &check_filename)
  58.     {
  59.         int input_number_;
  60.         ifstream infile;
  61.        
  62.         //opens deletion file
  63.         infile.open(check_filename);
  64.        
  65.         //checks if deletion file was opened or not
  66.         if(infile.fail())
  67.         {
  68.             cout<<"Check file could not be found."<<endl;
  69.             return;
  70.         }
  71.         //reads each number from the deletion file and deletes them from the queue and table
  72.         while(infile >> input_number_)
  73.         {
  74.             if(object.contains(input_number_) == true){
  75.  
  76.                 //checks if the queue is empty if it is, display the number deleted and that the queue is empty
  77.                 if(object.isEmpty() == true)
  78.                 {
  79.                     cout<<input_number_<<" Deletion successful - New minimum is nothing"<<endl;
  80.                 }
  81.                 //if not display the number deleted and the new minimum after the deletion
  82.                 else
  83.                 {
  84.                     cout<<input_number_<<" Deletion successful - New minimum is "<<object.findMin()<<endl;
  85.                    
  86.                 }
  87.             }
  88.         }
  89.     }
  90. private:
  91.     //instance of the binomial queue class
  92.     BinomialQueue<int> object;
  93.    
  94. };
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. int
  102. main(int argc, char **argv) {
  103.     if (argc != 3) {
  104.         cout << "Usage: " << argv[0] << " <createqueuefile> <checksearchfile/checkdeletefile> " << endl;
  105.         return 0;
  106.     }
  107.    
  108.     const string program_type(argv[0]);
  109.     const string create_queue_filename(argv[1]);
  110.     const string check_filename(argv[2]);
  111.    
  112.  
  113.     TestInsertAndDelete testing_insert_and_delete;
  114.     testing_insert_and_delete.load(create_queue_filename);
  115.     testing_insert_and_delete.deleteNumber(check_filename);
  116.    
  117.  
  118.    
  119.    
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement