#include "stdafx.h"
#include<iostream>
using namespace std;
const int defaultSize=128;
class AString
{
public:
AString(){
ch=NULL;
curLength=0;
MaxSize=defaultSize;
}
AString(char * init="",int sz=defaultSize):ch(init),curLength(sz),MaxSize(defaultSize){cout<<"顺序串已被创建!"<<endl;}
AString(const AString & ob){
ch=ob.ch;
curLength=ob.curLength;
MaxSize=defaultSize;
}
~AString()
{
ch=NULL;
curLength=0;
MaxSize=0;
cout<<"顺序串已被析构!"<<endl;
}
int Length() const
{
return curLength;
}
void Clear()
{
ch=NULL;
curLength=0;
}
void operator=(AString & ob)
{
ch=ob.ch;
curLength=ob.curLength;
}
void operator=(char * str)
{
ch=str;//const的有关问题
curLength=strlen(str);
}
int operator==(AString &ob) const
{
if(strcmp(ch,ob.ch)==0)
return 1;
else
return 0;
}
int operator<(AString & ob) const
{
if(strcmp(ch,ob.ch)<0)
return 1;
else
return 0;
}
int operator>(AString & ob) const
{
if(strcmp(ch,ob.ch)>0)
return 1;
else
return 0;
}
int operator==(char * str) const
{
if(strcmp(ch,str)==0)
return 1;
else
return 0;
}
int operator<(char * str) const
{
if(strcmp(ch,str)<0)
return 1;
else
return 0;
}
int operator>(char * str) const
{
if(strcmp(ch,str)>0)
return 1;
else
return 0;
}
void operator+=(AString & ob)
{
for(int i=0;i<ob.curLength;i++)
{
ch[curLength++]=ob.ch[i];
}
}
void operator+=(char * str)
{
for(int i=0;i<strlen(str);i++)
ch[curLength++]=str[i];
}
char & operator[](int i)
{
return ch[i];
}
bool IsEmpty() const
{
if(curLength==0)
return 1;
else
return 0;
}
void SubString(AString & subs,int index,int length)
{
char * temp=new char[length+1];
for(int i=0,j=index;i<length;i++,j++)
temp[i]=ch[j];
temp[length]='\0';
subs=temp;
}
void Delete(int startIndex,int length)
{
for(int i=startIndex+length-1;i<curLength;i++)
{
ch[startIndex]=ch[i];
}
curLength=curLength-length;
}
void Insert(int pos, char * value)
{
int i;
for(i=curLength-1;i>pos;i--)
ch[i+1]=ch[i];
ch[i]=*value;
curLength++;
}
void Output()
{
for(int i=0;i<curLength;i++)
cout<<ch[i]<<",";
cout<<endl;
}
int Index(AString p)
{
int i;
for(i=0;i<=curLength-p.curLength;i++)
{
int j;
for(j=0;j<=p.curLength;j++)
{
if(ch[i+j]!=p.ch[j])
break;
}
if(j==p.curLength)
return i-1;
}
return -1;
}
private:
char * ch;
int curLength;
int MaxSize;
};
void main()
{
char c[10]="abcdefghi";
AString as(c,9);
as.Output();
cout<<"此顺序串的长度为:"<<as.Length()<<endl;
char d[10]="123456789";
AString as1(d,9);
cout<<"串二为:"<<endl;
as1.Output();
cout<<"串二与串一谁更大?"<<endl;
if(as>as1)
cout<<"串一更大"<<endl;
else
if(as==as1)
cout<<"两串大小相等"<<endl;
else
cout<<"串二更大"<<endl;
cout<<"d串与as谁大?"<<endl;
if(as>d)
cout<<"串一更大"<<endl;
else
if(as==d)
cout<<"串as与d大小相等"<<endl;
else
cout<<"d更大"<<endl;
cout<<"将串1与串二合并:"<<endl;
as+=as1;
as.Output();
cout<<"将串1与d合并:"<<endl;
as+=d;
as.Output();
cout<<"求合并串的1~4个元素所组成的字串:"<<endl;
char * z=NULL;
AString s(z,0);
AString & zz=s;
as.SubString(zz,1,4);
s.Output();
cout<<"删除as串中1~4号元素:"<<endl;
as.Delete(1,4);
as.Output();
char k='A';
cout<<"将k字符插入:"<<endl;
as.Insert(4,&k);
char f[5]="1234";
AString A(f,4);
cout<<"对字符串Y进行定位:"<<endl;
cout<<"s定位于:"<<as.Index(A)<<endl;
getchar();
}
#include<iostream>
using namespace std;
const int defaultSize=128;
class AString
{
public:
AString(){
ch=NULL;
curLength=0;
MaxSize=defaultSize;
}
AString(char * init="",int sz=defaultSize):ch(init),curLength(sz),MaxSize(defaultSize){cout<<"顺序串已被创建!"<<endl;}
AString(const AString & ob){
ch=ob.ch;
curLength=ob.curLength;
MaxSize=defaultSize;
}
~AString()
{
ch=NULL;
curLength=0;
MaxSize=0;
cout<<"顺序串已被析构!"<<endl;
}
int Length() const
{
return curLength;
}
void Clear()
{
ch=NULL;
curLength=0;
}
void operator=(AString & ob)
{
ch=ob.ch;
curLength=ob.curLength;
}
void operator=(char * str)
{
ch=str;//const的有关问题
curLength=strlen(str);
}
int operator==(AString &ob) const
{
if(strcmp(ch,ob.ch)==0)
return 1;
else
return 0;
}
int operator<(AString & ob) const
{
if(strcmp(ch,ob.ch)<0)
return 1;
else
return 0;
}
int operator>(AString & ob) const
{
if(strcmp(ch,ob.ch)>0)
return 1;
else
return 0;
}
int operator==(char * str) const
{
if(strcmp(ch,str)==0)
return 1;
else
return 0;
}
int operator<(char * str) const
{
if(strcmp(ch,str)<0)
return 1;
else
return 0;
}
int operator>(char * str) const
{
if(strcmp(ch,str)>0)
return 1;
else
return 0;
}
void operator+=(AString & ob)
{
for(int i=0;i<ob.curLength;i++)
{
ch[curLength++]=ob.ch[i];
}
}
void operator+=(char * str)
{
for(int i=0;i<strlen(str);i++)
ch[curLength++]=str[i];
}
char & operator[](int i)
{
return ch[i];
}
bool IsEmpty() const
{
if(curLength==0)
return 1;
else
return 0;
}
void SubString(AString & subs,int index,int length)
{
char * temp=new char[length+1];
for(int i=0,j=index;i<length;i++,j++)
temp[i]=ch[j];
temp[length]='\0';
subs=temp;
}
void Delete(int startIndex,int length)
{
for(int i=startIndex+length-1;i<curLength;i++)
{
ch[startIndex]=ch[i];
}
curLength=curLength-length;
}
void Insert(int pos, char * value)
{
int i;
for(i=curLength-1;i>pos;i--)
ch[i+1]=ch[i];
ch[i]=*value;
curLength++;
}
void Output()
{
for(int i=0;i<curLength;i++)
cout<<ch[i]<<",";
cout<<endl;
}
int Index(AString p)
{
int i;
for(i=0;i<=curLength-p.curLength;i++)
{
int j;
for(j=0;j<=p.curLength;j++)
{
if(ch[i+j]!=p.ch[j])
break;
}
if(j==p.curLength)
return i-1;
}
return -1;
}
private:
char * ch;
int curLength;
int MaxSize;
};
void main()
{
char c[10]="abcdefghi";
AString as(c,9);
as.Output();
cout<<"此顺序串的长度为:"<<as.Length()<<endl;
char d[10]="123456789";
AString as1(d,9);
cout<<"串二为:"<<endl;
as1.Output();
cout<<"串二与串一谁更大?"<<endl;
if(as>as1)
cout<<"串一更大"<<endl;
else
if(as==as1)
cout<<"两串大小相等"<<endl;
else
cout<<"串二更大"<<endl;
cout<<"d串与as谁大?"<<endl;
if(as>d)
cout<<"串一更大"<<endl;
else
if(as==d)
cout<<"串as与d大小相等"<<endl;
else
cout<<"d更大"<<endl;
cout<<"将串1与串二合并:"<<endl;
as+=as1;
as.Output();
cout<<"将串1与d合并:"<<endl;
as+=d;
as.Output();
cout<<"求合并串的1~4个元素所组成的字串:"<<endl;
char * z=NULL;
AString s(z,0);
AString & zz=s;
as.SubString(zz,1,4);
s.Output();
cout<<"删除as串中1~4号元素:"<<endl;
as.Delete(1,4);
as.Output();
char k='A';
cout<<"将k字符插入:"<<endl;
as.Insert(4,&k);
char f[5]="1234";
AString A(f,4);
cout<<"对字符串Y进行定位:"<<endl;
cout<<"s定位于:"<<as.Index(A)<<endl;
getchar();
}