数据结构---第四章:串

1.块链实现串,下面是STRING.h头文件

#pragma once
//--------------------------下面是 串 的块链存储表示------------------------------------------

#include
#include
#include
#include
#include

#define TRUE			  1
#define FALSE			  0
#define OK				  1
#define ERROR			  0
#define INFEASIBLE		 -1
#define OVERFLOW		 -2
#define  CHUNKSIZE  80  // 可由用户定义的块大小
#define blank  '#'      //'#'作为填补空余的字符,不计为串的字符
using namespace std;
typedef int Status;

//--------------------------下面是 块-链 的声明---------------------------------------------

//用链表来存储串值,通常一个结点中存放一个子串。
typedef  struct Chunk {  // 结点结构
    char  ch[CHUNKSIZE];
    struct Chunk* next;
} Chunk;

typedef struct {  // 串的链表结构
    Chunk* head, * tail; // 串的头和尾指针
    int   curlen;     // 串的当前长度
} LString;
//例如: 在编辑系统中,整个文本编辑区可以看成是一个串,
//每一行是一个子串,构成一个结点。
//即: 同一行的串用定长结构(80个字符),   行和行之间用指针相联接。



//--------------------------下面是15个函数的声明---------------------------------------------

//1.初始化(产生空串)字符串T
void InitString(LString* T);

//2.生成一个其值等于chars的串T(要求chars中不包含填补空余的字符)。成功返回OK,否则返回ERROR 
Status StrAssign(LString* T, char* chars);

//3.将串T的内容转换为字符串,chars为其头指针。成功返回OK,否则返回ERROR。
Status ToChars(LString T, char** chars);

//4.初始条件:串S存在。操作结果:由串S复制得串T,去掉填补空余的字符。成功返回OK,否则返回ERROR
Status StrCopy(LString* T, LString S);

//5.初始条件:串S存在。操作结果:若S为空串,则返回TRUE,否则返回FALSE
Status StrEmpty(LString S);

//6.若S>T,则返回值>0;若S=T,则返回值=0;若Scurlen = 0;
    T->head = T->tail = NULL;
}

//2.生成一个其值等于chars的串T(要求chars中不包含填补空余的字符)。成功返回OK,否则返回ERROR 
Status StrAssign(LString* T, char* chars)
{ 
    int i, j, k, m;
    Chunk* p, * q= nullptr;
    i = strlen(chars); /* i为串的长度 */
    //cout << i << endl;
    if (!i || strchr(chars, blank)) /* 串长为0或chars中包含填补空余的字符 */
    {
        cout << "生成失败!" << endl;
        return ERROR;
    } 
    T->curlen = i;
    j = i / CHUNKSIZE; /* j为块链的结点数 */
    if (i % CHUNKSIZE)
    {
        j++;
    } 
    for (k = 0; k < j; k++)
    {
        p = (Chunk*)malloc(sizeof(Chunk)); /* 生成块结点 */
        if (!p) /* 生成块结点失败 */
        {
            return ERROR;
        }
        for (m = 0; m < CHUNKSIZE && *chars; m++) /* 给块结点的数据域赋值 */
        {
            *(p->ch + m) = *chars++;
        } 
        if (k == 0) /* 第一个链块 */
        {
            T->head = q = p; /* 头指针指向第一个链块 */
        }
        else
        {
            q->next = p;
            q = p;
        }
        if (!*chars) /* 最后一个链块 */
        {
            T->tail = q;
            q->next = NULL;
            for (; m < CHUNKSIZE; m++) /* 用填补空余的字符填满链表 */
            {
                *(q->ch + m) = blank;
            } 
        }
    }
    //cout << "串成功生成!" << endl;
    return OK;
}

//3.将串T的内容转换为字符串,chars为其头指针。成功返回OK,否则返回ERROR。
Status ToChars(LString T, char** chars)
{ 
    Chunk* p = T.head; /* p指向第1个块结点 */
    int i;
    char* q;
    *chars = (char*)malloc((T.curlen + 1) * sizeof(char));
    if (!*chars || !T.curlen) /* 生成字符串数组失败或串T长为0 */
    {
        return ERROR;
    } 
    q = *chars; /* q指向chars的第1个字符 */
    while (p) /* 块链没结束 */
    {
        for (i = 0; i < CHUNKSIZE; i++)
        {
            if (p->ch[i] != blank) /* 当前字符不是填补空余的字符 */
            {
                *q++ = (p->ch[i]); /* 赋给q所指字符空间 */
            }
        }   
        p = p->next;
    }
    (*chars)[T.curlen] = 0; /* 串结束符 */
  /*  cout << "字符串成功生成!" << endl;*/
    return OK;
}

//4.初始条件:串S存在。操作结果:由串S复制得串T,去掉填补空余的字符。成功返回OK,否则返回ERROR
Status StrCopy(LString* T, LString S)
{ 
    char* c;
    Status i;
    if (!ToChars(S, &c)) /* c为串S的内容 */
    {
        return ERROR;
    }
    i = StrAssign(T, c); /* 将串S的内容赋给T */
    free(c); /* 释放c的空间 */
 /*   cout << "成功去掉串中填补空余的字符!" << endl;*/
    return i;
}

//5.初始条件:串S存在。操作结果:若S为空串,则返回TRUE,否则返回FALSE
Status StrEmpty(LString S)
{ 
    if (!S.curlen) /* 非空 */
    {
        /*cout << "串为空!" << endl;*/
        return FALSE;
    }
    else
    {
        /*cout << "串不为空!" << endl;*/
        return TRUE;
    }
}

//6.若S>T,则返回值>0;若S=T,则返回值=0;若Shead;
    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }
    S->head = S->tail = NULL;
    S->curlen = 0;
}

//9.用T返回由S1和S2联接而成的新串(中间可能有填补空余的字符)
Status Concat(LString* T, LString S1, LString S2)
{ 
    InitString(T);
    LString a1, a2;
    Status i, j;
    InitString(&a1);
    InitString(&a2);
    i = StrCopy(&a1, S1);
    j = StrCopy(&a2, S2);
    if (!i || !j) /* 至少有1个串拷贝不成功 */
    {
        return ERROR;
    }   
    T->curlen = S1.curlen + S2.curlen; /* 生成串T */
    T->head = a1.head;
    a1.tail->next = a2.head; /* a1,a2两串首尾相连 */
    T->tail = a2.tail;
    return OK;
}

//10.用Sub返回串S的第pos个字符起长度为len的子串。其中,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1 
Status SubString(LString* Sub, LString S, int pos, int len)
{ 
    InitString(Sub);
    char* b, * c;
    Status i;
    if (pos<1 || pos>S.curlen || len<0 || len>S.curlen - pos + 1) /* pos或len值不合法 */
    {
        return ERROR;
    } 
    if (!ToChars(S, &c)) /* c为串S的内容 */
    {
        return ERROR;
    }  
    b = c + pos - 1; /* b指向串c中串Sub内容的首地址 */
    b[len] = 0; /* Sub结束处赋0(字符串结束符) */
    i = StrAssign(Sub, b); /* 将串b的内容赋给Sub */
    free(c);
    return i;
}

//11.T为非空串。若主串S中第pos个字符之后存在与T相等的子串,则返回第一个这样的子串在S中的位置,否则返回0 
int Index(LString S, LString T, int pos)
{ 
    int i, n, m;
    LString sub;
    if (pos >= 1 && pos <= StrLength(S)) /* pos满足条件 */
    {
        n = StrLength(S); /* 主串长度 */
        m = StrLength(T); /* 串T长度 */
        i = pos;
        while (i <= n - m + 1)
        {
            SubString(&sub, S, i, m); /* sub为从S的第i个字符起,长度为m的子串 */
            if (StrCompare(sub, T)) /* sub不等于T */
            {
                ++i;            /* 如果sub等于T,++i,i就一直后移 */
            }  
            else
            {
                return i;           //完美!!
            } 
        }
    }
    return 0;
}

//12. 1≤pos≤StrLength(S)+1。在串S的第pos个字符之前插入串T 
Status StrInsert(LString* S, int pos, LString T)
{ 
    char* b, * c;
    int i, j;
    Status k;
    if (pos<1 || pos>(*S).curlen + 1) /* pos的值超出范围 */
    {
        return ERROR;
    } 
    if (!ToChars(*S, &c)) /* c为串S的内容 */
    {
        return ERROR;
    } 
    if (!ToChars(T, &b)) /* b为串T的内容 */
    {
        return ERROR;
    }  
    j = (int)strlen(c); /* j为串S的最初长度 */
    c = (char*)realloc(c, (j + strlen(b) + 1) * sizeof(char)); /* 增加c的存储空间 */
    for (i = j; i >= pos - 1; i--)
    {
        c[i + strlen(b)] = c[i]; /* 为插入串b腾出空间 */
    } 
    for (i = 0; i < (int)strlen(b); i++) /* 在串c中插入串b */
    {
        c[pos + i - 1] = b[i];
    } 
    InitString(S); /* 释放S的原有存储空间 */
    k = StrAssign(S, c); /* 由c生成新的串S */
    free(b);
    free(c);
    return k;
}

//13.从串S中删除第pos个字符起长度为len的子串 
Status StrDelete(LString* S, int pos, int len)
{ 
    char* c;
    int i;
    Status k;
    if (pos<1 || pos>(*S).curlen - len + 1 || len < 0) /* pos,len的值超出范围 */
    {
        return ERROR;
    } 
    if (!ToChars(*S, &c)) /* c为串S的内容 */
    {
        return ERROR;
    }  
    for (i = pos + len - 1; i <= (int)strlen(c); i++)
    {
        c[i - len] = c[i]; /* c为删除后串S的内容 */
    }  
    InitString(S); /* 释放S的原有存储空间 */
    k = StrAssign(S, c); /* 由c生成新的串S */
    free(c);
    return k;
}

//14.初始条件:串S,T和V存在,T是非空串,操作结果:用V替换主串S中出现的所有与T相等的不重叠的子串
Status Replace(LString &S, LString T, LString V) /* 此函数与串的存储结构无关 */
{ 

    int i = 1; /* 从串S的第一个字符起查找串T */
    if (StrEmpty(T)) /* T是空串 */
        return ERROR;
    do
    {
        i = Index(S, T, i); /* 结果i为从上一个i之后找到的子串T的位置 */
        if (i) /* 串S中存在串T */
        {
            StrDelete(&S, i, StrLength(T)); /* 删除该串T */
            StrInsert(&S, i, V); /* 在原串T的位置插入串V */
            i += StrLength(V); /* 在插入的串V后面继续查找串T */
        }
    } while (i);
    return OK;
}

//15. 输出字符串T。
void StrPrint(LString T)
{
    int i = 0, j;
    Chunk* h;
    h = T.head;
    while (i < T.curlen)
    {
        for (j = 0; j < CHUNKSIZE; j++)
        {
            if (*(h->ch + j) != blank) /* 不是填补空余的字符 */
            {
                printf("%c", *(h->ch + j));
                i++;
            }
        } 
        h = h->next;
    }
    printf("\n");
}

2.块链实现串,下面是main函数进行测试(由于一些原因,Index和Replace操作有点BUG,但函数的代码没什么毛病)

#include
#include
#include
#include
#include
#include
#include "STRING.h"
using namespace std;


int main() {

	int select;	
	int pos = 0;
	int len = 0;
	LString ST0;
	InitString(&ST0);
	LString ST;
	InitString(& ST);
	LString ST1;
	InitString(& ST1);
	LString ST2;
	InitString(&ST2);
	char chars[CHUNKSIZE];
	char chars1[CHUNKSIZE];
	char chars2[CHUNKSIZE];

	do
	{
		cout << "----------------------------------------------------------------------------------------------------\n";
		cout << "请选择操作编号\n\n";
		cout << "1:输入串->生成串		2:串->字符串				3:去除填补字符 \n";
		cout << "4:判断串是否为空		5:比较两个串				6:求串的长度\n";
		cout << "7:清空串			8:拼接两个串				9:求串的定点定长度的子串\n";
		cout << "10:Index操作			11:在主串定点之前插入定串		12:在主串定点之后删除定长串\n";
		cout << "13:Replace操作			14:打印串				-1:退出\n";
		cout << "----------------------------------------------------------------------------------------------------\n";


		cin >> select;
		switch (select)
		{
		case  1 :
			cout << "---------------------------------\n";
			cout << "请选择对第几个串赋值:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				cout << "请输入:";
				cin >> chars;
				StrAssign(&ST, chars);		//生成一个其值等于chars的串T
				break;
			case 1:
				cout << "请输入:";
				cin >> chars1;
				StrAssign(&ST1, chars1);		//生成一个其值等于chars1的串T1
				break;
			case 2:
				cout << "请输入:";
				cin >> chars2;
				StrAssign(&ST2, chars2);		//生成一个其值等于chars2的串T2
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  2 :
			cout << "---------------------------------\n";
			cout << "请选择对第几个串转换:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				ToChars(ST, (char**)chars);
				break;
			case 1:
				ToChars(ST1, (char**)chars1);
				break;
			case 2:
				ToChars(ST2, (char**)chars2);
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  3 :
			cout << "---------------------------------\n";
			cout << "请选择对第几个串去除填补字符:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				StrCopy(&ST, ST);		//由串2复制得串1,去掉填补空余的字符。
				break;
			case 1:
				StrCopy(&ST1, ST1);		//由串2复制得串1,去掉填补空余的字符。
				break;
			case 2:
				StrCopy(&ST2, ST2);		//由串2复制得串1,去掉填补空余的字符。
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  4:
			cout << "---------------------------------\n";
			cout << "请判断第几个串是否为空:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				if (StrEmpty(ST))			//判断字符串是否为空
				{
					cout << "串不为空" << endl;
				}
				else
				{
					cout << "串为空" << endl;
				}
				break;
			case 1:
				if(StrEmpty(ST1))			//判断字符串是否为空
				{
					cout << "串不为空" << endl;
				}
				else
				{
					cout << "串为空" << endl;
				}
				break;
			case 2:
				if(StrEmpty(ST2))			//判断字符串是否为空
				{
					cout << "串不为空" << endl;
				}
				else
				{
					cout << "串为空" << endl;
				}
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  5:
			cout << "---------------------------------\n";
			cout << "比较两个串,先选第一个:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				cout << "---------------------------------\n";
				cout << "再选第二个:\n";
				cout << "1:第1个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 1:StrCompare(ST, ST1);	//若S>T,则返回值>0;若S=T,则返回值=0;若ST,则返回值>0;若S=T,则返回值=0;若S> select;
				switch (select)
				{
				case 0:StrCompare(ST1, ST);	//若S>T,则返回值>0;若S=T,则返回值=0;若ST,则返回值>0;若S=T,则返回值=0;若S> select;
				switch (select)
				{
				case 0:StrCompare(ST2, ST);	//若S>T,则返回值>0;若S=T,则返回值=0;若ST,则返回值>0;若S=T,则返回值=0;若S> select;
			switch (select)
			{
			case 0:
				cout << "串的长度为:" << StrLength(ST) << endl;	//返回S的元素个数,称为串的长度
				break;
			case 1:
				cout << "串的长度为:" << StrLength(ST1) << endl;	//返回S的元素个数,称为串的长度
				break;
			case 2:
				cout << "串的长度为:" << StrLength(ST2) << endl;	//返回S的元素个数,称为串的长度
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  7:
			cout << "---------------------------------\n";
			cout << "你想要清空第几个串:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				ClearString(&ST);	//初始条件:串S存在。操作结果:将S清为空串
				break;
			case 1:
				ClearString(&ST1);	//初始条件:串S存在。操作结果:将S清为空串
				break;
			case 2:
				ClearString(&ST2);	//初始条件:串S存在。操作结果:将S清为空串
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  8:
			cout << "---------------------------------\n";
			cout << "拼接两个串,先选第一个:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				cout << "---------------------------------\n";
				cout << "再选第二个:\n";
				cout << "1:第1个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 1:Concat(&ST2, ST, ST1);	//用T返回由S1和S2联接而成的新串(中间可能有填补空余的字符)
					cout << "新串2为: ";
					StrPrint(ST2);
					break;
				case 2:Concat(&ST1, ST, ST2); 	//用T返回由S1和S2联接而成的新串(中间可能有填补空余的字符)
					cout << "新串1为: ";
					StrPrint(ST1);
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			case 1:
				cout << "---------------------------------\n";
				cout << "再选第二个:\n";
				cout << "0:第0个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 0:Concat(&ST2, ST1, ST);	//用T返回由S1和S2联接而成的新串(中间可能有填补空余的字符)
					cout << "新串2为: ";
					StrPrint(ST2);
					break;
				case 2:Concat(&ST, ST1, ST2);	//用T返回由S1和S2联接而成的新串(中间可能有填补空余的字符)
					cout << "新串0为: ";
					StrPrint(ST);
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			case 2:
				cout << "---------------------------------\n";
				cout << "再选第二个:\n";
				cout << "0:第0个		1:第1个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 0:Concat(&ST1, ST2, ST);	//用T返回由S1和S2联接而成的新串(中间可能有填补空余的字符)
					cout << "新串1为: ";
					StrPrint(ST1);
					break;
				case 1:Concat(&ST, ST2, ST1);	//用T返回由S1和S2联接而成的新串(中间可能有填补空余的字符)
					cout << "新串0为: ";
					StrPrint(ST);
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  9:
			cout << "---------------------------------\n";
			cout << "请选择对第几个串操作:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				cout << "请输入起点: ";
				cin >> pos;
				cout << "请输入长度: ";
				cin >> len;
				SubString(&ST0, ST, pos, len);		//用Sub返回串S的第pos个字符起长度为len的子串。
				cout << "新串为: ";
				StrPrint(ST0);
				break;
			case 1:
				cout << "请输入起点: ";
				cin >> pos;
				cout << "请输入长度: ";
				cin >> len;
				SubString(&ST0, ST1, pos, len);		//用Sub返回串S的第pos个字符起长度为len的子串。
				cout << "新串为: ";
				StrPrint(ST0);
				break;
			case 2:
				cout << "请输入起点: ";
				cin >> pos;
				cout << "请输入长度: ";
				cin >> len;
				SubString(&ST0, ST2, pos, len);		//用Sub返回串S的第pos个字符起长度为len的子串。
				cout << "新串为: ";
				StrPrint(ST0);
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  10:
			cout << "---------------------------------\n";
			cout << "实现Index,先选第一个主串:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				cout << "---------------------------------\n";
				cout << "再选第二个子串:\n";
				cout << "1:第1个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 1:
					cout << "输入位置:";
					cin >> pos;
					cout << "位置是:"<< Index(ST, ST1, pos)<> pos;
					cout << "位置是:" << Index(ST, ST2, pos)<> select;
				switch (select)
				{
				case 0:
					cout << "输入位置:";
					cin >> pos;
					cout << "位置是:" << Index(ST1, ST, pos)<> pos;
					cout << "位置是:" << Index(ST1, ST2, pos)<> select;
				switch (select)
				{
				case 0:
					cout << "输入位置:";
					cin >> pos;
					cout << "位置是:" << Index(ST2, ST, pos)<> pos;
					cout << "位置是:" << Index(ST2, ST1, pos)<> select;
			switch (select)
			{
			case 0:
				cout << "---------------------------------\n";
				cout << "再选被插入的串:\n";
				cout << "1:第1个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 1:
					cout << "第几个字符之前插入:";
					cin >> pos;
					StrInsert(&ST, pos, ST1);		//在串S的第pos个字符之前插入串T 
					cout << "串0变为:"; StrPrint(ST);
					break;
				case 2:
					cout << "第几个字符之前插入:";
					cin >> pos;
					StrInsert(&ST, pos, ST2);		//在串S的第pos个字符之前插入串T 
					cout << "串0变为:"; StrPrint(ST);
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			case 1:
				cout << "---------------------------------\n";
				cout << "再选被插入的串:\n";
				cout << "0:第0个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 0:
					cout << "第几个字符之前插入:";
					cin >> pos;
					StrInsert(&ST1, pos, ST);		//在串S的第pos个字符之前插入串T 
					cout << "串1变为:"; StrPrint(ST1); cout << endl;
					break;
				case 2:
					cout << "第几个字符之前插入:";
					cin >> pos;
					StrInsert(&ST1, pos, ST2);		//在串S的第pos个字符之前插入串T 
					cout << "串1变为:"; StrPrint(ST1); cout << endl;
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			case 2:
				cout << "---------------------------------\n";
				cout << "再选被插入的串:\n";
				cout << "0:第0个		1:第1个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 0:
					cout << "第几个字符之前插入:";
					cin >> pos;
					StrInsert(&ST2, pos, ST);		//在串S的第pos个字符之前插入串T 
					cout << "串2变为:"; StrPrint(ST2); cout << endl;
					break;
				case 1:
					cout << "第几个字符之前插入:";
					cin >> pos;
					StrInsert(&ST2, pos, ST1);		//在串S的第pos个字符之前插入串T 
					cout << "串2变为:"; StrPrint(ST2); cout << endl;
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  12:
			cout << "---------------------------------\n";
			cout << "实现删除,先选第一个主串:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				cout << "请输入起点: ";
				cin >> pos;
				cout << "请输入长度: ";
				cin >> len;
				StrDelete(&ST, pos, len);		//从串S中删除第pos个字符起长度为len的子串 
				cout << "串0变为:"; StrPrint(ST); cout << endl;
				break;
			case 1:
				cout << "请输入起点: ";
				cin >> pos;
				cout << "请输入长度: ";
				cin >> len;
				StrDelete(&ST1, pos, len);		//从串S中删除第pos个字符起长度为len的子串 
				cout << "串1变为:"; StrPrint(ST1); cout << endl;
				break;
			case 2:
				cout << "请输入起点: ";
				cin >> pos;
				cout << "请输入长度: ";
				cin >> len;
				StrDelete(&ST2, pos, len);		//从串S中删除第pos个字符起长度为len的子串 
				cout << "串2变为:"; StrPrint(ST2); cout << endl;
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  13:
			cout << "---------------------------------\n";
			cout << "实现Replace,先选第一个主串:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				cout << "---------------------------------\n";
				cout << "再选被替换的串:\n";
				cout << "1:第1个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 1:
					Replace(ST, ST1, ST2);//用V替换主串S中出现的所有与T相等的不重叠的子串
					cout << "串0变为:"; StrPrint(ST); cout << endl;
					break;
				case 2:
					Replace(ST, ST2, ST1);//用V替换主串S中出现的所有与T相等的不重叠的子串
					cout << "串0变为:"; StrPrint(ST); cout << endl;
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			case 1:
				cout << "---------------------------------\n";
				cout << "再选第二个:\n";
				cout << "0:第0个		2:第2个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 0:
					Replace(ST1, ST, ST2);//用V替换主串S中出现的所有与T相等的不重叠的子串
					cout << "串1变为:"; StrPrint(ST1); cout << endl;
					break;
				case 2:
					Replace(ST1, ST2, ST);//用V替换主串S中出现的所有与T相等的不重叠的子串
					cout << "串1变为:"; StrPrint(ST1); cout << endl;
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			case 2:
				cout << "---------------------------------\n";
				cout << "再选第二个:\n";
				cout << "0:第0个		1:第1个 \n";
				cout << "---------------------------------\n";
				cin >> select;
				switch (select)
				{
				case 0:
					Replace(ST2, ST, ST1);//用V替换主串S中出现的所有与T相等的不重叠的子串
					cout << "串2变为:"; StrPrint(ST2); cout << endl;
					break;
				case 1:
					Replace(ST2, ST1, ST);//用V替换主串S中出现的所有与T相等的不重叠的子串
					cout << "串2变为:"; StrPrint(ST2); cout << endl;
					break;
				default: cout << "输入编号出错\n";
				}
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case  14:
			cout << "---------------------------------\n";
			cout << "请选择打印第几个串:\n";
			cout << "0:第0个		1:第1个		2:第2个 \n";
			cout << "---------------------------------\n";
			cin >> select;
			switch (select)
			{
			case 0:
				StrPrint(ST);		//输出字符串ST。
				break;
			case 1:
				StrPrint(ST1);		//输出字符串ST1。
				break;
			case 2:
				StrPrint(ST2);		//输出字符串ST2。
				break;
			default: cout << "输入编号出错\n";
			}
			break;
		case -1:cout << "退出\n"; break;
		default: cout << "输入编号出错\n";
		}
	} while (select != -1);
	cout << endl;
	cout << "\n20213002624李季鸿,终于搞出来了!!!" << endl;
	system("pause");
	return 0;
}

你可能感兴趣的:(c++,数据结构)