Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class MyString {
  7. public:
  8.     MyString(const char * s);
  9.     MyString();
  10.     ~MyString();
  11.     char* getStr() const;
  12.     friend ostream & operator<<(ostream& os, const MyString &st);
  13.     MyString & operator=(const MyString& originalString);
  14. private:
  15.     char *str;
  16.     int len;
  17. };
  18.  
  19. MyString::MyString(const char *s) {
  20.     len = strlen(s);
  21.     str = new char [len + 1];
  22.     strcpy(str, s);
  23. }
  24.  
  25. MyString::MyString() {
  26.     len = 4;
  27.     str = new char [4];
  28.     strcpy(str, "");
  29. }
  30.  
  31. MyString::~MyString() {
  32.     cout << "Nicim MyString" << endl;
  33.     delete [] str;
  34. }
  35.  
  36. char* MyString::getStr() const {
  37.     return str;
  38. }
  39.  
  40. ostream& operator<<(ostream& os, const MyString &st) {
  41.     os << st.str;
  42.     return os;
  43. }
  44.  
  45. class MyVector {
  46. public:
  47.     MyVector(const MyString& string);
  48.     MyVector();
  49.     ~MyVector();
  50.     void pushBack(const MyString&);
  51.     int getLen() const;
  52.     void insert(int position, const MyString& newString);
  53.     friend ostream & operator<<(ostream& os, const MyVector &vct);
  54.     MyString operator[](int);
  55.     int binarySearch(const MyString&) const;
  56. private:
  57.     int m_len;
  58.     int m_maxSize;
  59.     MyString *m_array;
  60.     void allocMem();
  61. };
  62.  
  63. MyVector::MyVector(){
  64.     m_maxSize = 2;
  65.     m_array = new MyString[m_maxSize];
  66.     m_len = 0;
  67. }
  68.  
  69. MyVector::~MyVector() {
  70.     cout << "Nicim MyVector" << endl;
  71.     delete [] m_array;
  72. }
  73.  
  74. void MyVector::pushBack(const MyString &tmpString) {
  75.     if (m_len +1 > m_maxSize) {
  76.         allocMem();
  77.     }
  78.     m_array[m_len] = tmpString;
  79.     m_len++;
  80. }
  81.  
  82. int MyVector::getLen() const {
  83.     return m_len;
  84. }
  85.  
  86. MyString MyVector::operator[](int i) {
  87.     return m_array[i];
  88. }
  89.  
  90. MyString& MyString::operator=(const MyString& originalString) {
  91.     if (&originalString == this) return *this;
  92.     delete [] str;
  93.     len = originalString.len;
  94.     str = new char [len+1];
  95.     memcpy(str, originalString.str, len+1);
  96.    
  97.     return *this;
  98. }
  99.  
  100. void MyVector::insert(int position, const MyString& newString) {
  101.     if (position == -1) return;
  102.     MyString* tmp = new MyString[m_len+1];
  103.     for (int i = 0; i < m_len+1; i++) {
  104.         if (i < position) tmp[i] = m_array[i];
  105.         else if (i == position) {
  106.             tmp[i] = newString;
  107.         } else if (i > position) {
  108.             tmp[i] = m_array[i-1];
  109.         }
  110.     }
  111.     m_len++;
  112.     delete [] m_array;
  113.     // if (m_len > m_maxSize) {
  114.     //     allocMem();
  115.     // }
  116.     m_array = new MyString[m_len+1];
  117.     for (int i = 0; i < m_len; i++) {
  118.         m_array[i] = tmp[i];
  119.     }
  120.     delete [] tmp;
  121.    
  122. }
  123.  
  124. void MyVector::allocMem(){
  125.     m_maxSize = m_len*2;
  126.     MyString* tmp = new MyString[m_len + 1];
  127.     for (int i = 0; i < m_len; i++) {
  128.         tmp[i] = m_array[i];
  129.     }
  130.     delete [] m_array;
  131.     m_array = new MyString[m_maxSize];
  132.     for(int i = 0; i < m_len; i++)
  133.         m_array[i] = tmp[i];
  134.     delete [] tmp;
  135. }
  136.  
  137. int MyVector::binarySearch(const MyString& newString) const {
  138.     int begin = 0, end = m_len, middle = 0;
  139.     bool found = false;
  140.     while (begin <= end) {
  141.         middle = begin + (end - begin) / 2;
  142.         cout << m_array[middle] << endl;
  143.         if (strcmp(m_array[middle].getStr(), "") == 0) {
  144.             return begin;
  145.         }
  146.         int comp = strcmp(m_array[middle].getStr(), newString.getStr());
  147.         if(comp == 0){
  148.             found = true;
  149.             break;
  150.         }
  151.         else if(comp < 0)
  152.             begin = middle + 1;
  153.         else
  154.             end = middle - 1;
  155.     }
  156.    
  157.     if(found){
  158.         while(middle >= 0 && strcmp(newString.getStr(), m_array[middle].getStr()))
  159.             middle--;
  160.         return middle;
  161.     }
  162.    
  163.     return -1;
  164. }
  165.  
  166. ostream& operator<<(ostream& os, const MyVector &vct) {
  167.     for (int i = 0; i < vct.m_len; i++) {
  168.         os << vct.m_array[i] << " ";
  169.     }
  170.     os << endl;
  171.    
  172.     return os;
  173. }
  174.  
  175. int main(int argc, char const *argv[])
  176. {
  177.     MyString mujString("aa");
  178.     MyString mujString2("bb");
  179.     MyString mujString3("cc");
  180.     MyString mujString4("a b");
  181.     MyString mujString5("bvs");
  182.     MyString mujString6("f as");
  183.    
  184.    
  185.     MyVector mujVector;
  186.     mujVector.insert(mujVector.binarySearch(mujString), mujString);
  187.     cout<<mujVector<<endl;
  188.     mujVector.insert(mujVector.binarySearch(mujString2), mujString2);
  189.     cout<<mujVector<<endl;
  190.     mujVector.insert(mujVector.binarySearch(mujString3), mujString3);
  191.     cout<<mujVector<<endl;
  192.     mujVector.insert(mujVector.binarySearch(mujString4), mujString4);
  193.     cout<<mujVector<<endl;
  194.     mujVector.insert(mujVector.binarySearch(mujString5), mujString5);
  195.     cout<<mujVector<<endl;
  196.     mujVector.insert(mujVector.binarySearch(mujString6), mujString6);
  197.     cout<<mujVector<<endl;
  198.     cout<<mujVector<<endl;
  199.    
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement