www.pudn.com > datastructor.rar > HString.c


#include  
#include  
typedef enum {ERROR = -2,OVERLOW,FLASE, TRUE, OK} Status; 
typedef struct{ 
        char* ch; 
        int length; 
}HString; 
 
Status StrAssign(HString* T, const char* chars){ 
    //生成一个其值等于串chars的串T  
        int i,j; 
         
        if(T->ch) free(T->ch);    //释放T原有的空间  
         
        i = strlen(chars);          
        if(!i) {T->ch = NULL; T->length = 0;} 
        else{ 
                T->ch=(char*)malloc(i* sizeof(char)); 
                if(!(T->ch)) 
                        exit(OVERLOW); 
                for(j = 0; jch[j] = chars[j]; 
                T->length = i; 
        } 
        return OK; 
} 
 
int StrLength(const HString *S){ 
    //返回S的元素个数 ,称为串的长度  
        return S->length; 
} 
 
int StrCompare(const HString *S, const HString *T){ 
    //若S>T,则返回值>0:若S=T,则返回值=0 :若Slength && i < T->length; ++i) 
                if(S->ch[i] != T->ch[i]) return S->ch[i] - T->ch[i]; 
        return S->length - T->length; 
} 
 
Status ClearString(HString *S){ 
    //将S清为空串  
        if(S->ch) {free(S->ch); S->ch = NULL;} 
        S->length = 0; 
        return OK; 
} 
 
Status Concat(HString *T, const HString *S1, const HString *S2){ 
    //用T返回由S1和S2联接而成的新串  
        int i; 
        if(T->ch) free(T->ch); 
        T->ch=(char*)malloc((S1->length + S2->length)*sizeof(char)); 
        if(!(T->ch)) 
                exit(OVERLOW); 
        for(i= 0; i < S1->length; i++) 
                T->ch[i] = S1->ch[i]; 
        T->length = S1->length + S2->length; 
        for(i = S1->length; i < T->length ; i++) 
                T->ch[i] = S2->ch[i - S1->length]; 
        return OK; 
} 
 
Status SubString(HString *Sub, const HString *S, int pos, int len){ 
    //用Sub返回串S的第pos个字符起长度为len的子串 
    //其中,1 <= pos <= Stringth(s)且O <= len <= StringLength(S)-pos+1  
        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)); 
                for(i=0; ich[i] = S->ch[pos-1+i]; 
                Sub->length = len; 
        } 
        return OK; 
} 
 
void output(const HString *S){ 
        int i; 
 
        for(i= 0; i< S->length; i++) 
                printf("%c", S->ch[i]); 
        printf("\n"); 
} 
 
int main() 
{ 
        HString S1, S2, S3, S4; 
        int compare; 
 
        StrAssign(&S1, "Hello, World."); 
        output(&S1); 
        printf("The length of S1: %d\n", StrLength(&S1) ); 
         
        StrAssign(&S2, "Hello, BugYou."); 
        output(&S2); 
        printf("The length of S2: %d\n", StrLength(&S2) ); 
         
        printf("\nCompare S1 to S2:\n"); 
        compare = StrCompare(&S1, &S2); 
        if(compare > 0) printf("S1 > S2.\n"); 
        else if(compare == 0) printf("S1 = S2.\n"); 
        else printf("S1 < S2\n"); 
         
        printf("\nConcat String S1 and S2:\n"); 
        Concat(&S3, &S1, &S2); 
        output(&S3); 
        printf("The length of S3: %d\n", StrLength(&S3) ); 
        //printf("%d\n", strlen(S3.ch));    
         
        printf("\nSubset of String S1:\n"); //子集 
        SubString(&S4, &S1, 1, 5); 
        output(&S4); 
 
        printf("\nClear String S3:\n"); 
        ClearString(&S3); 
        printf("Length: %d\n", S3.length); 
 
        free(S1.ch); 
        free(S2.ch); 
        if(S3.ch) { free(S3.ch);printf("Free S3.\n"); } 
        free(S4.ch); 
        system("pause"); 
        return 0; 
}