当前位置:网站首页>PTA:7-86 集合的模拟实现(函数模板)
PTA:7-86 集合的模拟实现(函数模板)
2022-06-23 03:45:00 【Sy_Faker】
我们可以用一个数组来模拟集合,add运算用以实现集合元素的增加,delete运算用于实现集合元素的删除,find运算用以实现集合元素的查找,但是目前集合元素类型未知,可以是int、char、double等基本数据类型,也可以是String、Time、Student等对象类型,要求采用模板函数实现集合元素的增加、删除和查找功能。
三个模板函数如下:
int addSet(T * myset, T elem,int len)
int deleSet(T * myset, T elem, int len)
int findElem(T * myset, T elem, int len)
其中,addSet向集合中添加一个元素,deleSet从集合中删除一个元素,findElem判断elem是否是集合成员,三个函数分别返回元素插入位置,删除位置和存在位置。
主函数有如下数据成员 :
int intSet[100]
double douSet[100]
String StrSet[100] 分别是int类型、double类型、String的数组集合。
int intLen, douLen, strLen分别是int类型、double类型、String的数组集合的长度
完成上述函数模板和主函数,主函数根据输入的信息,建立初始的空集合,调用三个模板函数分别对intSet、douSet和StrSet执行相应的操作,并输出对应的集合信息。
输入格式:
每一行为一个集合操作,每行的第一个数字为集合元素类型,1为整型元素,2为浮点型元素,3为String类型,第二个数字为集合操作类型,1为插入,2为删除,3为查找,第三个为集合元素,集合元素类型视第一个数字给定的集合元素类型而定。输入0时标志输入结束。
输出格式:
输出当前操作的执行位置(插入位置、删除位置和存在位置)
删除操作时,如果元素X不存在,输出“X is not exist!”。
插入操作时,如果集合已满,输出“Full Set.”若元素已存在,输出“X is already exist!”
查找操作时,如果找不到元素,输出“X is not exist!”。
输入:
1 1 1
1 1 2
1 3 1
1 2 1
1 2 3
1 3 1
2 1 1.1
2 1 2.2
2 1 3.3
2 3 1.1
2 2 2.2
2 2 2.2
3 1 abc
3 1 bcd
3 3 abc
3 2 abc
3 3 abc
0
输出:
0
1
0
0
3 is not exist!
1 is not exist!
0
1
2
0
1
2.2 is not exist!
0
1
0
0
abc is not exist!
#include<iostream>
using namespace std;
template<class T>
int findElem(T * myset, T elem,int len)
{
int i;
for(i=0;i<len;i++)
{
if(myset[i]==elem)
return i;
}
return -1;
}
template<class T>
int addSet(T * myset, T elem,int len)
{
if(len==100)//集合满了
{
return 101;
}
if(findElem(myset,elem,len)>=0)//元素已经存在
{
return -1;
}
// int i=0;
// for(int j=0;j<len;j++)
// {
// if(myset[j]<elem)
// i=j;
// }
// for(int j=len;j>i;j--)
// {
// myset[j]=myset[j-1];
// }
myset[len]=elem;
return len;
}
template<class T>
int deleSet(T * myset, T elem,int len)
{
if(findElem(myset,elem,len)<0)//元素不存在
{
return -1;
}
int i=findElem(myset,elem,len);
for(int j=i;j<len-1;j++)
{
myset[j]=myset[j+1];
}
return i;
}
int main()
{
int intSet[100];
double douSet[100];
string strSet[100];
int intLen=0, douLen=0, strLen=0;
int type1,type2,elementi,k;
double elementd;
string elements;
cin>>type1;
while(type1!=0)
{
switch(type1)
{
case 1:
cin>>type2>>elementi;
switch(type2)
{
case 1:
k=addSet(intSet,elementi,intLen);
if(k==101)
cout<<"Full Set."<<endl;
else if(k==-1)
cout<<elementi<<" is already exist!"<<endl;
else
{
cout<<k<<endl;
intLen++;
}
break;
case 2:
k=deleSet(intSet,elementi,intLen);
if(k==-1)
cout<<elementi<<" is not exist!"<<endl;
else
{
cout<<k<<endl;
intLen--;
}
break;
case 3:
k=findElem(intSet,elementi,intLen);
if(k<0)
cout<<elementi<<" is not exist!"<<endl;
else
cout<<k<<endl;
break;
}
break;
case 2:
cin>>type2>>elementd;
switch(type2)
{
case 1:
k=addSet(douSet,elementd,douLen);
if(k==101)
cout<<"Full Set."<<endl;
else if(k==-1)
cout<<elementd<<" is already exist!"<<endl;
else
{
cout<<k<<endl;
douLen++;
}
break;
case 2:
k=deleSet(douSet,elementd,douLen);
if(k==-1)
cout<<elementd<<" is not exist!"<<endl;
else
{
cout<<k<<endl;
douLen--;
}
break;
case 3:
k=findElem(douSet,elementd,douLen);
if(k<0)
cout<<elementd<<" is not exist!"<<endl;
else
cout<<k<<endl;
break;
}
break;
case 3:
cin>>type2>>elements;
switch(type2)
{
case 1:
k=addSet(strSet,elements,strLen);
if(k==101)
cout<<"Full Set."<<endl;
else if(k==-1)
cout<<elements<<" is already exist!"<<endl;
else
{
cout<<k<<endl;
strLen++;
}
break;
case 2:
k=deleSet(strSet,elements,strLen);
if(k==-1)
cout<<elements<<" is not exist!"<<endl;
else
{
cout<<k<<endl;
strLen--;
}
break;
case 3:
k=findElem(strSet,elements,strLen);
if(k<0)
cout<<elements<<" is not exist!"<<endl;
else
cout<<k<<endl;
break;
}
}
cin>>type1;
}
}
边栏推荐
- Bug STM32 interrupt (everyone knows)
- 京东云分布式数据库StarDB荣获中国信通院 “稳定性实践先锋”
- mysql优化,sql执行非常卡顿,不改变sql结构达到10秒内结束
- Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
- Three ways to export excel from pages
- TRTC setaudioroute invalid problem
- [tcapulusdb knowledge base] [list table] example code for deleting the data at the specified location in the list
- 【二叉树进阶】AVLTree - 平衡二叉搜索树
- Two ways to improve the writing efficiency of hard disk storage data
- 直接插入排序
猜你喜欢

背景彩带动画插件ribbon.js
![[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe](/img/cd/7f896a0f05523a07b5dd04a8737879.png)
[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe

两招提升硬盘存储数据的写入效率

高效的远程办公经验 | 社区征文
![[greed] leetcode991 Broken Calculator](/img/6e/ce552b55899c6e8d3c37f524f99f82.png)
[greed] leetcode991 Broken Calculator

Insert sort directly

粒子动画背景登录页面particles.js

炫酷鼠标跟随动画js插件5种

Twitter与Shopify合作 将商家产品引入Twitter购物当中

mysql如何删除表的一行数据
随机推荐
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Goframe framework: quick creation of static file download web service
Select sort method
冒泡排序法
关于sql语句的问题
Preface
【LeetCode】179. Maximum number
虫子 日期类 下 太子语言
JS array de duplication, removing the same value
linux下的开源数据库是什么
D overloading nested functions
[two points] leetcode1011 Capacity To Ship Packages Within D Days
MySQL optimization, the SQL execution is very stuck, and the SQL structure will not be changed until it ends in 10 seconds
虫子 日期类 上 太子语言
Black horse PostgreSQL, why is it black in the end
选择排序法
Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
Questions about SQL statements
仿360桌面悬浮球插件
静态查找表和静态查找表