顺序表示的串——顺序串2——串的基本操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_36669549/article/details/84678791

利用串的基础运算,对串进行赋值、比较、插入、删除、连接等操作。


【分析】


主要考察串的创建、定位、删除等操作。

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);


	}
}

运行结果:

猜你喜欢

转载自blog.csdn.net/baidu_36669549/article/details/84678791