123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <string>
- //#include <stdio.h>
- #include "myString.h"
- // êîíñòðóêòîð
- MyString::MyString(const char* Str)
- {
- if (Str)
- {
- m_pCounter = Counter::CreateCounter(Str);
- m_pCounter->AddOwner();
- }
- else{ m_pCounter = nullptr; } //ñëåäèòü çà íóëåâûì óêàçàòåëåì
- }
- //êîíñòðóêòîð êîïèðîâàíèÿ
- MyString::MyString(const MyString& Other)
- {
- m_pCounter = Other.m_pCounter;
-
- if (m_pCounter)
- {
- m_pCounter->AddOwner();
- }
- }
- //ïåðåìåùàþùèé êîíñòðóêòîð êîïèðîâàíèÿ
- MyString::MyString(MyString&& Other)
- {
- m_pCounter = Other.m_pCounter; //ïðîñòî çàáèðàåì äàííûå
- Other.m_pCounter = nullptr;//ãîâîðèì, ÷òî ó äðóãîãî íåò
- }
- // äåñòðóêòîðà
- MyString::~MyString()
- {
- if (m_pCounter) { m_pCounter->RemoveOwner(); }
- }
- //ìåòîä , êîòîðûé îáåñïå÷èò äîñòóï ê õðàíÿùåéñÿ ñòðîêå
- const char* MyString::GetString() const
- {
- if (m_pCounter) { return m_pCounter->m_pStr; }
- }
- //ìåòîä, êîòîðûé áóäåò çàìåíÿòü ñòðîêó íà íîâóþ
- void MyString::SetNewString(const char* Str)
- {
- if (Str)//óêàçàòåëü íà ñòðîêó íå íóëåâîé
- {
- if (m_pCounter)
- {m_pCounter->RemoveOwner();}//÷èñòèì òåêóùåå çíà÷åíèå
-
- m_pCounter = Counter::CreateCounter(Str);//òåêóùåìó ïðèñâàèâàåì âîçâðàùàåìîå çíà÷åíèå
- m_pCounter->AddOwner(); //óâåëè÷èâàåì êîëè÷åñòâî
- }
- else { m_pCounter = nullptr; }
- }
- //îïåðàòîð ïðèñâàèâàíèÿ
- MyString& MyString::operator=(const MyString& Other)
- {
- if (&Other != this)
- {
- if (m_pCounter != Other.m_pCounter)
- {
- if (m_pCounter) { m_pCounter->RemoveOwner(); }
-
- m_pCounter = Other.m_pCounter;
-
- if (Other.m_pCounter) { m_pCounter->AddOwner(); }
- }
- }
-
- return *this;
- }
- //ïåðåìåùàþùèé îïåðàòîð ïðèñâàèâàíèÿ
- MyString& MyString::operator=(MyString&& Other)
- {
- if (&Other != this)
- {
- if (m_pCounter != Other.m_pCounter)
- {
- if (m_pCounter) { m_pCounter->RemoveOwner(); }
- m_pCounter = Other.m_pCounter;
- Other.m_pCounter = nullptr;
- }
- }
- return *this;
- }
- void MyString::PrintAllStrings()
- {
- Counter* pCounter = Counter::Head;
- for (size_t i = 0; i < Counter::m_curCounters; i++)
- {
- std::cout << pCounter->m_pStr << "\n";
- pCounter = pCounter->pNext;
- }
- }
- void MyString::ChangeRegister()
- {
- Counter* pCounter = Counter::Head;
- bool fl = false;
-
- for (size_t i = 0; i < Counter::m_curCounters; i++)
- {
- for (size_t i = 0; i < strlen(pCounter->m_pStr); i++)
- {
- if (pCounter->m_pStr[i]>=65 && pCounter->m_pStr[i] <= 90)
- {
- pCounter->m_pStr[i] += 32;
- fl = true;
- }
- if (pCounter->m_pStr[i] >= 97 && pCounter->m_pStr[i] <= 122 && !fl)
- {
- pCounter->m_pStr[i] -= 32;
- }
- fl = false;
- }
- pCounter = pCounter->pNext;
- }
- }
- // <0, åñëè str1 < str2
- // = 0, åñëè str1 = str2
- // >0, åñëè str1 > str2
- void MyString::PrintSortedStrings()
- //ïî âîçðàñòàíèþ
- {
- if (Counter::m_curCounters <= 1) { return; }
- Counter* start_Str = Counter::Head;//õðàíèò óêàçàòåëü íà ñòàðòîâûé Counter (âíåøíèé öèêë)
- Counter* current = start_Str;//õðàíèò óêàçâòåëü íà òåêóùèé Counter (âíóòðåííèé öèêë)
- Counter* min_Counter = current;//õðàíèò óêàçàòåëü íà ìèíèìàëüíóþ ñòðîêó
- for (size_t i = 0; i < Counter::m_curCounters; i++)
- {
- const char* min_Str = current->m_pStr;
- bool fl = false;
- for (size_t j = i+1; j < Counter::m_curCounters; j++)
- {
- current = current->pNext;
- if (strcmp(min_Str, current->m_pStr) > 0)
- {
- min_Str = current->m_pStr;
- min_Counter = current;
- fl = true;
- }
- }
- if (fl)
- {
- (*start_Str).Swap(min_Counter);
- current = min_Counter;
- current=start_Str = min_Counter->pNext;
- continue;
- }
- current = start_Str = start_Str->pNext;
- min_Counter = current;
- }
- PrintAllStrings();
- }
- std::ostream& operator<<(std::ostream& os, const MyString& str)
- {
- os << str.m_pCounter->m_pStr << "\n";
- return os;
- }
|