串的三种实现方式

#include<iostream>
using namespace std;
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSTRLEN 10
typedef struct{
unsigned char str[MAXSTRLEN+1];
int length;//用来存放串的实际大小
}SString;
int Concat(SString &T,SString S1,SString S2){
if(S1.length+S2.length<MAXSTRLEN)
{ int i=0;
        while(i<S1.length)
{T.str[i]=S1.str[i];i++;}
int j=0;
while(j<S2.length)
{ T.str[i]=S2.str[j];
i++;j++;
}
T.str[i]='\0';//注意设置\0
}
else if(S1.length<MAXSTRLEN&&S1.length+S2.length>MAXSTRLEN)
{int i=0;
while(i<S1.length)
{T.str[i]=S1.str[i];i++;}
int j=0;
while(j<MAXSTRLEN-S1.length)
{T.str[i]=S2.str[j];i++;j++;}
T.str[i]='\0';//注意设置\0
}
else {
int i=0;
        while(i<MAXSTRLEN)
{T.str[i]=S1.str[i];i++;}
T.str[i]='\0';//注意设置\0
}return OK;
}
int SubString(SString&L,SString S,int len,int pos)
{int i=pos;int j;
for(j=0;j<len;j++,i++ )
L.str[j]=S.str[i-1];
L.str[j]='\0';
L.length=len;
return OK;
}
int InitSString(SString &L){
cin>>L.str;
cout<<"str="<<L.str<<endl;
int i=0;
while(L.str[i]) i++; L.length=i;
cout<<L.length<<endl;
return 0;
}
//====================================以上是串的定长顺序存储表示===================


//串的堆分配存储表示
typedef struct {
int length;
char *ch;
}Hstring;
int strAssign(Hstring&T,char *chars){//开辟空间
int i=0;
while(chars[i]) i++;
T.ch=(char*)malloc(i*sizeof(char));
if(!T.ch)exit(OVERFLOW);
for(int j=0;j<i;j++)T.ch[j]=chars[j];T.length=i;
return 0;}
int Strlength(Hstring T){
return T.length;
}
int clearstring(Hstring&L){//清空
if(L.ch)free(L.ch);
L.ch=0;L.length=0;
return 0;}


int Concat(Hstring &A,Hstring B,Hstring C)//合并
{
A.ch=(char*)malloc((B.length+C.length)*sizeof(char));
if(!A.ch)exit(OVERFLOW);int i=0;for(i;i<B.length;i++)A.ch[i]=B.ch[i];for(int j=0;j<C.length;j++,i++)A.ch[i]=C.ch[j];

A.length=B.length+C.length;
return 0;
}
int Substring(Hstring&T,Hstring S,int pos,int len){//用串T返回串S位置pos长度为len
T.ch=(char*)malloc(len*sizeof(char));
T.length=len;
int j;
for(j=0;j<len;j++,pos++){
T.ch[j]=S.ch[pos-1];
}return 0;
}
int printf(Hstring L){//输出
for(int y=0;y<L.length;y++)
cout<<L.ch[y];
return 0;}


int strcompare(Hstring S,Hstring H )//若S>H,返回值>0;若S=H,返回值=0;若S<H,返回值<0
{for(int i=0;i<S.length&&i<H.length;i++)
if(S.ch[i]!=H.ch[i]){break;}
if(i>=S.length||i>=H.length) return S.length-H.length;
else return S.ch[i]-H.ch[i];
}
//==================以上是串的堆分配存储表示=================================


#define CHUNKSIZE 80
typedef struct Chunk{
char ch[CHUNKSIZE];
struct Chunk*next;
}Chunk;
typedef struct{
Chunk*head,*tail;
int curlen;
}Lstring;


//=================以上是串的块链存储表示=====================================


//以下主函数实现前两种表达式
int  main(){
SString b,S,M,N,L;
cout<<"设置数据"<<endl;
cout<<"L.str=";
InitSString(L);
cout<<"设置数据"<<endl;
cout<<"S.str=";
InitSString(S);
cout<<"设置数据"<<endl;
cout<<"M.str=";
InitSString(M);
cout<<"合并M和S:";
    Concat(b,M,S);cout<<b.str<<endl;
cout<<"从s的第二个数取四个单位长度数据:";
SubString(N,S,4,2);cout<<N.str<<endl;
Hstring A,B,C;char*ch="bfcdefghijkl";
strAssign(A,ch);cout<<"A:";printf(A);cout<<endl;
strAssign(B,ch);cout<<"B:";printf(B);cout<<endl;
Concat(C,A,B);cout<<"C:";printf(C);cout<<endl;
cout<<"比较C与A的大小:";cout<<strcompare(C,A)<<endl;
Substring(C,A,2,5);cout<<"从A的第二个数取五个单位长度数据:"<<"C:";printf(C);cout<<endl;
cout<<"比较C与A的大小:";cout<<strcompare(C,A)<<endl;
return 0;
}

你可能感兴趣的:(串的三种实现方式)