Advertisement
Guest User

Vector Class revised 2

a guest
Feb 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.94 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);
  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){
  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.     T number;
  144.     cin >> number;
  145.     data[tempindex] = number;
  146.     vector_size++;
  147. }
  148.  
  149. template<typename T>
  150. void BasicVector<T>::pop_back(){
  151.     vector_size--;
  152.     data[vector_size] = 0;
  153. }
  154.  
  155. template<typename T>
  156. int BasicVector<T>::size(){
  157.     return vector_size;
  158. }
  159.  
  160. template<typename T>
  161. int BasicVector<T>::capacity(){
  162.     return vector_capacity;
  163. }
  164.  
  165. template<typename T>
  166. void BasicVector<T>::print(){
  167.     cout << "elements(" << vector_size << "): ";
  168.     for (int i = 0; i < vector_size; i++) {
  169.         cout << data[i] << " ";
  170.     }
  171.     cout << endl;
  172. }
  173.  
  174. template<typename T>
  175. void run()
  176. {
  177.    
  178. }
  179.  
  180. int main()
  181. {
  182.     int type;
  183.     int runs;
  184.     do{
  185.         if (runs > 1){
  186.             cout << endl <<"You have made an invalid selection. Please try again." << endl;
  187.         }
  188.     cout << "Specify what data type to store in vector: ";
  189.     cout << "1) int" << endl << "2) float" << endl;
  190.     cout << "3) double" << endl << "4) string" << endl << "5) bool" << endl;
  191.         cin >> type;
  192.         runs++;
  193.         cout << endl;
  194.     }while (type > 5 || type < 1);
  195.    
  196.     int capacity;
  197.     cout << "Enter starting capacity of vector: ";
  198.     cin >> capacity;
  199.     cout << "Now accepting commands (quit to exit program):" << endl;
  200.   /*
  201.    if (type == 1) {
  202.         BasicVector<int> Vector(capacity);
  203.        
  204.     }else if (type == 2){
  205.         BasicVector<float> Vector(capacity);
  206.     }else if (type == 3){
  207.         BasicVector<double> Vector(capacity);
  208.     }else if (type == 4){
  209.         BasicVector<string>(capacity);
  210.     }else if (type == 5){
  211.         BasicVector<bool>(capacity);
  212.     }
  213.     */
  214.     BasicVector<int> Vector(capacity);
  215.  
  216.     bool run = true;
  217.     // Implement command prompt loop
  218.     while(run == true)
  219.     {
  220.         string command;
  221.         cout << "> ";
  222.         cin >> command;
  223.        
  224.         if (command == "at") {
  225.             int tempindex;
  226.             cin >> tempindex;
  227.             cout << vector.at(tempindex) << endl;
  228.         } else if (command == "get"){
  229.             int tempindex;
  230.             cin >> tempindex;
  231.             cout << vector[(tempindex)] << endl;
  232.         }else if (command == "front"){
  233.             cout << vector.front() << endl;
  234.         }else if (command == "back"){
  235.            cout << vector.back() << endl;
  236.         }else if (command == "insert"){
  237.             int tempindex;
  238.             cin >> tempindex;
  239.             vector.insert(tempindex);
  240.         }else if (command == "push"){
  241.             int tempvalue;
  242.             cin >> tempvalue;
  243.             vector.push_back(tempvalue);
  244.         }else if (command == "pop"){
  245.             vector.pop_back();
  246.         }else if (command == "size"){
  247.             cout << vector.size() << endl;
  248.         } else if (command == "capacity"){
  249.             cout << vector.capacity() << endl;
  250.         }else if (command == "print"){
  251.             vector.print();
  252.         }else if (command == "quit"){
  253.             run = false;
  254.         }
  255.     }
  256.    
  257.     return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement