![]() |
#2
Jonny02012018-01-11 00:26
#include <iostream>
#include <cstdlib> using namespace std; class HeapString { private: char *str; int length; public: HeapString(); HeapString(const HeapString &c) { } //传递对象的时候,系统会生成临时对象,这就表示某个对象的析构方法会被调用两次,如果不写这个拷贝构造方法,对象里面又有指针属性,会造成第二次调用的时候,删除了一个已经删除的地址,会报错 void StrAssign(const char str1[]); //串的赋值操作 bool StrEmpty(); //判断串是否为空 int StrLength(); //求串的长度操作 int StrInsert(int pos, HeapString T); //串的插入操作 int StrDelete(int pos, int len); //串的删除操作 int StrReplace(HeapString T, HeapString V); //串的替换操作 int StrIndex(int pos, HeapString T); //串的定位操作 void StrPrint(); //串的输出声明 ~HeapString(); }; //构造函数初始化 HeapString::HeapString() { length=0; str=(char *)'\0'; } //串的赋值操作 void HeapString::StrAssign(const char str1[]) { int i = 0,len; delete str; for(i=0;str1[i]!='\0';i++); /*这里空循环求str1字符串的长度*/ len=i; if(!i) { str=(char *)'\0'; length=0; } else { str=new char[len*sizeof(char)]; if(!str) exit(-1); for (i=0;i<len;i++) str[i]=str1[i]; /*将字符串cstr的内容赋值给串S*/ length=len; } } /*判断串是否为空*/ bool HeapString::StrEmpty() { if(length!=0) return true; else return false; } /*求串的长度*/ int HeapString::StrLength() { return length; } int HeapString::StrInsert(int pos, HeapString T) { int i; if (pos<0||pos-1>length) { cout<<"插入的位置不对"<<endl; return 0; } str=(char*)realloc(str,(length+T.length)*sizeof(char)); if(!str) { cout<<"分配内存失败"<<endl; exit(-1); } for (i=length-1;i>=pos-1;i--) /*将串S中第pos个位置的字符往后移动T.length个位置*/ str[i+T.length]=str[i]; for (i=0;i<T.length;i++) /*把T中的字符插入到str中*/ str[pos+i-1]=T.str[i]; length=length+T.length; return 1; } /*串的删除操作*/ int HeapString::StrDelete(int pos, int len) { int i; char *p; if(pos<0||len<0||pos+len-1>length) { cout<<"删除的位置不正确,参数len不合法"<<endl; return 0; } p = new char[length-len]; /*p指向动态分配的内存单元*/ if(!p) exit(-1); for (i=0;i<pos-1;i++) /*将串第pos位置之前的字符复制到p中*/ p[i]=str[i]; for (i=pos-1;i<=length-len;i++) p[i]=str[i+len]; /*将串第pos+len位置以后的字符复制到p*/ length-=len; delete str; /*释放原来的串S的内存空间*/ str=p; /*将串的str指向p字符*/ return 1; } /*串的替换操作*/ int HeapString::StrReplace(HeapString T,HeapString V) { int i = 0; int flag; if (T.StrEmpty()) return 0; do { i=StrIndex(i,T); //定位T在S中的位置 if(i) { StrDelete(i,T.StrLength()); //将S中这个位置的串删除 flag = StrInsert(i,V); //插入 if (!flag) return 0; i+=T.StrLength(); } } while(i); return 1; } /*串的定位操作*/ int HeapString::StrIndex(int pos,HeapString T) { int i, j; if(T.StrEmpty()) return 0; i=pos; j=0; while(i<length&&j<T.length) { if(str[i]==T.str[j]) { i++; j++; } else { pos++; i=pos; j=0; } } if(j>=T.length) return pos + 1; //对应 main 函数里的 if 条件判断,如果第一个就对上了,那么默认返回的 pos 是 0,而条件判断里是大于 0 else return -1; } /*串的输出*/ void HeapString::StrPrint() { int i; for(i=0;i<length;i++) { cout<<str[i]; } cout<<endl; } /*析构函数负责销毁*/ HeapString::~HeapString() { delete str; } int main() { HeapString S1; S1 = HeapString(); HeapString S2; S2 = HeapString(); char ch[50]; cout<<"求S2在S1中的位置"<<endl; cout<<"请输入S1和S2"<<endl; gets(ch) ; S1.StrAssign(ch); gets(ch); S2.StrAssign(ch); if(S1.StrIndex(0,S2)>0) cout<<"S2在S1中的位置为:"<<S1.StrIndex(0,S2)<<endl; else cout<<"在S1中不存在S2"; return 0; } |
#include <cstdlib>
#include <malloc.h>
using namespace std;
class HeapString
{
private:
char *str;
int length;
public:
HeapString();
void StrAssign(const char cstr[]); //串的赋值操作
bool StrEmpty(); //判断串是否为空
int StrLength(); //求串的长度操作
int StrInsert(int pos, HeapString T); //串的插入操作
int StrDelete(int pos, int len); //串的删除操作
int StrReplace(HeapString$ T, HeapString V); //串的替换操作
int StrIndex(int pos, HeapString T); //串的定位操作
void StrPrint(); //串的输出声明
~HeapString();
};
//构造函数初始化
HeapString::HeapString()
{
length=0;
str='\0';
}
//串的赋值操作
void HeapString::StrAssign(const char str1[])
{
int i = 0,len;
if(str)
delete str;
for(i=0;str1[i]!='\0';i++); /*这里空循环求str1字符串的长度*/
len=i;
if(!i)
{
str='\0';
length=0;
}
else
{
str=new char[len*sizeof(char)];
if(!str)
exit(-1);
for (i=0;i<len;i++)
str[i]=str1[i]; /*将字符串cstr的内容赋值给串S*/
length=len;
}
}
/*判断串是否为空*/
bool HeapString::StrEmpty()
{
if(length!=0)
return true;
else
return false;
}
/*求串的长度*/
int HeapString::StrLength()
{
return length;
}
int HeapString::StrInsert(int pos, HeapString T)
{
int i;
if (pos<0||pos-1>length)
{
cout<<"插入的位置不对"<<endl;
return 0;
}
str=(char*)realloc(str,(length+T.length)*sizeof(char));
if(!str)
{
cout<<"分配内存失败"<<endl;
exit(-1);
}
for (i=length-1;i>=pos-1;i--) /*将串S中第pos个位置的字符往后移动T.length个位置*/
str[i+T.length]=str[i];
for (i=0;i<T.length;i++) /*把T中的字符插入到str中*/
str[pos+i-1]=T.str[i];
length=length+T.length;
return 1;
}
/*串的删除操作*/
int HeapString::StrDelete(int pos, int len)
{
int i;
char *p;
if(pos<0||len<0||pos+len-1>length)
{
cout<<"删除的位置不正确,参数len不合法"<<endl;
return 0;
}
p = new char[length-len]; /*p指向动态分配的内存单元*/
if(!p)
exit(-1);
for (i=0;i<pos-1;i++) /*将串第pos位置之前的字符复制到p中*/
p[i]=str[i];
for (i=pos-1;i<=length-len;i++)
p[i]=str[i+len]; /*将串第pos+len位置以后的字符复制到p*/
length-=len;
delete str; /*释放原来的串S的内存空间*/
str=p; /*将串的str指向p字符*/
return 1;
}
/*串的替换操作*/
int HeapString::StrReplace(HeapString &T,HeapString V)
{
int i = 0;
int flag;
if (T.StrEmpty())
return 0;
do
{
i=StrIndex(i,T); //定位T在S中的位置
if(i)
{
StrDelete(i,T.StrLength()); //将S中这个位置的串删除
flag = StrInsert(i,V); //插入
if (!flag)
return 0;
i+=T.StrLength();
}
} while(i);
return 1;
}
/*串的定位操作*/
int HeapString::StrIndex(int pos,HeapString T)
{
int i, j;
if(T.StrEmpty())
return 0;
i=pos;
j=0;
while(i<length&&j<T.length)
{
if(str[i]==T.str[j])
{
i++;
j++;
}
else
{
pos++;
i=pos;
j=0;
}
}
if(j>=T.length)
return pos;
else
return -1;
}
/*串的输出*/
void HeapString::StrPrint()
{
int i;
for(i=0;i<length;i++)
{
cout<<str[i];
}
cout<<endl;
}
/*析构函数负责销毁*/
HeapString::~HeapString()
{
if(str)
delete str;
}
int main()
{
int main()
{
HeapString S1;
S1 = HeapString();
HeapString S2;
S2 = HeapString();
HeapString S3;
S3=HeapString();
char ch[50];
cout<<"请输入S1,S2,S3"<<endl;
cout<<"用S3把S1中内容为S2的替换掉"<<endl;
gets(ch);
S1.StrAssign(ch);
gets(ch);
S2.StrAssign(ch);
gets(ch);
S3.StrAssign(ch);
S1.StrReplace(S2,S3);
S1.StrPrint();
return 0;
}
想把S1中内容为S2的换为S3
[此贴子已经被作者于2018-1-11 01:00编辑过]