当前位置:网站首页>PTA:7-87 集合的模拟实现(类模板)
PTA:7-87 集合的模拟实现(类模板)
2022-06-23 03:45:00 【Sy_Faker】
我们可以用一个类来模拟集合及集合运算,add运算用以实现集合元素的增加,delete运算用于实现集合元素的删除,find运算用以实现集合元素的查找,但是目前集合元素类型未知,可以是int、char、double等基本数据类型,也可以是String、Time、Student等对象类型,要求采用类模板实现集合及集合运算,包括集合元素的增加、删除和查找的等基本功能。
集合模板类MySet包括数据如下:
T data[100];//用数组来存放所有的集合元素,最多不超过100个元素
int count;//表示目前集合中有多少个元素
包括成员函数如下:
构造函数若干个,集合运算函数如下:
int addSet( T elem)
int deleSet(T elem)
int findElem(T elem)
其中,addSet向集合中添加一个元素,deleSet从集合中删除一个元素,findElem判断elem是否是集合成员,三个函数分别返回元素插入位置,删除位置和存在位置。
主函数有如下数据成员 :
MySet<\int>\ intSet;(反斜杠是转义字符,使用时去掉)
MySet<\double>\ douSet;
MySet<\string>\ strSet;
分别是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>
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;
}
}
边栏推荐
- 【LeetCode】23. 合并K个升序链表
- 静态查找表和静态查找表
- 怎样能在小程序中实现视频通话及互动直播功能?
- Pyspark, paid for data cleaning and uploading to the database
- 【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM
- photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
- [OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe
- Source code encryption of data encryption technology
- 靜態查找錶和靜態查找錶
- Navar's Treasure Book: the principle of getting rich without luck
猜你喜欢

MySQL data recovery (.Ibdata1, bin log)

JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology

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

第一批00后下场求职:不要误读他们的“不一样”

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

8 key indicators to measure technology debt in 2022

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

Twitter cooperates with Shopify to introduce merchant products into twitter shopping

浅析2022年物联网现状

Web page dynamic and static separation based on haproxy
随机推荐
怎样能在小程序中实现视频通话及互动直播功能?
怎么使用Shell脚本实现监测文件变化
P1347 排序(topo)
Form development mode
photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
Swiftui component encyclopedia creating animated 3D card scrolling effects using Scrollview and geometryreader
Bug STM32 interrupt (everyone knows)
pyspark,有偿询问数据清洗和上传到数据库的问题
【二叉树进阶】AVLTree - 平衡二叉搜索树
Tencent cloud tcapulusdb helps tmeland, the first virtual Music Carnival in China, and tens of thousands of people cross the new year together!
What is the APM tool skywalking
How to process large volume xlsx/csv/txt files?
Pytorch---Pytorch进行自定义Dataset
Questions about SQL statements
D overloading nested functions
Twitter与Shopify合作 将商家产品引入Twitter购物当中
Adobe international certification 𞓜 how IIT Madras brings efficiency and accessibility to scholars through Adobe e Acrobat
如何处理大体积 XLSX/CSV/TXT 文件?
Which insurance company is the most cost-effective for purchasing serious illness insurance?