Класс - "моя строка" st
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
class st
{ private:
int l; // длина
char * s; // адрес строки
public:
st(); // конструктор без параметра
st(char *); // конструктор с инициализирующим параметром
~st();
const char * put(const char *); // взять указатель на
строку
const char * get(); // записать строку
st(const st & x); // конструктор
копирования
st & operator=(const st & x); // оператор
присваивания
st & operator+=(const st & x); //унарный оператор +=
friend
st operator+(const st & x, const st & y);
// дружественная функция +
friend
st operator-(const st & x, const st & y);
// дружественная функция -
void ch(char); // для теста - изменить 0-й символ
void pr( char *); // для теста - распечатать....
};
void st::ch(char x)
{ if (s[0] != '\0') this->s[0]=x;
return;
};
void st::pr(char tit[]="")
{printf("%s[%d]<%s>\n",tit,l,s);return; };
st::st()
{ l=1; s=new char[1]; s[0]='\0';};
st::st(char * d)
{ l=strlen(d)+1; s=new char[l];
strcpy(s, d);
};
st::~st()
{
delete [] s;
l=0;
};
st & st::operator=(const st & x)
{
if( this == &x ) return *this;
delete [] this->s;
this->l=x.l;
this->s=new char[l];
strcpy(s, x.s);
return *this;
};
// КОСТРУКТОР КОПИРОВАНИЯ !!!!!!!!!!!!!!!!!!!!!!!!
// попробуйте убрать.....
st::st(const st & x)
{
this->l=x.l;
this->s=new char[l];
strcpy(s, x.s);
};
// унарный +=
st & st::operator+=(const st & x)
{
char *p; int lp=this->l+x.l-1;
p=new char[lp];
sprintf(p,"%s%s",this->s, x.s);
// в р - конактенитрованная строка
delete [] this->s;
this->l=lp;
this->s=new char[lp];
strcpy(this->s, p);
return *this;
};
// бинарный +
st operator+(const st & x, const st & y )
{
char *p;
st temp;//! вызывается конструктор копирования
int lp=x.l + y.l -1;
p=new char[lp];
sprintf(p,"%s%s", x.s, y.s);
// в р - конактенитрованная строка
temp.l=lp;
temp.s=new char[lp];
strcpy(temp.s, p);
return temp;
};
// бинарный -
st operator-(const st & x, const st & y )
{
printf("x=%s y=%s \n", x.s, y.s);
char *p;
st temp; // !!!!!!!!!!!!!!!!!!!!!
int i,j;
int k;
char tchar;
int flag;
int n=x.l+1;
p=new char[n];
k=0;
for(i=0; i< x.l; i++)
{ tchar=x.s[i];
// есть ли tchar в y.s ?
flag=0;
for(j=0;j<y.l;j++)
if(tchar==y.s[j]) {flag=1; break;};
if(flag==0) p[k++]=tchar;
};
p[k]='\0';
// в р - новая строка
temp.l=k;
temp.s=new char[k];
strcpy(temp.s, p);
return temp;
};
const char * st::put(const char * d)
{
delete [] s;
l=strlen(d)+1; s=new char[l];
strcpy(s, d);
return s;
};
const char * st::get()
{
return s;
}
int main(int argc, char *argv[] )
{ st a1, a2, a3, a4="куку";
const char * p;
p=a1.put("привет");
printf("***p===<%s>\n",p);
a1.pr("a1= .... ==> ");
a2=a1;
a2.pr("a2=a1 => ");
a3.put("Всем "); a3.pr("a3.put(...) ==> ");
a3+=a2; a3.pr("a3+=a2 ==> ");
printf("*************************\n");
a1.ch('$'); a1.pr("a1($) ==> ");
a2=a1;
a2.ch('@'); a2.pr("a1(@) ==> ");
a4.pr("a4 ==> ");
printf("*************************\n");
a2=a1 + " +++ " + a4; a2.pr("a1 +++ a4 ==> ");
a1 = "победа!"; a1.pr("a1=.... ==> ");
a2 = a1 - "по"; a2.pr("a2=a1 - по ==> ");
a3 = a1 - "ап"; a3.pr("a3=a1 - ап ==> ");
return 0;
}
===========================================================
C:\Test\cpp\b_str>t
***p===<привет>
a1= .... ==> [7]<привет>
a2=a1 => [7]<привет>
a3.put(...) ==> [6]<Всем >
a3+=a2 ==> [12]<Всем привет>
*************************
a1($) ==> [7]<$ривет>
a1(@) ==> [7]<@ривет>
a4 ==> [5]<куку>
*************************
a1 +++ a4 ==> [16]<$ривет +++ куку>
a1=.... ==> [8]<победа!>
x=победа! y=по
a2=a1 - по ==> [5]<беда!>
x=победа! y=ап
a3=a1 - ап ==> [5]<обед!>
C:\Test\cpp\b_str>