当前位置:网站首页>PTA: Simulation Implementation of 7-86 set (function template)
PTA: Simulation Implementation of 7-86 set (function template)
2022-06-23 04:26:00 【Sy_ Faker】
We can use an array to simulate a collection ,add The operation is used to realize the addition of set elements ,delete The operation is used to delete the set elements ,find Operation is used to realize the search of set elements , But the collection element type is unknown at present , It can be int、char、double Basic data type , It can also be String、Time、Student Object type, etc , Template functions are required to increase the set elements 、 Delete and find functions .
The three template functions are as follows :
int addSet(T * myset, T elem,int len)
int deleSet(T * myset, T elem, int len)
int findElem(T * myset, T elem, int len)
among ,addSet Add an element to the collection ,deleSet Remove an element from the collection ,findElem Judge elem Whether it is a collection member , The three functions return the element insertion position respectively , Delete location and existing location .
The main function has the following data members :
int intSet[100]
double douSet[100]
String StrSet[100] Namely int type 、double type 、String Array set of .
int intLen, douLen, strLen Namely int type 、double type 、String The length of the array set of
Complete the above function template and main function , The main function is based on the input information , Create an initial empty set , Call three template functions to intSet、douSet and StrSet Perform the corresponding operation , And output the corresponding set information .
Input format :
Each behavior is a collection operation , The first number in each row is the collection element type ,1 Is an integer element ,2 Is a floating point element ,3 by String type , The second number is the collection operation type ,1 Insert for ,2 To delete ,3 To find , The third is the set element , The set element type depends on the set element type given by the first number . Input 0 Mark the end of input .
Output format :
Output the execution position of the current operation ( Insertion position 、 Delete location and existing location )
Delete operation , If the element X non-existent , Output “X is not exist!”.
When inserting , If the set is full , Output “Full Set.” If the element already exists , Output “X is already exist!”
There was an error in the lookup operation , If you can't find an element , Output “X is not exist!”.
Input :
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
Output :
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)// The collection is full
{
return 101;
}
if(findElem(myset,elem,len)>=0)// Elements already exist
{
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)// Elements don't exist
{
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;
}
}
边栏推荐
- What is the difference between redistemplate and CacheManager operation redis
- The new version of Kali switches the highest account
- [Zeng shuge's laser slam notes] gmapping filter based slam
- 关于sql语句的问题
- 如何处理大体积 XLSX/CSV/TXT 文件?
- 背景彩带动画插件ribbon.js
- Questions about SQL statements
- Navar's Treasure Book: the principle of getting rich without luck
- mysql,字段问题
- 12 excellent practices of wireless network security
猜你喜欢

移动端城市列表排序js插件vercitylist.js

Full analysis of embedded software testing tool tpt18 update

【一起上水硕系列】Day Three - preview4

Deploying Apache pulsar on kubesphere

城链科技董事长肖金伟:践行数据经济系国家战略,引领数字时代新消费发展!

Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!

node+express如何操作cookie

背景彩带动画插件ribbon.js

在word里,如何让页码从指定页开始编号

What is metadata
随机推荐
自媒体时代的贤内助——AI 视频云
Twitter cooperates with Shopify to introduce merchant products into twitter shopping
How to realize data transaction
2022年的软件开发:首席信息官应该知道的五个现实
[tcapulusdb knowledge base] [list table] example code for deleting the data at the specified location in the list
[two points] leetcode1011 Capacity To Ship Packages Within D Days
AI video cloud vs narrowband HD, who is the favorite in the video Era
PTA:7-69 数据的间距问题
[leetcode] flip linked list II
Source code encryption of data encryption technology
8 key indicators to measure technology debt in 2022
华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
城链科技董事长肖金伟:践行数据经济系国家战略,引领数字时代新消费发展!
Google Earth engine (GEE) - long time series monthly VCI data extraction, analysis and area calculation (Mexico as an example)
Weekly Postgres world news 2022w02
bubble sort
IDEA-导入模块
How e-commerce makes use of small programs
Zhongang Mining: the demand for fluorite in the new energy and new material industry chain has increased greatly
静态查找表和静态查找表