String类的实现.docx
- 文档编号:9659286
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:47
- 大小:21.04KB
String类的实现.docx
《String类的实现.docx》由会员分享,可在线阅读,更多相关《String类的实现.docx(47页珍藏版)》请在冰豆网上搜索。
String类的实现
#include
#include
#include
#include
#include
usingnamespacestd;
classi_string
{
private:
char*str;
intlen;
public:
//构造函数
~i_string();
i_string();
i_string(i_string&);
i_string(char*);
i_string(char);
i_string(int);
i_string(char*,int);
i_string(char,int);
i_string(i_string&,int);
public:
//位数控制输出
voidSci_out(int);
voidSci_outln(int);
voidSci_out();
voidSci_outln();
public:
//功能函数
intlength(){returnlen;}
char*begin(){return&str[0];}
char*end(){return&str[len];}
char*Str(){returnstr;}
voidinsert(int,char*);
voidinsert(int,char);
voidinsert(int,i_string&);
voidreserve();
voidreserve(int,int);
voidswap(int,int);
voidremove(char);
voidremove(int,int,char);
voidremove(char*);
voidremove(int,int,char*);
voidremove(int);
voidremove(int,int);
voiderase(char*);
voiderase(int);
i_stringsubstring(int,int);
i_stringsubstring(int);
_int64valueof();
_int64valueof(int,int);
voidreset();
friendvoidswap(char*,char*);
friendvoidswap(char&,char&);
friendvoidReset(char*);
friendvoidReserve(char*);
friendvoidget_next(char*,int*);
friend_int64valueof(i_string&);
friend_int64valueof(char*);
friendintvalueof(char);
boolempty();
boolFigure();
voidclear();
public:
//值运算函数
i_stringadd(i_string&);
i_stringadd(char*);
i_stringadd(char);
i_stringadd(int);
i_stringadd(_int64);
i_stringmul(i_string&);
i_stringmul(char*);
i_stringmul(char);
i_stringmul(int);
i_stringmul(_int64);
i_stringsub(i_string&);
i_stringsub(char*);
i_stringsub(char);
i_stringsub(int);
i_stringsub(_int64);
public:
//运算符重载
i_string&operator=(i_string&);
i_string&operator=(char*);
i_string&operator=(char);
booloperator<(i_string&);
booloperator>(i_string&);
booloperator<=(i_string&);
booloperator>=(i_string&);
booloperator==(i_string&);
booloperator!
=(i_string&);
voidoperator+=(i_string&);
voidoperator+=(char*);
voidoperator+=(char);
i_stringoperator*(int);
i_stringoperator^(int);
i_stringoperator+(i_string&);
i_stringoperator+(char*);
i_stringoperator+(char);
i_stringoperator<<(int);
i_stringoperator>>(int);
public:
//特殊运算符
friendistream&operator>>(istream&is,i_string&ch)
{
is>>ch.str;
ch.len=strlen(ch.str);
returnis;
}
friendostream&operator<<(ostream&os,i_string&ch)
{
os< returnos; } char*operator+(inti) { return&str[i]; } char&operator[](inti)const { returnstr[i]; } }; /////////////////////////////////////////////////////// //////////构造函数//////////////// ////////////////////////////////////////////////////// i_string: : i_string() { str=newchar[1]; len=0; str[0]=NULL; } i_string: : ~i_string() { delete[]str; } i_string: : i_string(char*s) { len=strlen(s); str=newchar[len+1]; strcpy(str,s); str[len]=NULL; } i_string: : i_string(charc,intn) { len=n; str=newchar[len+1]; for(inti=0;i str[i]=c; str[len]=NULL; } i_string: : i_string(charc) { len=1; str=newchar[2]; str[0]=c; str[1]=NULL; } i_string: : i_string(intn) { len=n; str=newchar[len+1]; for(inti=0;i str[len]=NULL; } i_string: : i_string(i_string&i_str) { len=i_str.len; str=newchar[len+1]; strcpy(str,i_str.str); str[len]=NULL; } i_string: : i_string(char*s,intn) { intk=strlen(s); len=n*k; str=newchar[len+1]; for(inti=0;i for(intj=0;j str[len]=NULL; } i_string: : i_string(i_string&ch,intn) { len=n*ch.len; str=newchar[len+1]; for(inti=0;i for(intj=0;j str[len]=NULL; } ////////////////////////////////////////////////////////////////// ///////////////////输出函数////////////////////////////// ////////////////////////////////////////////////////////////// voidi_string: : Sci_out(intw) { i_stringi_str(*this); i_str.reset(); if(w<=0) { cout<<"Outputerror"< exit(0); } if(w>=len)cout< else { intop=0; if(i_str.str[0]=='-')op=1,w++; i_strings(i_str.substring(w)); if(i_str.str[w]<'5') { cout< if(s.str[0]=='-')cout< if(w>1+op)cout<<"."; cout< } else { intk=s.len; s=op>0? s.sub("1"): s.add("1"); if(k { i_str.len++; s.remove(s.len); } cout< if(s.str[0]=='-')cout< if(w>1+op)cout<<"."; cout< } } } voidi_string: : Sci_outln(intw) { this->Sci_out(w); cout< } voidi_string: : Sci_out() { this->Sci_out (1); } voidi_string: : Sci_outln() { this->Sci_out (1); cout< } ///////////////////////////////////////////////////////////// /////////////运算符重载////////////////////// ////////////////////////////////////////////////////////////// i_string&i_string: : operator=(i_string&ch) { if(this! =&ch) { str=newchar[ch.len+1]; strcpy(str,ch.str); len=ch.len; str[len]=NULL; } return*this; } i_string&i_string: : operator=(char*ch) { str=newchar[strlen(ch)+1]; strcpy(str,ch); len=strlen(ch); str[len]=NULL; return*this; } i_string&i_string: : operator=(charc) { str=newchar[2]; str[0]=c;len=1; str[len]=NULL; return*this; } voidi_string: : operator+=(i_string&ch) { intk1=len,k2=ch.len; str=(char*)realloc(str,(k2+k1+1)*sizeof(char)); for(inti=0;i len=k1+k2; str[len]=NULL; } voidi_string: : operator+=(char*ch) { intk1=len,k2=strlen(ch); str=(char*)realloc(str,(k2+k1+1)*sizeof(char)); for(inti=0;i len=k1+k2; str[len]=NULL; } i_stringi_string: : operator+(char*ch) { intk1=len,k2=strlen(ch),i; i_stringSTR(k1+k2); for(i=0;i for(i=k1;i returnSTR; } i_stringi_string: : operator+(i_string&ch) { return*this+ch.str; } voidi_string: : operator+=(charch) { str=(char*)realloc(str,(2+len)*sizeof(char)); str[len++]=ch; str[len]=NULL; } i_stringi_string: : operator*(intc) { i_stringSTR(*this); while(--c)STR+=*this; returnSTR; } i_stringi_string: : operator^(intc) { if(c<0) { cout<<"Runtimeerror"< exit(0); } if(c==0) { i_stringONE("1"); returnONE; } i_stringSUM("1"),A(*this); A.reset(); while(c>0) { if(c%2)SUM=SUM.mul(A); A=A.mul(A); c/=2; } returnSUM; } i_stringi_string: : operator+(charch) { char*c=newchar[2]; c[0]=ch;c[1]=NULL; return*this+c; } i_stringi_string: : operator<<(intn) { if(n<0) { cout<<"Inputerror"< exit(0); } i_stringSTR(n+len); for(inti=0;i returnSTR; } i_stringi_string: : operator>>(intn) { if(len { cout<<"Insufficientmemory"< exit(0); } i_stringSTR(len-n); for(inti=0;i returnSTR; } booli_string: : operator<(i_string&ch) { if(this->Figure()&&ch.Figure()) { i_stringi_str1(*this),i_str2(ch); i_str1=i_str1.sub(i_str2); if(i_str1[0]=='-')returntrue; returnfalse; } if(strcmp(str,ch.str)<0)returntrue; returnfalse; } booli_string: : operator>(i_string&ch) { if(this->Figure()&&ch.Figure()) { i_stringi_str1(*this),i_str2(ch); i_str1=i_str2.sub(i_str1); if(i_str1[0]=='-')returntrue; returnfalse; } if(strcmp(str,ch.str)>0)returntrue; returnfalse; } booli_string: : operator==(i_string&ch) { if(this->Figure()&&ch.Figure()) { i_stringi_str1(*this),i_str2(ch); i_str1=i_str1.sub(i_str2); if(i_str1[0]=='0')returntrue; returnfalse; } if(strcmp(str,ch.str)==0)returntrue; returnfalse; } booli_string: : operator! =(i_string&ch) { if(this->Figure()&&ch.Figure()) { i_stringi_str1(*this),i_str2(ch); i_str1=i_str1.sub(i_str2); if(i_str1[0]=='0')returnfalse; returntrue; } if(strcmp(str,ch.str)! =0)returntrue; returnfalse; } booli_string: : operator>=(i_string&ch) { if(this->Figure()&&ch.Figure()) { i_stringi_str1(*this),i_str2(ch); i_str1=i_str1.sub(i_str2); if(i_str1[0]>='0')returntrue; returnfalse; } if(strcmp(str,ch.str)>=0)returntrue; returnfalse; } booli_string: : operator<=(i_string&ch) { if(this->Figure()&&ch.Figure()) { i_stringi_str1(*this),i_str2(ch); i_str1=i_str1.sub(i_str2); if(i_str1[0]<='0')returntrue; returnfalse; } if(strcmp(str,ch.str)<=0)returntrue; returnfalse; } /////////////////////////////////////////////////////////////// ////////////////功能函数//////////////////////// ////////////////////////////////////////////////////////////// voidi_string: : insert(intpos,charc) { if(pos>len) { cout<<"Youcan'tinsertinttheoutside! "< exit(0); } str=(char*)realloc(str,(2+len)*sizeof(char)); for(inti=len;i>pos;i--)str[i]=str[i-1]; str[pos]=c;len++; str[len]=NULL; } voidi_string: : insert(intpos,char*ch) { if(pos>len) { cout<<"Youcan'tinsertoutside! "< exit(0); } intk=strlen(ch),i; str=(char*)realloc(str,(k+len+1)*sizeof(char)); len=len+k; for(i=len-1;i>pos+k-1;i--)str[i]=str[i-k]; for(;i>=pos;i--)str[i]=ch[--k]; str[len]=NULL; } voidi_string: : insert(intpos,i_string&ch) { if(pos>len) { cout<<"Youcan'tinsertoutside! "< exit(0); } this->insert(pos,ch.str); } voidi_string: : swap(intp1,intp2) { if(p1<1||p2<1||p1>len||p2>len) { cout<<"Swapshouldinside! "< exit(0); } chartem=str[p1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- String 实现