1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| #include<iostream> #include<stdlib.h> using namespace std; #define OK 1 #define FALSE 0 #define ERROR 0 #define TRUE 1 #define Status int typedef struct { char *ch; int length; }HString; Status StrInsert(HString &S, int pos, HString T) { if (pos<1 || pos>S.length + 1) return ERROR; if (T.length) { S.ch = (char *)realloc(S.ch, (S.length + T.length) * sizeof(char)); for (int i = S.length - 1; i >= pos - 1; --i) S.ch[i + T.length] = S.ch[i]; for (int i = 0; i < T.length; i++) S.ch[pos - 1 + i] = T.ch[i]; S.length += T.length; } } Status StrAssign(HString &T, char *chars) { int i; char *c = chars; if (T.ch) free(T.ch); for (i = 0; *c; ++i, ++c); if (!i) { T.ch = NULL; T.length = 0; } else { if (!(T.ch = (char *)malloc(i * sizeof(char)))) exit(OVERFLOW); for (int j = 0; j < i; j++) { T.ch[j] = chars[j]; } T.length = i; } return OK; } int StrLength(HString S) { return S.length; } int StrCompare(HString S, HString T) { for (int 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; } Status ClearString(HString &S) { if (S.ch) { free(S.ch); S.ch = NULL; } S.length = 0; return OK; } Status Concat(HString &T, HString S1, HString S2) { if (T.ch) free(T.ch); T.ch = (char *)malloc((S1.length + S2.length) * sizeof(char)); for (int i = 0; i < S1.length; i++) { T.ch[i] = S1.ch[i]; } T.length = S1.length + S2.length; for (int i = 0; i < S2.length; i++) { T.ch[S1.length + i] = S2.ch[i]; } return OK; } Status SubString(HString &Sub, HString S, int pos, int len) { if (pos<0 || 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 (int i = 0; i < len; i++) { Sub.ch[i] = S.ch[pos - 1 + i]; } Sub.length = len; } return OK; } void DispStr(HString S) { int i; if (S.length > 0) { for (i = 0; i < S.length; i++) { cout << S.ch[i]; } cout << endl; } } Status StrCopy(HString &S, HString T) { if (S.ch) free(S.ch); S.ch = (char *)malloc((T.length) * sizeof(char)); for (int i = 0; i < T.length; i++) { S.ch[i] = T.ch[i]; } S.length = T.length; return OK; } Status StrDelete(HString &S, int pos, int len) { if (pos<0 || pos>S.length || len<0 || len>S.length - pos + 1) return ERROR; for (int i = 0; i < len; i++) { S.ch[pos - 1 + i] = S.ch[pos - 1 + len + i]; } S.length -= len; return OK; } void main() { HString T, S, S1, S2, Sub; int pos, len; char *a, str[100], *chars = "House"; T.ch = NULL; S.ch = NULL; S1.ch = NULL; S2.ch = NULL; Sub.ch = NULL; cout << "输出字符串常量chars:" << chars << endl; StrAssign(T, chars); cout << "生成一个其值等于串常量chars的串T:"; DispStr(T); StrCopy(S, T); cout << "将串T的值复制到串S中,串S的值为:"; DispStr(S); cout << "串S的长度为:" << StrLength(S) << endl; cout << "比较串S和串T的大小:" << StrCompare(S, T) << endl; cout << "将S清为空串!" << endl; ClearString(S); cout << "串S的值为:"; DispStr(S); cout << "串T的值为:"; DispStr(T); a = str; cout << "输入串S1:"; cin >> a; StrAssign(S1, a); cout << "串S1的值为:"; DispStr(S1); cout << "输入串S2:"; cin >> a; StrAssign(S2, a); cout << "串S2的值为:"; DispStr(S2); cout << "返回由S1和S2联接而成的新串S:"; Concat(S, S1, S2); DispStr(S); cout << "输入S的子串的起始字符位置:"; cin >> pos; cout << "输入S的子串的长度"; cin >> len; cout << "用Sub返回串S的第" << pos << "个字符起长度为" << len << "的子串:"; SubString(Sub, S, pos, len); DispStr(Sub); cout << "输入串S的插入位置:"; cin >> pos; cout << "在串S的第" << pos << "个位置插入串T,插入后串S的值为:"; StrInsert(S, pos, T); DispStr(S); cout << "输入串S删除的子串的起始字符位置:"; cin >> pos; cout << "输入串S删除的子串的长度:"; cin >> len; cout << "串S中删除第" << pos << "个字符起长度为" << len << "的子串,删除后S的值为:"; StrDelete(S, pos, len); DispStr(S); system("pause"); }
|