Добавил:
yermolenkoigor9
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:First_magistr / First_course_magistr / Copynstructor_and_Polimorphism / constructors
.cpp#include "precompiler.h"
#include "constructors.h"
Tstring::Tstring() // конструктор по умолчанию
{
this->size = 17*sizeof(char);
this->s = new char[size];
strcpy(this->s, "computer_physics\0");
}
Tstring::Tstring(char *s) // конструктор с параметром
{
this->size = strlen(s)+1;
this->s = new char[size];
strcpy(this->s,s);
}
Tstring::Tstring(const Tstring & obj)
{
this->size = obj.size;
this->s = new char[size];
strcpy(this->s, obj.s);
}
ostream& operator << (ostream& t, Tstring d)
{
return t << d.s;
}
istream& operator >> (istream& t, Tstring& d)
{
t >> d.s;
return t;
}
size_t length(Tstring d)
{
return d.size;
}
Tstring Tstring :: operator + (const Tstring& obj)
{
char *tmp = new char[this->size+obj.size];//новую, чтоб в старую не накопировать
strcpy(tmp,this->s);
return Tstring(strcat(tmp,obj.s));//Объединение строк
}
Tstring& Tstring :: operator += (const Tstring& obj)
{
this->size += obj.size;
strcpy(this->s, strcat(this->s,obj.s));
return *this;
}
Tstring& Tstring :: operator = (const Tstring& obj)
{
this->size = strlen(obj.s)+1;
strcpy(this->s, obj.s);//Скопировать строку
return *this;
}
Tstring Tstring :: operator [] (size_t i)
{
char ss[2];
while(i >= this->size)
cin >> i;
return Tstring(strncpy(ss,this->s+i,1));//Скопировать n символов строки
}
Tstring Tstring :: operator != (const Tstring& obj)//Слияние строк с удалением повторяющихся элементов
{
size_t ix, ix1 = 0, ix2 = 0;
char *ss = new char[this->size + obj.size];
ix = 0;
while(ix1 < this->size)
{
ix = strcspn(this->s + ix1, obj.s);//Выполняет поиск первого вхождения в строку 1 любого из символов строки 2,
strncpy(ss+ix2, this->s+ix1, ix);//и возвращает количество символов до найденного первого вхождения.
//cout << ix << " " << ss << endl;
ix1 += ix+1;
ix2 += ix;
}
ix1 = 0;
while(ix1 < obj.size)//заново, но строки поменялись ролями
{
ix = strcspn(obj.s + ix1, this->s);
strncpy(ss+ix2, obj.s+ix1, ix);
//cout << ix << " " << ss << endl;
ix1 += ix+1;
ix2 += ix;
}
strcpy(ss+ix2, "\0");//заканчивает строку
return Tstring(ss);//упаковывает и отправляет
//delete []ss;
}
bool Tstring :: operator > (const Tstring& obj)
{
return (this->size > obj.size);
}
bool Tstring :: operator < (const Tstring& obj)
{
return (this->size < obj.size);
}
bool Tstring :: operator >= (const Tstring& obj)
{
return this->size >= obj.size;
}
bool Tstring :: operator <= (const Tstring& obj)
{
return this->size <= obj.size;
}
bool Tstring :: operator == (const Tstring& obj)
{
return this->size == obj.size;
}
int menu(int max_menu) {
int i_menu;
cout << "\n\nInput number of the action 1-" << max_menu << endl;
do
{
cout << "1 - Exit" << endl;
cout << "2 - Set S1" << endl;
cout << "3 - S1 + S2" << endl;
cout << "4 - S1 += S2" << endl;
cout << "5 - S1 = S2" << endl;
cout << "6 - S1[i]" << endl;
cout << "7 - S1 != S2" << endl;
cout << "8 - length(S1)" << endl;
cout << "9 - Check conditions: S1<S2; S1>S2; S1<=S2; S1>=S2; S1==S2" << endl;
cout << "10- S1 = " << endl;
cout << "11- S2 = " << endl;
cout << endl;
cin >> i_menu;
}
while( (i_menu < 0) || (i_menu > max_menu));
return i_menu;
}
Соседние файлы в папке Copynstructor_and_Polimorphism