本文共 3498 字,大约阅读时间需要 11 分钟。
typedef struct { char *ch; int length;}HStirng;
ADT{ StrAssign(&T,chars); //chars是字符串常量 //生成一个值等于chars的串T strCopy(&T,S) //串S存在 //由串S复制得串T StrEmpty(S) //串S存在 //若为空则返回true StrCompare(S,T) //S>T返回值大于0 //S
bool StrAssign(&T,chars){ //delete[] 释放new分配的对象数组指针指向的内存 if(T.ch) { delete[] T.ch; } char *ctemp = chars; T.length=0; //求T.length的长度 while(*ctemp) { ++T.length; ++ctemp; } T.ch = new char[T.length+1]; if(!T.ch) { printf("分配失败!"); system(pause); exit(-1); } else { char *temp = T.ch; while(*chars) { *temp++ = *chars++; } printf("分配成功!"); return true; }
bool StrCopy(&T,S){ if(!S.ch) { printf("串S不存在!"); system(pause); exit(0); } if(T.ch) { delete[] T.ch; } T.length = S.length; T.ch = new char[S.length+1]; if(!T.ch) { printf("分配失败!"); system(pause); exit(-1); } char *temps = S.ch; char *tempt = T.ch; while(*temps) { *tempt++ = *temps++; } *tempt = '\0'; printf("成功!"); return true;}
bool StrEmpty(S){ if(!S.ch) { printf("串不存在!\n"); system(pause); exit(0); } else { if(!S.length) { printf("串为空!\n"); return true; } else { printf("串不为空!\n"); return false; } } }
int StrLength(S){ if(!S.ch) { printf("串不存在!\n"); system(pause); exit(0); } else { printf("串的长度%d",S.length); return S.length; }
注意清空串跟一个串不存在的区别!
清空是ch="\0",length=0,但给他分配了内存空间 串不存在是没有分配内存空间bool Contact(&T,S1,S2){ if(!T.ch) { printf("串不存在!\n"); system(pause); exit(0); } T.length = S1.length+S2.length; T.ch = new char[T.length+1]; if(!T.ch) { printf("分配失败!\n"); system(pause); exit(-1); } char *temp = T.ch; while(*S1.ch) { *temp++ = *S1.ch++; } while(*S2.ch) { *temp++ = *S2.ch++; } *temp = '\0'; printf("连接成功!\n"); return true;}
bool SubString(&Sub,S,pos,len){ if(!S.ch) { printf("串不存在!\n"); system(pause); exit(0); } if(Sub.ch) { delete[] Sub.ch; } if(Pos<1 || Pos>S.length || len<0 || len>S.length-Pos+1 ) { printf("Pos&&len 错误!"); Sub.ch = new char[1]; *Sub.ch="\0"; Sub.length = 0; return false; } Sub.ch = new char[len+1]; char *temp = Sub.ch; for(int i=1;i != Pos;i++) { S.ch++; } for(i=0;i!=len;i++) { Sub.ch++ = S.ch++; } return true;}
bool Index(S,T,pos){ if(!S.ch || !T.ch) { printf("串不存在!\n"); system(pause); exit(0); } if(Pos <= 1 || Pos >= S.length) { printf("Pos错误!"); } else { char *tempt = T.ch; char *temps = S.ch; for(int i=0;iT.length) { printf("匹配成功"); return tempt.length-temps.length; } else return false;}
bool StringReplace(HStirng &Str,HStirng T,HStirng V){ if(!Str.ch || !T.ch || !V.ch || 0 == T.length) { printf("串不存在或为空!"); system(pause); exit(0); } int pos = Index(S,T,1); while(pos) { int nlength = Str.lenght + V.length - T.length; char *ctemp = new char[nlength+1]; if(!ctemp) { printf("函数执行失败!"); system(pause); exit(-1); } char *temp = ctemp; char *stemp = Str.ch; char *vtemp = V.ch; for(int i=0;i != pos;i++) { *temp++ = *stemp++; } for(i=0;i!=T.length;i++) { stemp++; } for(i=0;i!=V.length;i++) { *temp++ = *vtemp++; } while(*stemp) { *temp++ = *stemp++; } *temp='\0'; delete Str.ch; Str.length = nlength; Str.ch = temp; pos = Index(S,T,Pos+V.length); } printf("子串替代成功"); return true;}
转载地址:http://ulsh.baihongyu.com/