Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы / MyStr / mystr
.cpp#include "mystr.h"
MyStr::MyStr()
{
Len = 0;
Data = NULL;
}
MyStr::~MyStr()
{
if (Data != NULL)
delete [] Data;
}
MyStr::MyStr(const char *st)
{
Len = strlen(st);
Data = new char[Len + 1];
strcpy(Data, st);
}
MyStr &MyStr::operator=(const MyStr &ms)
{
// Если происходит присвоение объекта самому себе, то ничего делать
// не надо
if (&ms == this)
return *this;
if (Data != NULL)
delete [] Data;
Len = ms.Len;
Data = new char[Len + 1];
strcpy(Data, ms.Data);
return *this;
}
const char *MyStr::GetChar() const
{
return (const char*)Data;
}
int MyStr::GetLength() const
{
return Len;
}
MyStr &MyStr::operator=(const char *st)
{
if (Data != NULL)
delete [] Data;
Len = strlen(st);
Data = new char[Len + 1];
strcpy(Data, st);
return *this;
}
int MyStr::FindSubstr(const MyStr &ms)
{
int index = -1;
char *adr = strstr(Data, ms.Data);
if (adr != NULL)
{
index = reinterpret_cast <int> (adr) - reinterpret_cast <int> (Data);
}
else
{
index = -1;
}
return index;
}
MyStr::MyStr(const MyStr &mystr)
{
Len = mystr.Len;
Data = new char[Len + 1];
strcpy(Data, mystr.Data);
}
const MyStr operator+(const MyStr &mystr1, const MyStr &mystr2)
{
char *sum;
sum = new char[mystr1.Len + mystr2.Len + 1];
// sum = new char[strlen(mystr1.Data) + strlen(mystr2.Data) + 1];
strcpy(sum, mystr1.Data);
strcat(sum, mystr2.Data);
MyStr a(sum);
return a;
}
const MyStr &MyStr::operator+=(const MyStr &mystr)
{
char *sum;
Len += mystr.Len;
sum = new char[Len + 1];
strcpy(sum, Data);
strcat(sum, mystr.Data);
if (Data != NULL)
delete [] Data;
Data = new char[Len + 1];
strcpy(Data, sum);
delete [] sum;
return *this;
}
bool MyStr::operator==(const MyStr &mystr)
{
if (Len == mystr.Len)
if (strcmp(Data, mystr.Data) == 0)
{
return true;
}
else
{
return false;
}
return false;
}
bool MyStr::operator!=(const MyStr &mystr)
{
if (Len == mystr.Len)
if (strcmp(Data, mystr.Data) == 0)
return false;
return true;
}
ostream &operator<<(ostream &os, const MyStr &mystr)
{
os << mystr.Data;
return os;
}
istream &operator>>(istream &is, MyStr &mystr)
{
char buffer[256];
// Не очень хорошо, т. к. возможно переполнение буфера
is >> buffer;
if (mystr.Data != NULL)
delete [] mystr.Data;
mystr.Len = strlen(buffer);
mystr.Data = new char[mystr.Len + 1];
strcpy(mystr.Data, buffer);
return is;
}
int MyStr::operator[](int index)
{
if (index < Len && index >= 0)
{
return static_cast <unsigned char> (Data[index]);
}
return -1;
}
MyStr MyStr::InsertStr(int start, const MyStr &mystr)
{
if (start < 0)
{
wprintf(L"Ошибка: индекс символа не может быть отрицательным\n");
exit(1);
}
if (start > Len)
{
wprintf(L"Ошибка: индекс символа превышает длину строки\n");
exit(1);
}
char *part1, *part2;
part1 = new char[start + 1];
part2 = new char[Len - start + 1];
strncpy(part1, Data, start);
part1[start] = 0x00;
strcpy(part2, Data + start);
char *newstr;
newstr = new char[Len + mystr.Len + 1];
strcpy(newstr, part1);
strcat(newstr, mystr.Data);
strcat(newstr, part2);
Len = Len + mystr.Len;
if (Data != NULL)
delete [] Data;
Data = new char[Len + 1];
strcpy(Data, newstr);
delete [] part1;
delete [] part2;
delete [] newstr;
return *this;
}