其他分享
首页 > 其他分享> > 堆串

堆串

作者:互联网

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 
  5 typedef struct
  6 {
  7     char* ch;
  8     int len;
  9 }HString;
 10 
 11 void Assign(HString* A, const char* a);//赋值串
 12 void Put(HString* A);
 13 void Insert(HString* A, int pos, const HString B);
 14 void Delete(HString* A, int pos, int len);
 15 void Cat(HString* A, const HString B);
 16 void Sub(HString* A, int pos, int len, HString* B);
 17 
 18 int main(void)
 19 {
 20     int pos;//插入位置
 21     int len;
 22     HString A = { NULL, 0 };
 23     HString B = { NULL, 0 };
 24     char a[] = "hello world!";
 25     char b[] = "hi!";
 26 
 27     Assign(&A, a);
 28     Assign(&B, b);
 29     Put(&A);
 30     Put(&B);
 31 
 32     printf("请输入要插入的位置\n");
 33     scanf("%d", &pos);
 34     Insert(&A, pos, B);
 35     Put(&A);
 36 
 37     printf("\n请输入要删除的位置和长度(逗号分隔)\n");
 38     scanf("%d,%d", &pos, &len);
 39     Delete(&A, pos, len);
 40     Put(&A);
 41 
 42     printf("\n正在连接A和B\n");
 43     Cat(&A, B);
 44     Put(&A);
 45     
 46     printf("\n请输入子串在主串的位置和长度(逗号分隔)\n");
 47     scanf("%d,%d", &pos, &len);
 48     Sub(&A, pos, len, &B);
 49     Put(&B);
 50 
 51     system("pause");
 52     return 0;
 53 }
 54 
 55 void Assign(HString* A, const char* a)
 56 {
 57     int i = 0;
 58     while (a[i] != '\0')
 59     {
 60         i++;
 61     }
 62     A->len = i;//串A的长度等于串a的长度
 63     if (A->len != 0)
 64     {
 65         if (A->ch != NULL)
 66         {
 67             free(A->ch);
 68         }
 69         if (NULL == (A->ch = (char*)calloc(A->len, sizeof(char))))//空间开辟失败
 70         {
 71             printf("\nclear and allocation fail !\n");
 72                 exit(-1);
 73         }
 74         for (i = 0; i < A->len; i++)
 75         {
 76             A->ch[i] = a[i];
 77         }
 78     }
 79     else//当串A长度为0时,串A为空串
 80     {
 81         A->ch = NULL;
 82     }
 83 }
 84 
 85 void Put(HString* A)
 86 {
 87     int i;
 88     printf("串值为:");
 89     for (i = 0; i < A->len; i++)
 90     {
 91         printf("%c", A->ch[i]);
 92     }
 93     printf("\n");
 94 }
 95 
 96 void Insert(HString* A, int pos, const HString B)
 97 {
 98     int i;
 99     char* temp;
100     if (pos < 0 || pos > A->len + 1)
101     {
102         printf("\n插入位置不合法!\n");
103         exit(-1);
104     }
105     pos = pos - 1;//数组下标从0开始
106     if (NULL == (temp = (char*)calloc(A->len + B.len, sizeof(char))))
107     {
108         printf("\nclean and callocation fail !\n");
109         exit(-1);
110     }
111     for (i = 0; i < pos; i++)//把A串pos之前的字符赋给temp
112     {
113         temp[i] = A->ch[i];
114     }
115     for (i = pos; i < pos + B.len; i++)//把temp之间部分赋成串B的内容
116     {
117         temp[i] = B.ch[i - pos];
118     }
119     for (i = pos + B.len; i < A->len + B.len; i++)//把原pos之后的内容连接
120     {
121         temp[i] = A->ch[i - B.len];
122     }
123     free(A->ch);
124     A->ch = temp;
125     A->len = A->len + B.len;
126 }
127 
128 void Delete(HString* A, int pos, int len)
129 {
130     int i;
131     char* temp;
132     if (pos < 1 || pos > A->len || len < 1 || len > (A->len - pos + 1))
133     {
134         printf("%d", len);
135         printf("\n删除参数不合法!\n");
136         exit(-1);
137     }
138     pos = pos - 1;
139     if (NULL == (temp = (char*)calloc(A->len - len, sizeof(char*))))
140     {
141         printf("\nclean and callocation fail !\n");
142         exit(-1);
143     }
144     for (i = 0; i < pos; i++)//把A串pos之前的字符复制给temp
145     {
146         temp[i] = A->ch[i];
147     }
148     for (i = pos; i < A->len - len; i++)//把A串删除部分字符后,剩余部分字符复制给temp
149     {
150         temp[i] = A->ch[i + len];
151     }
152     free(A->ch);
153     A->ch = temp;
154     A->len = A->len - len;
155 }
156 
157 void Cat(HString* A, const HString B)
158 {
159     int i;
160     if (NULL == (A->ch = (char*)realloc(A->ch, (A->len + B.len) * sizeof(char))))//函数realloc保留了串A原有的字符内容
161     {
162         printf("\nreset allocation fail !\n");
163         exit(-1);
164     }
165     for (i = A->len; i < A->len + B.len; i++)
166     {
167         A->ch[i] = B.ch[i - A->len];
168     }
169     A->len = A->len + B.len;
170 }
171 
172 void Sub(HString* A, int pos, int len, HString* B)
173 {
174     int i;
175     if (pos < 1 || pos > A->len + 1 || len < 1 || len > A->len - pos + 1)
176     {
177         printf("\n求子串参数不合法!\n");
178         exit(-1);
179     }
180     pos = pos - 1;
181     B->len = 0;
182     if (B->ch != NULL)//清空子串
183     {
184         free(B->ch);
185     }
186     if (NULL == (B->ch = (char*)calloc(len, sizeof(char))))
187     {
188         printf("\nclean and allocation fail !\n");
189         exit(-1);
190     }
191     for (i = pos; i < pos + len; i++)
192     {
193         B->ch[i - pos] = A->ch[i];
194     }
195     B->len = len;
196 }

 

标签:堆串,ch,int,HString,pos,len,char
来源: https://www.cnblogs.com/lvhui123/p/15703988.html