#include "String.h"
AString::AString()
{
int sz = defaultSize;
maxSize = sz;
ch = new char[maxSize+1];
if(ch == NULL)
{cerr<<"Allocation Error!"<<endl;exit(1);}
curLength = 0;
ch[0] = '\0';
}
AString::AString(const AString &ob)
{
maxSize = ob.maxSize;
ch = new char[maxSize+1];
if(ch == NULL){cerr<<"Allocation Error!"<<endl;exit(1);}
curLength = ob.curLength;
strcpy(ch,ob.ch);
}
AString::AString(const char *init)
{
int len = strlen(init);
maxSize = (len > defaultSize)? len:defaultSize;
ch = new char[maxSize+1];
if(ch == NULL)
{cerr<<"Allocation Error!"<<endl;exit(1);}
curLength = len;
strcpy(ch,init);
}
AString& AString::operator += (const AString &ob)
{
char *temp = ch;
int n = curLength + ob.curLength;
int m = (maxSize >= n)? maxSize : n;
ch = new char[m];
if(ch == NULL) {cerr<<"Allocation Error!"<<endl;exit(1);}
maxSize = m;
curLength = n;
strcpy(ch,temp);
strcat(ch,ob.ch);
delete [] temp;
return *this;
}
char AString::operator [](int i)
{
if(i<0||i>=curLength)
{cerr<<"Allocation Error!"<<endl;exit(1);}
return ch[i];
}
AString& AString::operator ()(int pos, int len)
{
AString temp;
if(pos<0||pos+len-1>=maxSize||len<0)
{
temp.curLength = 0;
temp.ch[0] = '\0';
}
else
{
if(pos+len-1>=curLength)
len = curLength - pos;
temp.curLength = len;
for(int i= 0, j = pos; i<len; i++, j++)
temp.ch[i] = ch[j];
temp.ch[len] = '\0';
}
return temp;
}
int AString::Find(AString &pat, int k)const
{
int i,j;
for(i=k;i<=curLength-pat.curLength;i++)
{
for(j=0;j<pat.curLength;j++)
if(ch[i+j]!=pat.ch[j]) break;
if(j==pat.curLength) return i;
}
return -1;
}
void main()
{
AString s1("abcde");
AString s2("gabcdef");
cout<<"s1串为:"<<endl;
for(int i=0; i<s1.Length(); i++)
cout<<s1[i]<<endl;
cout<<"s2串为:"<<endl;
for(i=0; i<s2.Length(); i++)
cout<<s2[i]<<endl;
if(s1 == s2)
cout<<"s1与s2相等"<<endl;
else
cout<<"s1与s2不相等"<<endl;
cout<<"s1在s2串的位置为:"<<s2.Find(s1,0)<<endl;
cout<<"把s2串连在s1串后的新串为:"<<endl;
s1+=s2;
for(i=0; i<s1.Length(); i++)
cout<<s1[i]<<endl;
}
c++数据结构字符串程序
5星 · 超过95%的资源 需积分: 10 74 浏览量
2010-01-03
10:49:05
上传
评论
收藏 233KB RAR 举报
topfeng
- 粉丝: 0
- 资源: 2
最新资源
- 老飞飞搭建基础通用数据库V19数据库.rar
- jquery.js
- 机械设计多工位ACF贴胶带&预压设备sw18可编辑非常好的设计图纸100%好用.zip
- 基于Pytorch复现Point-Transformer,用于ShapeNet数据集点云分割
- 【医学影像分析】2D超声图像的分割检测(Ultrasound Nerve Segmentation - Kaggle数据集)
- 嘎嘎香的五款神仙谷歌插件
- .arch书源导入教程.mp4
- 贪心算法介绍及代码示例讲解
- CR13SP35MSI64 Crystal 水晶报表运行组件最后版本64位
- 图像分类数据集:玉米叶是否感染分类数据集(2分类,包含训练集、验证集)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈