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
| #include<iostream> #include<string> #define Status int #define TRUE 1 #define FALSE 0 #define ERROR 0 #define OK 1 #define MAXSTRLEN 255 typedef unsigned char SString[MAXSTRLEN + 1]; using namespace std; Status Concat(SString &T, SString S1, SString S2) { bool uncut; if (S1[0] +S2[0] <= MAXSTRLEN) { for (int i = 1; i <= S1[0]; i++) { T[i] = S1[i]; } for (int i = 1; i <= S2[0]; i++) { T[i + S1[0]] = S2[i]; } T[0] = S1[0] + S2[0]; uncut = TRUE; } else if (S1[0] < MAXSTRLEN) { for (int i = 1; i <= (int)S1[0]; i++) { T[i] = S1[i]; } for (int i = 1; i <= MAXSTRLEN - (int)S1[0]; i++) { T[i + S1[0]] = S2[i]; } T[0] = MAXSTRLEN; uncut = FALSE; } else { for (int i = 0; i <= MAXSTRLEN; i++) { T[i] = S1[i]; } uncut = FALSE; } return uncut; } Status SubString(SString &Sub, SString S, int pos, int len) { if (pos<1 || pos>(int)S[0] || len<0 || len>(int)S[0] - pos + 1) return ERROR; for (int i = 1; i <= len; i++) { Sub[i] = S[pos + i-1]; } Sub[0] = len; return OK; } void main() { SString T, S1, S2, S, Sub; int i, pos, len; cout << "输入串S1的长度:"; cin >> S1[0]; S1[0] -= 48; getchar(); cout << "输入串S1的值:"; for (i = 1; i <= S1[0]; i++) { cin >> S1[i]; } cout <<"输出串S1的值:"<< endl; for (i = 1; i <= S1[0]; i++) { cout << S1[i]; } cout << endl; cout << "输入串S2的长度:"; cin >> S2[0]; S2[0] -= 48; getchar(); cout << "输入串S2的值:"; for (i = 1; i <= S2[0]; i++) { cin >> S2[i]; } cout <<"输出串S2的值:"<< endl; for (i = 1; i <= S2[0]; i++) { cout << S2[i]; } cout << endl; Concat(T, S1, S2); cout << "输出串S1和串S2的联接串T的值:" << endl; for (i = 1; i <= T[0]; i++) { cout << T[i]; } cout << endl; cout << "输入串S的长度:"; cin >> S[0]; S[0] -= 48; getchar(); cout << "输入串S的值:" << endl; for (i = 1; i <= S[0]; i++) { cin >> S[i]; } cout << "输出串S的值:" << endl; for (i = 1; i <= S[0]; i++) { cout << S[i]; } cout << endl; cout << "输入串S的子串Sub的开始字符位置:"; cin >> pos; cout << "输入串S的子串Sub的长度:"; cin >> len; SubString(Sub, S, pos, len); cout << "输出子串Sub的值:" << endl; for (i = 1; i <= Sub[0]; i++) { cout << Sub[i]; } cout << endl; system("pause"); }
|