栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 实现串及相关操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 实现串及相关操作

#include
#include
using namespace std;
#define Maxsize 100 //串的最大长度
typedef struct {
	string list[Maxsize];
	int listsize;
}Sqlist;

//显示操作菜单
void showmenu()
{
	cout<<"位置对应:0 1 2 3 4 5 . 。 。 。(输入) "<> a->listsize;
	cout << endl;
	cout << "请输入串的内容:" << endl;
	for (int i = 0;i < a->listsize;i++)
	{
		cin >> a->list[i];
	}
	cout << "添加成功!" << endl;
	system("pause");
	system("cls");
}
//显示串
void showlist(Sqlist* a)
{
	cout << "当前串为:" << endl;
	for (int i = 0;i < a->listsize;i++)
	{
		cout << a->list[i] << endl;
	}
	system("pause");
	system("cls");
}
//修改串
void change(Sqlist* a) {
	cout << "请输入修改的起始位置:";
	int e1;
	cin >> e1;
	cout << endl;
	cout << "请输入修改的终止位置:";
	int e2;
	cin >> e2;
	cout << endl;
	cout<<"请输入修改的内容:";
	int temp;
	temp=e1;
	for (int i = 0;i < (e2-e1+1);i++)
	{
		cin>>a->list[temp++];
	}
	cout << "修改成功!" << endl;
	system("pause");
	system("cls");
}
//插入一个串
void insert(Sqlist* a)
{
	int e1;
	cout << "请输入插入的位置:";
	cin >> e1;
	string list[Maxsize];
	int size;
	cout << "请输入新串的长度:";
	cin >> size;
	cout << endl;
	cout << "请输入串的内容"<> list[j];
	}
	//将原串后移
	int temp;
	temp = a->listsize;
	int temb;
	temb=temp-e1-1;
	cout<<"串后移"<list[(temp) - 1 + size] = a->list[(temp) - 1];
		temp--;
	}
	//将新串插入
	int tale;
	tale=e1;
	cout<<"插入新串"<list[++tale] = list[k];//插入原串
	}
	a->listsize = a->listsize + size;
	cout << "修改成功!";
	system("pause");
	system("cls");
}
//删除
void listdele(Sqlist* a)
{
	int e1, e2;
	cout << "请输入初始位置:";
	cin >> e1;
	cout << endl;
	cout << "请输入终止位置";
	cin >> e2;
	//删除串
	for (int j = 0;j < e2 - e1 + 1;j++)
	{
		a->list[e1] = "0";
	}
	//字符串前移
	int size;
	size = e2 - e1 + 1;
	int temp;
	temp = e2;
	for (int i = 0;i < (a->listsize - e2 - 1);i++) //循环执行次数,即几个元素前移
	{
		a->list[temp + 1 - size] = a->list[temp + 1];
		temp++;
	}
	a->listsize = a->listsize - size;
	cout << "修改成功!" << endl;
	system("pause");
	system("cls");
}
//判断是否为子串
void iszichuan(Sqlist* a)//可以使用KMP算法来降低时间成本,但此处串的长度比较小,采用BF算法
{
	int j = 0;
	int k = 0;
	int n1;
	cout << "请输入子串的长度:" << endl;
	cin >> n1;
	string listx[Maxsize];
	cout << "请输入子串的内容:" << endl;
	for (int i = 0;i < n1;i++)
	{
		cin >> listx[i];
	}
	while (j < a->listsize && k < n1)  //遍历子串和主串
	{
		if (a->list[j] == listx[k])
		{
			++j; //匹配成功,继续往下遍历
			++k;
		}
		else
		{
			j = j - k + 1;   //主串和子串均回溯
			k = 0;
		}
	}
	if (k == n1)
	{
		cout<<"是子串!"<> select;
		switch (select)
		{
		case 1:
			createlist(&a);
			break;
		case 2:
			showlist(&a);
			break;
		case 3:
			change(&a);
			break;
		case 4:
			insert(&a);
			break;
		case 5:
			listdele(&a);
			break;
		case 6:
			iszichuan(&a);
			break;
		case 7:
			length(a);
			break;

		case 0:
			cout << "欢迎您的下次使用!" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913608.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号