串的堆分配存储表示[原创]

  1/*=================串的堆分配存储表示====================*/
  2#include <stdio.h>
  3#include <malloc.h>
  4typedef struct{
  5    char *ch;
  6    int length;
  7}
hstring;
  8/*==============================*/
  9int print(hstring str){
 10    int i=str.length,j=0;
 11    if(!i){
 12        printf("\nthe string is empty..\n");
 13        return 0;
 14    }

 15    printf("The string:~");
 16
 17    while(i--){
 18        printf("%c",str.ch[j]);
 19        j++;
 20    }

 21}

 22/*===========基本操作函数原型说明=============*/
 23int strassign(hstring *t,char *chars){/* 生成一个其值等于串常量chars的串t*/
 24    int i,j=0;
 25    char *c;
 26    if(t->ch) free(t->ch);/* 释放t指向的原有空间*/
 27
 28    for(i=0,c=chars ; c ; ++i,++c); /* 求chars的长度i*/
 29
 30    if(!i){ t->ch=NULL; t->length=0;}/* 当chars所指串为空的情况*/
 31    else{
 32        if(!(t->ch=(char *)malloc(i*sizeof(char))))    return 0;
 33        while(--i){/*--i而不是i--*/
 34            t->ch[j]=chars[j];
 35            t->length=i;
 36            j++;
 37        }

 38    }

 39    return 1;
 40}

 41
 42
 43int strcompare(hstring s,hstring t){/*比较两个串*/
 44    int i;
 45    for(i=0;i<s.length&&i<t.length;++i)
 46        if(s.ch[i]!=t.ch[i])    return s.ch[i]-t.ch[i];
 47    return s.length-t.length;
 48}

 49
 50
 51int clearstring(hstring *s){/*将s清空*/
 52    if(s->ch){
 53        free(s->ch);
 54        s->ch=NULL;
 55    }

 56    s->length=0;
 57    return 1;
 58}

 59
 60
 61int concat(hstring *t,hstring s1,hstring s2){/*用t返回由s1,s2联结成的新串*/
 62    int i,j=0;
 63    if(t->ch) free(t->ch);
 64    if(!(t->ch=(char *)malloc((s1.length+s2.length)*sizeof(char))))    return 0;
 65
 66    i=s1.length;
 67    while(--i){/*先将s1复制到t中*/
 68        t->ch[j]=s1.ch[j];
 69        j++;
 70    }

 71
 72    t->length=s1.length+s2.length;
 73
 74    i=s2.length; j=0;
 75    while(--i){/*再将s2联结到t后面*/
 76        t->ch[j+s1.length]=s2.ch[j];
 77        j++;
 78    }

 79    return 1;
 80}

 81
 82
 83void substring(hstring *sub,hstring s,int pos,int len){
 84    /*用sub返回串s的第pos个字符起长度为len的子串*/
 85    int i,j;
 86    if(pos<1 || pos>s.length || len<0 || len>s.length-pos+1){
 87        printf("\nplease input the correct pos or len..\n");
 88        return 0;
 89    }

 90
 91    if(sub->ch) free(sub->ch);/*释放旧空间*/
 92
 93    if(!len){/*返回空子串的情况*/
 94        sub->ch=NULL;
 95        sub->length=0;
 96    }

 97    else{/*完整子串*/
 98        sub->ch=(char*)malloc(len*sizeof(char));
 99        j=len;  j=0;
100        while(--i){
101            sub->ch[j]=s.ch[pos-1+j];
102            j++;
103        }

104        sub->length=len;
105    }

106}

107/*================主函数部分======================*/
108main(){
109     hstring *hs,*str,*st,*sub;
110
111     str->ch="YANG ZHOU UNIVERCITY";  str->length=20;
112     st->ch="JIAO JI 07";  st->length=10;
113     hs->ch="";   hs->length=0;
114     sub->ch="";   sub->length=0;
115
116
117     print(*hs);
118
119     strassign(hs,str->ch);
120     printf("\nstrassign(hs,str),");
121     print(*hs);
122
123     printf("the string st,");/**/
124     print(*st);
125
126     if(strcompare(*str,*st)>0) printf("\nstr>st\n");/**/
127
128     clearstring(str);/**/
129     printf("clearstring(str),str,");
130     print(*str);
131
132     concat(hs,*str,*st);
133     printf("\nconcat(hs,*str,*st),");
134     print(*hs);
135
136     substring(sub,*hs,5,10); /**/
137     printf("\nsubstring(sub,*hs,5,10),print sub,");
138     print(*sub);
139
140     substring(sub,*hs,10,15);/**/
141     printf("\nsubstring(sub,*hs,10,15),print sub,");
142     print(*sub);
143
144
145getch();
146}

147    
148
posted @ 2009-07-25 10:16  林田惠  阅读(846)  评论(0编辑  收藏  举报


.
    ;  `┣━┒ ; `.┣─┒`   . .   ;   `.        
   .┟━┃┍╄┓┟━│ ╃━  `     、.    
` ┝─┃┣╈┤┣━┃;/ ╈ ╰⌒ˋのˊ  
 . ┗━┘┗┸┛└━┛/┃┻ `.   ′ ˋ `