数据结构-任务05
3
杨伟彬
开始于 2019-10-30 13:57
0 9 399
已截止

任务尚未发布或者你没有权限查看任务内容。

任务讨论
米明豪

Status StrAssign(SString T,char * chars)

{

         int i;

         if(strlen(chars) > MAXSTRLEN)

    {

                   return ERROR;

    }

    else

    {

                   T[0] = strlen(chars);

                   for(i=1;i<=T[0];i++)

                   {

                            T[i] = *(chars+i-1);

                   }

                   return OK;

   }

 

 

StrInsert函数如下:

Status StrInsert(SString S, int pos, SString T)

{

         int i;

         if(pos<1 || pos>S[0]+1)

          {

                   return ERROR;

         }

 

         if(S[0] + T[0] <= MAXSTRLEN)

         {

                  

                   for(i=S[0];i>=pos;i--)

                   {

                            S[i+T[0]] = S[i];

                   }

                  

                   for(i=pos;i<pos+T[0];i++)

                   {

                            S[i] = T[i-pos+1];

                   }

                   S[0] = S[0]+T[0];

         }

         else

         {

                  

                   for(i=MAXSTRLEN;i<=pos;i--)

                   {

                            S[i] = S[i-T[0]];

                   }

                    

                   for(i=pos;i<pos+T[0];i++)

                   {

                            S[i] = T[i-pos+1];

                   }

                   S[0]=MAXSTRLEN;

                   return INFEASIBLE;

         }

}

 

PrintStr函数如下:

void PrintStr(SString S)

{

         int i;

         for(i=1;i<=S[0];i++)

         {

                   printf("%c",S[i]);

         }

         printf("n");

}

 

 

 

任务05_2 串的堆分配存储

StrCompare函数如下:

int StrCompare(HString S,HString T)

{

   int i;

   for(i=0;i<S.length && i<T.length;++i)

   {

                   if(S.ch[i] != T.ch[i])

                   {

                            return S.ch[i]-T.ch[i];

                   }

   }

   return S.length-T.length;

}

 

SubString函数如下:

Status SubString(HString *Sub,HString S,int pos,int len)

{

         int i;

         if(pos<1||pos>S.length||len<0||len>S.length-pos+1)

         {

                   return ERROR;

          }

    

         if(Sub->ch)

         {

                   free(Sub->ch);

         }

                  

         if(!len)

         {

                    Sub->ch = NULL;

                    Sub->length = 0;

          }

          else

          {

                    Sub->ch = (char*)malloc(len*sizeof(char));

                    if(!Sub->ch)

                    {

                             exit(OVERFLOW);

                    }

                    for(i=0;i<=len-1;i++)

                    {

                            Sub->ch[i] = S.ch[pos-1+i];

                    }

                    Sub->length = len;

          }

          return OK;

}

 

Strfun函数如下:

Status Strfun(HString *S,HString T)

{

         int i = 0;

         int j = 1;

 

         while(i < S->length && j < T.length)

    {

                  

                   if(S->ch[i] == T.ch[j])

                   {

                            ++i;

                            ++j;

                   }

                   else

                   {

                            i = i-j+2;

                   }

    }

 

         if(j >= T.length)

         {

                   int z;

                   if(S->length < i)

                   {

                            exit(OVERFLOW);

                   }

                   for(z=i-j;i<=S->length-j;i++)

                   {

                            S->ch[z] = S->ch[z+j];

                   }

                   S->length -= j;

                   S->ch = (char*)realloc(S->ch,S->length*sizeof(char));

                   return ERROR;         

         }

         else

         {

                   //printf;

                   return OK;                 

         }

}

 

任务05_3 模式匹配算法

1

S                 a       a       b       b       a       a       a       b       a       a       a       a       c       b

next 0       1       2       1       1       2       3       4       5       6       7       8       1       1

P                a       b       a       a       a      

next 0       1       1       2       1

 

 

2      

next[0] = 0;

next[1] = 1;

next[2] = 2;

杨伟彬

任务已更新

杨伟彬

任务已更新