当前位置:网站首页>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;
	}
}
原网站

版权声明
本文为[Sy_ Faker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222259538172.html