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

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