
串的基本操作实现源码|数据结构
发布日期:2021-05-07 06:44:03
浏览次数:17
分类:精选文章
本文共 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;}






发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年03月31日 22时11分05秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
js求阶乘
2019-03-05
小程序图片正确使用方式(防止发布之后不显示)
2019-03-05
Java学习
2019-03-05
Js函数
2019-03-05
L1-009 N个数求和 (20 分)
2019-03-05
L2-031 深入虎穴 (25 分)
2019-03-05
Unity之PlayerPrefs
2019-03-05
简单的xml读取存储方法(未优化)
2019-03-05
Nginx---惊群
2019-03-05
2种解法 - 获取一条直线上最多的点数
2019-03-05
项目中常用的审计类型概述
2019-03-05
nodeName与tagName的区别
2019-03-05
(九)实现页面底部购物车的样式
2019-03-05
python-day3 for语句完整使用
2019-03-05
linux下远程上传命令scp
2019-03-05
可重入和不可重入函数
2019-03-05
(2.1)关系模型之关系结构和约束
2019-03-05
androidstudio同步的时候下载jcenter的库出错解决办法
2019-03-05
ButterKnife使用问题
2019-03-05