Advertisement
bbbbbb107

Vector Class revised 1

Feb 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. //---------------------------------------------------------------------
  2. // Name: Bowen Butler
  3. // Email: bnb5329@psu.edu
  4. // Class: CMPSC 122, Section 3
  5. // Program 2.2
  6. // Due Date: February 28, 2018, 11:59 PM
  7. //
  8. // Description: This program implements a basic integer vector
  9. // and provides the user with an interactive prompting system
  10. // to issue commands over an instance of our BasicVector class.
  11. //
  12. // Acknowledgements:
  13. // 1. Please use this portion of the banner comment to list
  14. // any resources or individuals you consulted on the creation
  15. // of this program.
  16. //---------------------------------------------------------------------
  17.  
  18. #include <iostream>
  19. #include <string>
  20. #include <stdlib.h>
  21.  
  22. using namespace std;
  23.  
  24. template<typename T>
  25. class BasicVector
  26. {
  27. private:
  28.     int vector_size;
  29.     int vector_capacity;
  30.     T* data;
  31.    
  32.     void resize();
  33.    
  34. public:
  35.     BasicVector() {}; // default constructor does nothing
  36.     BasicVector(int);
  37.     ~BasicVector();
  38.    
  39.     T& at(int);
  40.     T& operator[](int);
  41.     T& front();
  42.     T& back();
  43.    
  44.     void push_back(T);
  45.     void insert(int, T);
  46.     void pop_back();
  47.    
  48.     int size();
  49.     int capacity();
  50.    
  51.     void print();
  52. };
  53.  
  54. template<typename T>
  55. void BasicVector<T>::resize()
  56. {
  57.     int* tempdata = data;
  58.     vector_capacity = (vector_capacity * 2);
  59.    
  60.     data = new int [vector_size];
  61.     for (int i = 0; i < vector_size; i++) {
  62.         data [i] = tempdata[i];
  63.         tempdata[i] = 0;
  64.     }
  65.     delete [] tempdata;
  66. }
  67.  
  68. template<typename T>
  69. BasicVector<T>::BasicVector(int capacity)
  70. {
  71.     if (capacity <= 16) {
  72.         vector_capacity = 16;
  73.     }else {
  74.         int power = 16;
  75.         vector_capacity = power;
  76.         do {
  77.             power = (power * 2);
  78.             vector_capacity = power;
  79.         }while (power <= capacity);
  80.     }
  81.     data = new int [vector_size];
  82. }
  83.  
  84. template<typename T>
  85. BasicVector<T>::~BasicVector()
  86. {
  87.     for (int i = 0; i < vector_size; i++) {
  88.         data[i] = 0;
  89.     }
  90.     delete [] data;
  91.    
  92.     vector_size = 0;
  93.     vector_capacity = 0;
  94. }
  95.  
  96. template<typename T>
  97. T& BasicVector<T>::at(int index)
  98. {
  99.     if (index > vector_size) {
  100.         exit(1);
  101.     }
  102.     return data[index];
  103. }
  104.  
  105. template<typename T>
  106. T& BasicVector<T>::operator[](int index)
  107. {
  108.     if (index > vector_size) {
  109.         exit(1);
  110.     }
  111.     return data[index];
  112. }
  113.  
  114. // Add in the rest of the public functions
  115. template<typename T>
  116. T& BasicVector<T>::front(){
  117.     return data[0];
  118. }
  119.  
  120. template<typename T>
  121. T& BasicVector<T>::back(){
  122.     return data[(vector_size - 1)];
  123. }
  124.  
  125. template<typename T>
  126. void BasicVector<T>::push_back(T number){
  127.     if (vector_size == vector_capacity) {
  128.         resize();
  129.     }
  130.     vector_size++;
  131.    
  132.     data[(vector_size - 1)] = number;
  133. }
  134.  
  135. template<typename T>
  136. void BasicVector<T>::insert(int tempindex, T number){
  137.     if (vector_size == vector_capacity) {
  138.         resize();
  139.     }
  140.     for (int i = vector_size; i >= tempindex; i--) {
  141.         data[i] = data [i];
  142.     }
  143.     data[tempindex] = number;
  144.     vector_size++;
  145. }
  146.  
  147. template<typename T>
  148. void BasicVector<T>::pop_back(){
  149.     vector_size--;
  150.     data[vector_size] = 0;
  151. }
  152.  
  153. template<typename T>
  154. int BasicVector<T>::size(){
  155.     return vector_size;
  156. }
  157.  
  158. template<typename T>
  159. int BasicVector<T>::capacity(){
  160.     return vector_capacity;
  161. }
  162.  
  163. template<typename T>
  164. void BasicVector<T>::print(){
  165.     cout << "elements(" << vector_size << "): ";
  166.     for (int i = 0; i < vector_size; i++) {
  167.         cout << data[i] << " ";
  168.     }
  169.     cout << endl;
  170. }
  171.  
  172.  
  173. int main()
  174. {
  175.     int type;
  176.     int runs;
  177.     do{
  178.         if (runs > 1){
  179.             cout << endl <<"You have made an invalid selection. Please try again." << endl;
  180.         }
  181.     cout << "Specify what data type to store in vector: ";
  182.     cout << "1) int" << endl << "2) float" << endl;
  183.     cout << "3) double" << endl << "4) string" << endl << "5) bool" << endl;
  184.         cin >> type;
  185.         runs++;
  186.         cout << endl;
  187.     }while (type > 5 || type < 1);
  188.    
  189.     int capacity;
  190.     cout << "Enter starting capacity of vector: ";
  191.     cin >> capacity;
  192.     cout << "Now accepting commands (quit to exit program):" << endl;
  193.    
  194.     if (type == 1) {
  195.         BasicVector<int> Vector(capacity);
  196.     }else if (type == 2){
  197.         BasicVector<float> Vector(capacity);
  198.     }else if (type == 3){
  199.         BasicVector<double> Vector(capacity);
  200.     }else if (type == 4){
  201.         BasicVector<string>(capacity);
  202.     }else if (type == 5){
  203.         BasicVector<bool>(capacity);
  204.     }
  205.  
  206.  
  207.     bool run = true;
  208.     // Implement command prompt loop
  209.     while(run == true)
  210.     {
  211.         string command;
  212.         cout << "> ";
  213.         cin >> command;
  214.        
  215.         if (command == "at") {
  216.             int tempindex;
  217.             cin >> tempindex;
  218.             cout << vector.at(tempindex) << endl;
  219.         } else if (command == "get"){
  220.             int tempindex;
  221.             cin >> tempindex;
  222.             cout << vector[(tempindex)] << endl;
  223.         }else if (command == "front"){
  224.             cout << vector.front() << endl;
  225.         }else if (command == "back"){
  226.             cout << vector.back() << endl;
  227.         }else if (command == "insert"){
  228.             int tempindex;
  229.             int tempvalue;
  230.             cin >> tempindex;
  231.             cin >> tempvalue;
  232.             vector.insert(tempindex, tempvalue);
  233.         }else if (command == "push"){
  234.             int tempvalue;
  235.             cin >> tempvalue;
  236.             vector.push_back(tempvalue);
  237.         }else if (command == "pop"){
  238.             vector.pop_back();
  239.         }else if (command == "size"){
  240.             cout << vector.size() << endl;
  241.         } else if (command == "capacity"){
  242.             cout << vector.capacity() << endl;
  243.         }else if (command == "print"){
  244.             vector.print();
  245.         }else if (command == "quit"){
  246.             run = false;
  247.         }
  248.     }
  249.    
  250.     return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement