定长顺序串的基本操作

  1 #define _CRT_SECURE_NO_WARNINGS//解决vs下scanf报错
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #define MAXLEN 5
  5 
  6 typedef struct
  7 {
  8     char ch[MAXLEN];
  9     int len;
 10 }SString;//selected length string
 11 
 12 void Get(SString* str);
 13 void Put(SString* str);
 14 void Insert(SString* strA, int pos, SString* strB);
 15 void Delete(SString* strA, int pos, int len);
 16 void Cat(SString* strA, const SString strB);
 17 void Sub(const SString strA, SString* strB, int pos, int len);//求子串
 18 
 19 int main(void)
 20 {
 21     int pos;//插入字符串位置
 22     int len;//删除字符串长度
 23     SString strA = {"", 0};
 24     SString strB = {"", 0};
 25 
 26     Get(&strA);
 27     Put(&strA);
 28     Get(&strB);
 29     Put(&strB);
 30     
 31     printf("请输入要插入字符串的位置:\n");
 32     scanf("%d", &pos);
 33     Insert(&strA, pos, &strB);
 34     Put(&strA);
 35 
 36     printf("请输入要删除字符串的位置和长度(以逗号分割)\n");
 37     scanf("%d,%d", &pos, &len);
 38     Delete(&strA, pos, len);
 39     Put(&strA);
 40 
 41     printf("正在连接两个字符串:\n");
 42     Cat(&strA, strB);
 43     Put(&strA);
 44 
 45     printf("请输入子串在主串的位置和长度(逗号分隔)\n");
 46     scanf("%d,%d", &pos, &len);
 47     Sub(strA, &strB, pos, len);
 48     Put(&strB);
 49 
 50     system("pause");
 51     return 0;
 52 }
 53 
 54 void Get(SString* str)
 55 {
 56     int x;
 57     printf("请输入一串字符:\n");
 58     while ((x = getchar()) != '\n')
 59     {
 60         if (str->len > MAXLEN - 1)
 61         {
 62             printf("串空间已满,已截断!\n");
 63             while ((x = getchar()) != EOF && x != '\n');//清空缓存区
 64             break;
 65         }
 66         str->ch[(str->len++)] = x;
 67     }
 68 }
 69 
 70 void Put(SString* str)
 71 {
 72     int i;
 73     printf("len:%d\t", str->len);
 74     printf("str:");
 75     for (i = 0; i < str->len; i++)
 76     {
 77         printf("%c", str->ch[i]);
 78     }
 79     printf("\n");
 80 }
 81 
 82 void Insert(SString* strA, int pos, SString* strB)
 83 {
 84     int i;
 85     if (pos < 1 || pos > MAXLEN)
 86     {
 87         printf("\n插入位置不合法!\n");
 88         exit(-1);
 89     }
 90 
 91     pos = pos - 1;//字符串数组下标从0开始
 92     if (strA->len + strB->len <= MAXLEN)//插入后串长小于MAXLEN
 93     {
 94         for (i = strA->len + strB->len; i > pos + strB->len -1; i--)//插入后
 95         {
 96             strA->ch[i] = strA->ch[i - strB->len];
 97         }
 98         for (i = pos; i < pos + strB->len; i++)//插入部分
 99         {
100             strA->ch[i] = strB->ch[i - pos];
101         }
102         strA->len = strA->len + strB->len;
103     }
104     else if (pos + strB->len <= MAXLEN)//插入后串长大于MAXLAN,串B可以全部插入
105     {
106         for (i = MAXLEN - 1; i > pos + strB->len - 1; i--)
107         {
108             strA->ch[i] = strA->ch[i - strB->len];
109         }
110         for (i = pos; i < pos + strB->len; i++)
111         {
112             strA->ch[i] = strB->ch[i - pos];
113         }
114     }
115     else//插入后串长大于MAXLAN,串B的部分字符也可能被舍弃
116     {
117         for (i = pos; i < MAXLEN; i++)
118         {
119             strA->ch[i] = strB->ch[i - pos];
120         }
121     }
122 }
123 
124 void Delete(SString* strA, int pos, int len)
125 {
126     int i;
127     if (pos < 1 || pos > strA->len || len < 1 || len > strA->len - pos + 1)
128     {
129         printf("\n删除参数不合法!\n");
130         exit(-1);
131     }
132     pos = pos - 1;//字符串数组下标从0开始
133     for (i = pos; i <= strA->len - len; i++)
134     {
135         strA->ch[i] = strA->ch[i + len];
136     }
137     strA->len = strA->len - len;
138 }
139 
140 void Cat(SString* strA, const SString strB)
141 {
142     int i;
143     if (strA->len + strB.len <= MAXLEN)//连接后串长不大于MAXLEN
144     {
145         for (i = strA->len; i < strA->len + strB.len; i++)
146         {
147             strA->ch[i] = strB.ch[i - strA->len];
148         }
149         strA->len = strA->len + strB.len;
150     }
151     else if (strA->len < MAXLEN)//连接后串长大于MAXLEN,且串A的长度小于MAXLEN,串B的部分字符被舍弃
152     {
153         for (i = strA->len; i < MAXLEN; i++)
154         {
155             strA->ch[i] = strB.ch[i - strA->len];
156         }
157         strA->len = MAXLEN;
158     }
159     else//串A的长度等于MAXLEN,串B不被连接
160     {
161         ;
162     }
163 }
164 
165 void Sub(const SString strA, SString* strB, int pos, int len)
166 {
167     int i;
168     if (len < 1 || len > strA.len - pos + 1 || pos < 1 || pos > strA.len)
169     {
170         printf("\n求子串参数不合法!\n");
171         exit(-1);
172     }
173     pos = pos - 1;//字符串数组下标从0开始
174     for (i = 0; i < len; i++)
175     {
176         strB->ch[i] = strA.ch[pos + i];
177     }
178     strB->len = len;
179 }

 

posted @ 2021-12-16 21:13  吕辉  阅读(171)  评论(0)    收藏  举报