当前位置:网站首页>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;
}
}
边栏推荐
- [从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
- [pycharm] ide Eval resetter
- MySQL optimization, the SQL execution is very stuck, and the SQL structure will not be changed until it ends in 10 seconds
- 什么是元数据
- QMainWindow
- 靜態查找錶和靜態查找錶
- 移动端城市列表排序js插件vercitylist.js
- Compilation, installation and global configuration section description of haproxy
- flutter系列之:flutter中的Wrap
- Pyspark, paid for data cleaning and uploading to the database
猜你喜欢

Compilation, installation and global configuration section description of haproxy

Review the SQL row column conversion, and the performance has been improved

AI 视频云 VS 窄带高清,谁是视频时代的宠儿

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

bubble sort

The first batch of job hunting after 00: don't misread their "different"

How to process large volume xlsx/csv/txt files?

Centos7 installing MySQL and configuring InnoDB_ ruby

Zhongang Mining: the demand for fluorite in the new energy and new material industry chain has increased greatly

Implementation of VGA protocol based on FPGA
随机推荐
It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!
【二叉樹進階】AVLTree - 平衡二叉搜索樹
最新编程语言排行榜
Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
[tcapulusdb knowledge base] [list table] sample code of asynchronous scanning data
IDEA-导入模块
Mysql, field problem
What is the difference between redistemplate and CacheManager operation redis
[leetcode] sum of two numbers II
What is the digital "true" twin? At last someone made it clear!
Getting started with tensorflow
Google Earth engine (GEE) - long time series monthly VCI data extraction, analysis and area calculation (Mexico as an example)
【LeetCode】179. Maximum number
redisTemplate和cacheManager操作redis有什么不同
Centos7 installing MySQL and configuring InnoDB_ ruby
redis 精讲系列介绍八 - 淘汰策略
炫酷鼠标跟随动画js插件5种
Avltree - arbre de recherche binaire équilibré
Code refactoring Guide
Half search method