当前位置:网站首页>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;
}
}
边栏推荐
- 京东云分布式数据库StarDB荣获中国信通院 “稳定性实践先锋”
- Mysql, field problem
- PTA:7-69 数据的间距问题
- Efficient remote office experience | community essay solicitation
- Adobe international certification 𞓜 how IIT Madras brings efficiency and accessibility to scholars through Adobe e Acrobat
- 【一起上水硕系列】Day Three - preview4
- mysql能不能在linux中使用
- 会话和守护进程
- Svg+js smart home monitoring grid layout
- 在线JSON转CSharp(C#)Class工具
猜你喜欢

Halcon glue line detection - template matching, pose transformation, glue width, glue continuity detection

摆烂LuoGu刷题记

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

A summary of PostgreSQL data types. All the people are here

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

给你的AppImage创建桌面快捷方式

Inscription of lougu brush

Latest programming language rankings

Pytoch --- pytoch customizes the dataset

基于FPGA的VGA协议实现
随机推荐
A summary of PostgreSQL data types. All the people are here
PTA:7-85 数据的间距问题(重载+函数模板)
Centos7 installing MySQL and configuring InnoDB_ ruby
Inscription of lougu brush
JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
12 excellent practices of wireless network security
Source code encryption of data encryption technology
How e-commerce makes use of small programs
photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
【深度学习】深度学习推理框架 TensorRT MNN OpenVINO ONNXRuntime
AI video cloud: a good wife in the era of we media
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
怎么用好MySQL索引
移动端城市列表排序js插件vercitylist.js
Getting started with tensorflow
leetcode 91. Decode Ways 解码方法(中等)
[leetcode] sum of two numbers II
[tcapulusdb knowledge base] [list table] example code of batch deleting data at specified location in the list
Compilation, installation and global configuration section description of haproxy