Guest User

Vector Class

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