順序表示的串——順序串2——串的基本操作
阿新 • • 發佈:2018-12-01
利用串的基礎運算,對串進行賦值、比較、插入、刪除、連線等操作。
【分析】
主要考察串的建立、定位、刪除等操作。
SeqString.h
#pragma once #include <iostream> using namespace std; #define MAXSIZE 100 #define MaxLen 50 //儲存結構 typedef struct { char str[MAXSIZE]; int Length; }SeqString; //串的賦值 void StrAssign(SeqString *S, char cstr[]) { int i=0; for (i = 0; cstr[i] != '\0';i++) { S->str[i] = cstr[i]; } S->Length = i; } //判斷串是否為空 int StrEmpty(SeqString S) { if (S.Length==0) { return 1; } else { return 0; } } //求串的長度 int StrLength(SeqString S) { return S.Length; } //串的複製 void StrCopy(SeqString *T, SeqString S) { int i; for (i = 0; i < S.Length;i++) { T->str[i] = S.str[i]; } T->Length = S.Length; } //比較兩個串的大小 int StrCompare(SeqString S, SeqString T) { int i; for (i = 0; i < S.Length&&T.Length;i++) { if (S.str[i]!=T.str[i]) { return (S.str[i] - T.str[i]); } } return (S.Length - T.Length); } /*在串的第pos位置插入串T。若插入成功,返回1;否則返回0.*/ /*串的插入操作具體實現分為3種情況: 第1種情況,在S中插入T後串長不超過能容納的最長字元,即S->Length+T.Length<=MaxLen 則先將串S中pos後的字元向後移動Len個位置,然後將串T插入S中即可; 第2種情況,若將T插入S後,串長超過能容納的最長字元但T能完全插入S中,即S->Length+T.Length>MaxLen 則將串S中pos後的字元往後移動Len個位置後,S中的部分字元被捨棄; 第3種情況,將T插入S中,有S->Length+T.Length>MaxLen且T不能完全被插入S中 則T中部分字元和S中第Len位置以後的字元均被捨棄。*/ int StrInsert(SeqString *S, int pos, SeqString T) { int i; if (pos < 0||pos-1>S->Length) { cout << "插入位置不正確!"; return 0; } if (S->Length + T.Length <=MaxLen) { for (i = S->Length + T.Length - 1; i >= pos + T.Length - 1;i--) { S->str[i] = S->str[i - T.Length]; } for (i = 0; i < T.Length;i++) { S->str[pos + i - 1] = T.str[i]; } S->Length = S->Length + T.Length; return 1; } else if (pos+T.Length<=MaxLen) { for (i = MaxLen - 1; i > T.Length + pos - 1;i--) { S->str[i] = S->str[i - T.Length]; } for (i = 0; i < T.Length;i++) { S->str[i + pos - 1] = T.str[i]; } S->Length = MaxLen; return 0; } else { for (i = 0; i < MaxLen - pos;i++) { S->str[i + pos - 1] = T.str[i]; } S->Length = MaxLen; return 0; } } /*刪除串S中pos開始的len個字元*/ int StrDelete(SeqString *S, int pos, int len) { int i; if (pos<0||len<0||pos+len-1>S->Length) { cout << "刪除位置不合法,引數len不合法!"; return 0; } else { for (i = pos + len; i <= S->Length - 1;i++) { S->str[i - len] = S->str[i]; } S->Length = S->Length - len; return 1; } } /*將串S連線在串T的末尾。串的連線操作可以分為兩種情況: 第1種,連線後串長T->Length+S.Length≤MaxLen,則直接將串S連線在串T的尾部; 第2種,連線後串長T->Length+S.Length≥MaxLen且串的長度<MaxLen,則串S會有字元丟失。*/ int StrConcat(SeqString *T, SeqString S) { int i, flag; if (T->Length+S.Length<=MaxLen) { for (i = T->Length; i < T->Length + S.Length;i++) { T->str[i] = S.str[i - T->Length]; } T->Length = T->Length + S.Length; flag = 1; } else if(T->Length<MaxLen) { for (i = T->Length; i < MaxLen;i++) { T->str[i] = S.str[i - T->Length]; } T->Length = MaxLen; flag = 0; } return flag; } /*清空串操作*/ void StrClear(SeqString *S) { S->Length = 0; }
main.cpp
#include <stdlib.h> #include <iostream> #define MAX 255 #include "SeqString.h" #include <string.h> int DelSubString(SeqString *S, int pos, int n); void DelAllString(SeqString *S1, SeqString *S2); void StrPrint(SeqString S) /*串的輸出*/ { int i = 0; for (i = 0; i < S.Length;i++) { cout << S.str[i]; } cout << endl; } void DispPrompt() { printf("\n\t*************************************"); printf("\n\t* 串的基本操作及應用 *"); printf("\n\t ***********************************\n"); printf("\t * 1.串的賦值 2.串比較 *\n"); printf("\t * 3.串的長度 4.串的連線 *\n"); printf("\t * 5.串的插入 6.串的刪除 *\n"); printf("\t * 7.清空佇列 8.退出 *\n"); printf("\n\t ***********************************\n"); } void main() { int i, pos, k; char str[MAX]; SeqString S, T; while (1) { DispPrompt(); cout << "請輸入選項<1-8>:" << endl; scanf("%d", &k); if (k<0||k>8) { cout << "輸入有誤,請重新輸入!" << endl; continue; } switch (k) { case 1: cout << "字串的賦值:\n"; cout << "請輸入一個字串!\n"; cout << "請輸入1個字串;"; cin >> str; StrAssign(&S, str); cout << "你輸入的字串為:" << endl; StrPrint(S); cout << endl; break; case 2: cout << "串的比較:" << endl; cout <<"請輸入兩個字串!\n" ; cout << "請輸入第1個字串:"; cin >> str; StrAssign(&S, str); cout << "請輸入第2個字串:"; cin >> str; StrAssign(&T, str); i = StrCompare(S, T); if (i==0) { cout << "兩個字串相等!" << endl; } else if(i<0) { cout << "第1個字串比第2個字串小!"; } else { cout << "第1個字串比第2個字串大!"; } break; case 3: cout << "求字串的長度:\n"; cout << "請輸入字串:" << endl; cin >> str; StrAssign(&S, str); i = StrLength(S); cout << "串的長度為:" << i << endl; break; case 4: cout << "字串連線\n" ; cout << "請輸入第1個字串;"; cin >> str; StrAssign(&S, str); cout << "請輸入第2個字串;"; cin >> str; StrAssign(&T, str); i = StrConcat(&S, T); if (i==0) { cout << "連線失敗!"<<endl; } else { cout << "連線後的新串為:" << endl; StrPrint(S); } break; case 5: cout << "字串插入:" << endl; cout << "請輸入主字串:"<<endl; cin >> str; StrAssign(&S, str); cout << "請輸入插入的字串:" << endl; cin >> str; StrAssign(&T, str); cout << "請輸入插入的位置:"<<endl; cin >> pos; StrInsert(&S, pos, T); cout << "插入後主主字串變為:" << endl; StrPrint(S); cout << endl; break; case 6: cout << "刪除主字串中的子字串:" << endl; cout << "請輸入主字串:" << endl; cin >> str; StrAssign(&S, str); cout << "請輸入子字串:" << endl; cin >> str; StrAssign(&T, str); DelAllString(&S, &T); cout << "刪除所有子字串後的字串:" << endl; StrPrint(S); break; case 7: StrClear(&S); case 8: break; } } system("pause"); } int Index(SeqString *S1, SeqString *S2) { int i = 0, j, k; while (i<S1->Length) { j = 0; if (S1->str[i]==S2->str[j]) { k = i + 1; j++; while (k<S1->Length&&j<S2->Length&&S1->str[k]==S2->str[j]) { k++; j++; } if (j==S2->Length) { break; } else { i++; } } else { i++; } } if (i>=S1->Length) { return -1; } else { return i + 1; } } int DelSubString(SeqString *S, int pos, int n) { int i; if (pos+n-1>S->Length) { return 0; } for (i = pos + n - 1; i < S->Length;i++) { S->str[i - n] = S->str[i]; } S->Length = S->Length - n; S->str[S->Length] = '\0'; return 1; } int STRLENGTH(SeqString *S) { return S->Length; } void DelAllString(SeqString *S1, SeqString *S2) { int n; n = Index(S1, S2); while (n>=0) { DelSubString(S1, n, STRLENGTH(S2)); n = Index(S1,S2); } }
執行結果: