当前位置:编程学习 > C/C++ >>

C++ 字符串类的设计

内容及要求:

定义字符串类(String),包含数据成员,存放字符串的字符数组和字符串中字符的个数。

(1)字符串的输入与赋值;

(2)字符串的运算:连接、复制、查找、交换、比较;

(3)字符串的输出。

对字符串的输入和输出都用文件实现。

补充:最好能给解释下

 

追问:我需要答辩 所以有些地方看不明白 所以最好能解释下 如果可以 我会继续追加分数谢谢
答案:
如果不懂,加入群151988677,找千里,大家一起探讨
#include<iostream.h>
#include<string.h>
#include<malloc.h>
class string{
unsigned len;//串中字符个数
unsigned maxlen;//最大字符个数
char *str;
public:
string(const char *a);
string( string &a);
string(){maxlen=10;len=0;str=new char [maxlen+1];}
~string(){delete []str;}
string operator+(const string &b);
string operator+(const char *b);
friend string operator+(const char *a,const string &b);
string operator=(const string &b);
string operator-(const string &b);
string operator-(const char *b);
friend string operator-(char *b,const string &a);
friend string operator-(char a,const string &b);
char * findsub(const string &b);
char * findsub(const char *b);
int seeksub(const string &b);
int seeksub(const char *b);
int replacesub(const string &b,const string &c);
int replacesub(const char *b,const char *c);
int replacesub(const string &b,const char *c);
int replacesub(const char *b,const string &c);
int size(){return len;}
friend ostream & operator<<(ostream &out,const string &a)
{
out<<a.str;
return out;
}
friend istream & operator>>(istream &in,string &a)
{ a.len=-1;
do
{
a.len++;
a.str[a.len]=in.get();
if(a.len==a.maxlen)
{ char *p=new char [a.len];
strncpy(p,a.str,a.len+1);
delete []a.str;
a.maxlen=2*a.len;
a.str=new char[a.maxlen+1];
strncpy(a.str,p,a.len+1);
}
}while(a.str[a.len]!=' '&&a.str[a.len]!='\n');
a.str[a.len]='\0';
return in;
}
};
string::string(const char *a)
{ len=strlen(a);
maxlen=2*len;
str=new char[maxlen+1];
strcpy(str,a);
}
string::string(string &a)
{
len=a.len;
maxlen=a.maxlen;
str=new char[maxlen+1];
strcpy(str,a.str);
}
string string::operator+(const char *b)
{
char *p=new char[len+strlen(b)+1];
strcpy(p,str);
strcat(p,b);
return string(p);
}
string string::operator +(const string &b)
{
return *this+b.str;
}
string operator+(const char *a,const string &b)
{
string aa(a);
return aa+b.str;
}
string string::operator-(const char *b)
{
unsigned len1=strlen(b);
unsigned sublen;
char *p,*sub,*q;
if(len<len1) return *this;
p=findsub(b);//在对象的成员str中查找b的位置,如果没有返回NULL
if(p==NULL) return *this;//如果b不在对象的str中,返回对象本身
if(p==str)//如果p在串首,将p指向串中与b匹配的最后一个元素的后面一个元素
{
p=p+len1;

}
else p=str;//否则p指向str的串首
sublen=strlen(p);//算出p的长度
if(sublen<len1) //如果小于b的长度,则从p位置开始,将剩下的元素拷贝给sub,返回用sub初始化的对象;
{ sub=new char [sublen+1];
strncpy(sub,p,sublen);
sub[sublen]='\0';
return string(sub);
}
//如果对于等于b的长度,比较p串的后len1个元素和b是否相等
q=p+sublen-len1;//q指向p串的后len1个元素的开始
if(strcmp(q,b)!=0) q=q+len1;//如果不相等,将q指向p串的末尾
sublen=sublen-strlen(q);//算出p到q的元素个数
sub=new char [sublen+1];
strncpy(sub,p,sublen);//将p到q的元素拷贝给sub
sub[sublen]='\0';
return string(sub);
}
string string::operator -(const string &b)
{
return *this-b.str;

}
string operator-(char *a,const string &b)
{
string aa(a);
return aa-b.str;
}
string string::operator=(const string &b)
{ len=b.len;
if(maxlen<b.len)//如果要被赋值的串长度小于给他赋值的串的长度,则为他重新分配空间
{ delete []str;

maxlen=2*len;
str=new char[maxlen+1];
}
strcpy(str,b.str);
return *this;//返回等号左边的对象

}
char * string::findsub(const char *b)
{ return strstr(str,b);}
char * string::findsub(const string &b)
{
return strstr(str,b.str);
}
int string::replacesub(const char *b,const char *c)
{
char *find,*p,*q;int i,blen,clen;
blen=strlen(b);
clen=strlen(c);
find=str;
find=strstr(find,b);
if(find==NULL) return 0;
if(clen<=blen)
{

while(find!=NULL)
{
for(unsigned i=0;i<clen;i++)
find[i]=c[i];
find=find+i;
q=find;
if(clen!=blen)
for(p=find+blen-i;*p!='\0';p++,q++)
*q=*p;
find=strstr(find,b);
}
return 1;
}
q=str;i=0;
p=new char [len+len/blen*clen];

while(find!=NULL)
{
for(;q!=find;q++,i++)
p[i]=*q;
p[i]='\0';
strcat(p,c);
i=i+clen;
q=find=find+blen;
find=strstr(find,b);
}
strcat(p,q);
delete []str;
len=strlen(p);
str=new char[len+1];
strcpy(str,p);
return 1;
}
int string::replacesub(const string &b,const string &c)
{
return this->replacesub(b.str,c.str);
}
int string::replacesub(const string &b,const char *c)
{
return this->replacesub(b.str,c);
}
int string::replacesub(const char *b,const string &c)
{
return this->replacesub(b,c.str);
}
int string::seeksub(const char *b)
{
char *p;
p=this->findsub(b);
if(p)
return (p-this->str);
else return 0;
}
int string::seeksub(const string &b)
{
return this->seeksub(b.str);
}
void main()
{
string a("I like C++ !");
char bb[]="I think you too,are you?";
string b(bb);
string c(a);
string d("like");
char *p;
cout<<"a="<<a<<"长度为"<<a.size()<<endl;
cout<<"b="<<b<<"长度为"<<b.size()<<endl;
cout<<"a+b="<<a+b<<"长度为"<<(a+b).size()<<endl;
a=a+b;
cout<<"a="<<a<<"长度为"<<a.size()<<endl;
cout<<"c="<<c<<"长度为"<<c.size()<<endl;
cout<<"a-c="<<a-c<<"长度为"<<(a-c).size()<<endl;
cout<<"b="<<b<<"长度为"<<b.size()<<endl;
cout<<"c="<<c<<"长度为"<<c.size()<<endl;
cout<<"b="<<b<<"长度为"<<b.size()<<endl;
b=c+b+c;
cout<<"b="<<b<<"长度为"<<b.size()<<endl;cout<<"c="<<c<<"长度为"<<c.size()<<endl;
cout<<"b-c="<<(b=b-c)<<"长度为"<<(b-c).size()<<endl;
b=b+c;
cout<<"b="<<b<<"长度为"<<b.size()<<endl;
cout<<"c="<<c<<"长度为"<<c.size()<<endl;
cout<<"b-c="<<b-c<<"长度为"<<(b-c).size()<<endl;
int seek=0;
if((seek=a.seeksub(b))!=0) cout<<b<<"在"<<a<<"的第"<<seek<<"位"<<endl;//字串定位
else cout<<b<<"不是"<<a<<"的子串"<<endl;
if((seek=a.seeksub("like"))!=0) cout<<"like"<<"在"<<a<<"的第"<<seek<<"位"<<endl;//字串定位
else cout<<b<<"不是"<<a<<"的子串"<<endl;
b="like";
if((seek=a.seeksub(b))!=0) cout<<b<<"在"<<a<<"的第"<<seek<<"位"<<endl;//字串定位
else cout<<b<<"不是"<<a<<"的子串"<<endl;
string e("love");
if(a.replacesub(d,e)) cout<<"a="<<a<<endl;//字串替换
else cout<<d<&l

上一个:各种求c++代码中文对照表
下一个:C++程序的小问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,