myString.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <string>
  3. //#include <stdio.h>
  4. #include "myString.h"
  5. // êîíñòðóêòîð
  6. MyString::MyString(const char* Str)
  7. {
  8. if (Str)
  9. {
  10. m_pCounter = Counter::CreateCounter(Str);
  11. m_pCounter->AddOwner();
  12. }
  13. else{ m_pCounter = nullptr; } //ñëåäèòü çà íóëåâûì óêàçàòåëåì
  14. }
  15. //êîíñòðóêòîð êîïèðîâàíèÿ
  16. MyString::MyString(const MyString& Other)
  17. {
  18. m_pCounter = Other.m_pCounter;
  19. if (m_pCounter)
  20. {
  21. m_pCounter->AddOwner();
  22. }
  23. }
  24. //ïåðåìåùàþùèé êîíñòðóêòîð êîïèðîâàíèÿ
  25. MyString::MyString(MyString&& Other)
  26. {
  27. m_pCounter = Other.m_pCounter; //ïðîñòî çàáèðàåì äàííûå
  28. Other.m_pCounter = nullptr;//ãîâîðèì, ÷òî ó äðóãîãî íåò
  29. }
  30. // äåñòðóêòîðà
  31. MyString::~MyString()
  32. {
  33. if (m_pCounter) { m_pCounter->RemoveOwner(); }
  34. }
  35. //ìåòîä , êîòîðûé îáåñïå÷èò äîñòóï ê õðàíÿùåéñÿ ñòðîêå
  36. const char* MyString::GetString() const
  37. {
  38. if (m_pCounter) { return m_pCounter->m_pStr; }
  39. }
  40. //ìåòîä, êîòîðûé áóäåò çàìåíÿòü ñòðîêó íà íîâóþ
  41. void MyString::SetNewString(const char* Str)
  42. {
  43. if (Str)//óêàçàòåëü íà ñòðîêó íå íóëåâîé
  44. {
  45. if (m_pCounter)
  46. {m_pCounter->RemoveOwner();}//÷èñòèì òåêóùåå çíà÷åíèå
  47. m_pCounter = Counter::CreateCounter(Str);//òåêóùåìó ïðèñâàèâàåì âîçâðàùàåìîå çíà÷åíèå
  48. m_pCounter->AddOwner(); //óâåëè÷èâàåì êîëè÷åñòâî
  49. }
  50. else { m_pCounter = nullptr; }
  51. }
  52. //îïåðàòîð ïðèñâàèâàíèÿ
  53. MyString& MyString::operator=(const MyString& Other)
  54. {
  55. if (&Other != this)
  56. {
  57. if (m_pCounter != Other.m_pCounter)
  58. {
  59. if (m_pCounter) { m_pCounter->RemoveOwner(); }
  60. m_pCounter = Other.m_pCounter;
  61. if (Other.m_pCounter) { m_pCounter->AddOwner(); }
  62. }
  63. }
  64. return *this;
  65. }
  66. //ïåðåìåùàþùèé îïåðàòîð ïðèñâàèâàíèÿ
  67. MyString& MyString::operator=(MyString&& Other)
  68. {
  69. if (&Other != this)
  70. {
  71. if (m_pCounter != Other.m_pCounter)
  72. {
  73. if (m_pCounter) { m_pCounter->RemoveOwner(); }
  74. m_pCounter = Other.m_pCounter;
  75. Other.m_pCounter = nullptr;
  76. }
  77. }
  78. return *this;
  79. }
  80. void MyString::PrintAllStrings()
  81. {
  82. Counter* pCounter = Counter::Head;
  83. for (size_t i = 0; i < Counter::m_curCounters; i++)
  84. {
  85. std::cout << pCounter->m_pStr << "\n";
  86. pCounter = pCounter->pNext;
  87. }
  88. }
  89. void MyString::ChangeRegister()
  90. {
  91. Counter* pCounter = Counter::Head;
  92. bool fl = false;
  93. for (size_t i = 0; i < Counter::m_curCounters; i++)
  94. {
  95. for (size_t i = 0; i < strlen(pCounter->m_pStr); i++)
  96. {
  97. if (pCounter->m_pStr[i]>=65 && pCounter->m_pStr[i] <= 90)
  98. {
  99. pCounter->m_pStr[i] += 32;
  100. fl = true;
  101. }
  102. if (pCounter->m_pStr[i] >= 97 && pCounter->m_pStr[i] <= 122 && !fl)
  103. {
  104. pCounter->m_pStr[i] -= 32;
  105. }
  106. fl = false;
  107. }
  108. pCounter = pCounter->pNext;
  109. }
  110. }
  111. // <0, åñëè str1 < str2
  112. // = 0, åñëè str1 = str2
  113. // >0, åñëè str1 > str2
  114. void MyString::PrintSortedStrings()
  115. //ïî âîçðàñòàíèþ
  116. {
  117. if (Counter::m_curCounters <= 1) { return; }
  118. Counter* start_Str = Counter::Head;//õðàíèò óêàçàòåëü íà ñòàðòîâûé Counter (âíåøíèé öèêë)
  119. Counter* current = start_Str;//õðàíèò óêàçâòåëü íà òåêóùèé Counter (âíóòðåííèé öèêë)
  120. Counter* min_Counter = current;//õðàíèò óêàçàòåëü íà ìèíèìàëüíóþ ñòðîêó
  121. for (size_t i = 0; i < Counter::m_curCounters; i++)
  122. {
  123. const char* min_Str = current->m_pStr;
  124. bool fl = false;
  125. for (size_t j = i+1; j < Counter::m_curCounters; j++)
  126. {
  127. current = current->pNext;
  128. if (strcmp(min_Str, current->m_pStr) > 0)
  129. {
  130. min_Str = current->m_pStr;
  131. min_Counter = current;
  132. fl = true;
  133. }
  134. }
  135. if (fl)
  136. {
  137. (*start_Str).Swap(min_Counter);
  138. current = min_Counter;
  139. current=start_Str = min_Counter->pNext;
  140. continue;
  141. }
  142. current = start_Str = start_Str->pNext;
  143. min_Counter = current;
  144. }
  145. PrintAllStrings();
  146. }
  147. std::ostream& operator<<(std::ostream& os, const MyString& str)
  148. {
  149. os << str.m_pCounter->m_pStr << "\n";
  150. return os;
  151. }