串的基本操作(KMP算法实现)

#include 
#include 
using namespace std;

void StrAssign(char *T)
{
	char ch;
	int i=1;
	cout<<"Please enter a string.0 is exit."<>ch&&ch!='0') {
		T[i++]=ch;
	}
	T[0]=i-1+'0';
	cout<<"Setting successfully!"<T[i]) {
			flag = 1;
			break;
		}
		else if (S[i]str[0]-'0') {
		cout<<"Position is illegal."<v[0]-'0') {
		return i-(v[0]-'0');
	}
	return 0;
}

int PrintAll(char *str)
{
	for (int i=1;i<=str[0]-'0';i++) {
		cout<0) {//负数的逻辑值为真,所以需要加上大于小于号
		flag = 1;
	}
	else if (len_cmp<0) {
		flag = -1;
	}
	//cout << flag << endl;
	len_cmp = abs(len_cmp);
	while (i0) {//如果v的长度大于t的长度,需要将主串s向后移动
			for (int j = s[0] - '0'; j >= pos; j--) {
				s[j + len_cmp] = s[j];
			}//move
			for (int j = pos, k = 1; k <= v[0] - '0';k++,j++) {
				s[j] = v[k];
			}
			s[0] = (s[0] - '0' + len_cmp) + '0';//修改主串长短
		}
		else if (flag==0){
			for (int j = pos, k = 1; k <= v[0] - '0';k++,j++) {
				s[j] = v[k];
			}
		}
		i = pos + (v[0] - '0');
		//cout << pos <<" "<< i << endl;
	}
	cout << "Substitute succeed." << endl;
	PrintAll(s);
	return 0;
	/*
	测试数据
	1
	1
	a0
	1
	0
	abababab0
	10
	c0

	1
	1
	ab0
	1
	0
	abababab0
	10
	c0

	1
	1
	ab0
	1
	0
	abababab0
	10
	abc0
	*/
}

int StrInsert(char *s,int pos,char *v)
{
	
	int v_len = v[0] - '0';
	for (int i = s[0] - '0'; i >= pos;i--) {
		s[i + v_len] = s[i];
	}
	for (int i = pos,j=1; j <= v_len;j++,i++) {
		s[i] = v[j];
	}
	s[0] = s[0] - '0' + v_len + '0';
	cout << "Insert successfully!" << endl;
	PrintAll(s);
	return 0;
}

int StrDelete(char *s,int pos,int len)
{
	if (pos<1||pos>s[0]-'0') {
		cout << "The position is illegal." << endl;
		return -1;
	}
	else if (len>s[0]-'0') {
		cout << "The length is too long." << endl;
		return -2;
	}
	for (int i = pos + len; i <= s[0] - '-';i++)
	{
		s[i-len] = s[i];
	}
	s[0] = s[0] - '0' - len + '0';
	cout << "Delete successfully!" << endl;
	PrintAll(s);
	return 0;
}

int main()
{
	cout<<"Please enter what you want to do.Zero is exit."<>n&&n) {
		switch (n) {
			case 1:
				cout << "Which do you want to set? T or S? Please enter the 1 or 0 to represent the diffirent string." << endl;
				cin >> num;
				if (num) {
					StrAssign(T);
				}
				else {
					StrAssign(S);
				}
				break;
			case 2:
				StrAssign(S);
				StrCopy(T,S);
				break;
			case 3:
				cout<<"The 1 represent the string T and the 0 represent the string S. Which string would you like to judge?"<>num;
				if (num) {
					StrEmpty(T);
				}
				else {
					StrEmpty(S);
				}
				break;
			case 4:
				StrCompare(S,T);
				break;
			case 5:
				cout<<"The 1 represent the string T and the 0 represent the string S. Which length would you like to print?"<>num;
				if (num) {
					cout<>num;
				if (num) {
					ClearString(T);
				}
				else {
					ClearString(S);
				}
				break;
			case 7:
				cout<<"Please enter a string for connect."<> num;
				cout<<"Please enter the position and length."<>pos>>len;
				if (num) {
					SubString(sub, T, pos, len);
				}
				else {
					SubString(sub, S, pos, len);
				}
				break;
			case 9:
				StrAssign(V);
				cout<<"Where do you want to start?"<> num;
				cout<> num;
				StrInsert(S, num, V);
				break;
			case 12:
				cout << "Where do you want to start? How long do you want to delete?" << endl;
				cin >> pos >> num;
				StrDelete(S,pos,num);
				break;
			case 13:
				cout << "Which string do you want to see? The string T or the string S? They are represent the 1 and 0." << endl;
				cin >> num;
				if (num) {
					PrintAll(T);
				}
				else {
					PrintAll(S);
				}
				break;
		}
	}
	return 0;
}
/*
测试数据
2
xiang0
7
yuan0
14
0
*/

 

转载于:https://www.cnblogs.com/xyqxyq/p/10211372.html

你可能感兴趣的:(串的基本操作(KMP算法实现))