Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. /**
  2.  *  @author Huy
  3.  *  @date 11.20.18
  4.  *  @file h18.cpp
  5.  */
  6. #include <string>
  7. #include <iostream>
  8. #include <memory>
  9. using namespace std;
  10.  
  11. string STUDENT = "hnguyen771"; // Add your Canvas/occ-email ID
  12. #include "h18.h"
  13.  
  14. // Write your functions here
  15.  
  16. FlexArray& readData(istream& in, FlexArray& a)
  17. {
  18.     size_t capacity = INITIAL_CAPACITY, i = 0;
  19.     int num;
  20.     a.data_.reset(new int[INITIAL_CAPACITY]);
  21.  
  22.     while (in >> num)
  23.     {
  24.         a.data_[i] = num;
  25.         i++;
  26.         if (i == capacity)
  27.         {
  28.             capacity *= 2;
  29.             int * temp = new int [capacity];
  30.             for (size_t j = 0; j < i; j++) temp[j] = a.data_[j];
  31.             a.size_ = i;
  32.             a.data_.reset(temp);
  33.         }
  34.         else
  35.         {
  36.             int * temp = new int [capacity];
  37.             for (size_t j = 0; j < i; j++) temp[j] = a.data_[j];
  38.             a.size_ = i;
  39.             a.data_.reset(temp);
  40.         }
  41.     }
  42.     int * temp = new int[a.size_];
  43.     a.data_.reset(temp);
  44.     return a;
  45. }
  46.  
  47. string toString(const FlexArray& a)
  48. {
  49.     string result = "{";
  50.     if (a.size_ == 0) return "{}";
  51.     result += to_string(a.data_[0]);
  52.     for (size_t i = 1; i < a.size_; i++) result += ", " + to_string(a.data_[i]);
  53.     return result + "}";
  54. }
  55.  
  56.  
  57. //////////////////////// STUDENT TESTING //////////////////////////
  58. #include <iostream>
  59. #include <sstream>
  60. int run()
  61. {
  62.     cout << "Add your own tests here" << endl;
  63.     istringstream in("8 9 Q 4 5");
  64.     FlexArray a;
  65.     in >> a;
  66.     cout << "a->" << a << endl;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement