当前位置:网站首页>PTA: Simulation Implementation of 7-87 set (class template)
PTA: Simulation Implementation of 7-87 set (class template)
2022-06-23 04:27:00 【Sy_ Faker】
We can use a class to simulate sets and set operations ,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 , It is required to use class templates to implement sets and set operations , Including the addition of set elements 、 Basic functions such as deleting and searching .
Collection template class MySet The data included are as follows :
T data[100];// Use an array to store all collection elements , Not more than 100 Elements
int count;// Indicates how many elements are in the current collection
The following member functions are included :
Constructor several , The set operation functions are as follows :
int addSet( T elem)
int deleSet(T elem)
int findElem(T elem)
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 :
MySet<\int>\ intSet;( A backslash is an escape character , Remove when using )
MySet<\double>\ douSet;
MySet<\string>\ strSet;
Namely int type 、double type 、String Set .
Complete the above class template and main function , The main function is based on the input information , Create the initial three different types of empty collection objects , Call member 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>
class MySet
{
T data[100];
int count;
public:
MySet()
{
count=0;
}
int find(T elem)
{
for(int i=0;i<count;i++)
{
if(data[i]==elem)
return i;
}
return -1;
}
void findElem(T elem)
{
for(int i=0;i<count;i++)
{
if(data[i]==elem)
{
cout<<i<<endl;
return;
}
}
cout<<elem<<" is not exist!"<<endl;
}
void addSet(T elem)
{
if(count==100)
{
cout<<"Full Set."<<endl;
return;
}
if(find(elem)>=0)
{
cout<<elem<<" is already exist!"<<endl;
return;
}
int k=count;
data[count++]=elem;
cout<<k<<endl;
}
void deleSet(T elem)
{
int k=find(elem);
if(k<0)
{
cout<<elem<<" is not exist!"<<endl;
return;
}
for(int i=k;i<count-1;i++)
{
data[i]=data[i+1];
}
cout<<k<<endl;
count--;
}
};
int main()
{
MySet<int> intSet;
MySet<double> douSet;
MySet<string> strSet;
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:
intSet.addSet(elementi);
break;
case 2:
intSet.deleSet(elementi);
break;
case 3:
intSet.findElem(elementi);
break;
}
break;
case 2:
cin>>type2>>elementd;
switch(type2)
{
case 1:
douSet.addSet(elementd);
break;
case 2:
douSet.deleSet(elementd);
break;
case 3:
douSet.findElem(elementd);
break;
}
break;
case 3:
cin>>type2>>elements;
switch(type2)
{
case 1:
strSet.addSet(elements);
break;
case 2:
strSet.deleSet(elements);
break;
case 3:
strSet.findElem(elements);
break;
}
}
cin>>type1;
}
}
边栏推荐
- 深度学习 简介
- [greed] leetcode991 Broken Calculator
- Pytorch---Pytorch进行自定义Dataset
- mysql存储引擎之Myisam和Innodb的区别
- 自媒体时代的贤内助——AI 视频云
- redisTemplate和cacheManager操作redis有什么不同
- IDEA-导入模块
- [leetcode] flip linked list II
- [advanced binary tree] AVLTree - balanced binary search tree
- [two points] leetcode1011 Capacity To Ship Packages Within D Days
猜你喜欢

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

浅析2022年物联网现状

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

AI video cloud vs narrowband HD, who is the favorite in the video Era

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

MySQL common instructions

It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!

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

Software project management 8.4 Software project quality plan
![[two points] leetcode1011 Capacity To Ship Packages Within D Days](/img/fd/c6f31a44ebaf41bd5ab2a342f10d06.png)
[two points] leetcode1011 Capacity To Ship Packages Within D Days
随机推荐
众昂矿业:新能源新材料产业链对萤石需求大增
MySQL data recovery (.Ibdata1, bin log)
Svg+js smart home monitoring grid layout
QMainWindow
[advanced binary tree] AVLTree - balanced binary search tree
移动端城市列表排序js插件vercitylist.js
城链科技董事长肖金伟:践行数据经济系国家战略,引领数字时代新消费发展!
【深度学习】深度学习推理框架 TensorRT MNN OpenVINO ONNXRuntime
What is the digital "true" twin? At last someone made it clear!
mysql优化,sql执行非常卡顿,不改变sql结构达到10秒内结束
[tcapulusdb knowledge base] [list table] example code of batch deleting data at specified location in the list
Inscription of lougu brush
svg d3.js生成tree树状图
After Huawei online battle service players quickly match, different players receive different lists of players in the same room
Source code encryption of data encryption technology
Adobe international certification 𞓜 how IIT Madras brings efficiency and accessibility to scholars through Adobe e Acrobat
Redis启动有问题
Online text filter less than specified length tool
The new version of Kali switches the highest account
linux下的开源数据库是什么