7-1 集合的模拟实现(类模板) (40分)

7-1 集合的模拟实现(类模板) (40分)
我们可以用一个类来模拟集合及集合运算,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
#include
using namespace std;
template<class T>
class MySet
{
	T data[100];
	int count;
	public:
		int addset(T elem)
		{ 
			int x=count,i,flag=0;
			if(count==100) 
			{cout<<"Full Set."<<endl;
				return 111;
			}
			for(i=0;i<count;i++)
			{
				if(elem==data[i]) 
				{
					cout<<elem<<" is already exist!"<<endl;
					return 111;
				}
			}
			data[count++]=elem;
			return x;
		}
		int deleset(T elem)
		{
			int y=count,i,flag=0,j;
		    for(i=0;i<count;i++)
		    {
		    	if(data[i]==elem)
		    	{
		    		flag=1;
		    		j=i;break;
				}
			}
			if(flag==1){
			for(i=j+1;i<count;i++)
			{
				data[i-1]=data[i];
			}
			count--;return j;}
			cout<<elem<<" is not exist!"<<endl;
			return 111;
		} 
		int findelem(T elem)
		{
			int x,i;
			for(i=0;i<count;i++)
			{
				if(elem==data[i])
				{
					return i;
				}
			}
			cout<<elem<<" is not exist!"<<endl;
			return 111;
		}
		MySet(){count=0;}
};
int main()
{
	MySet<int> intset;
	MySet<double> douset;
	MySet<string> strset;
	int type,oper,num1;
	double num2;string num3;
	while(1)
	{
		cin>>type;
		if(type==0) break;
		cin>>oper;
		switch(type)
		{
			case 1:
				cin>>num1;
				if(oper==1)
				{
					int x=intset.addset(num1);
					if(x!=111)
					cout<<x<<endl;
				}
				if(oper==2)
				{
					int x=intset.deleset(num1);
					if(x!=111)
					cout<<x<<endl;
				}
				if(oper==3)
				{
					int x=intset.findelem(num1);
					if(x!=111)
					cout<<x<<endl;
				}
				break;
			case 2:
				cin>>num2;
				if(oper==1)
				{
					int x=douset.addset(num2);
					if(x!=111)
					cout<<x<<endl;
				}
				if(oper==2)
				{
					int x=douset.deleset(num2);
					if(x!=111)
					cout<<x<<endl;
				}
				if(oper==3)
				{
					int x=douset.findelem(num2);
					if(x!=111)
					cout<<x<<endl;
				}
				break;
			case 3:
				cin>>num3;
				if(oper==1)
				{
					int x=strset.addset(num3);
					if(x!=111)
					cout<<x<<endl;
				}
				if(oper==2)
				{
					int x=strset.deleset(num3);
					if(x!=111)
					cout<<x<<endl;
				}
				if(oper==3)
				{
					int x=strset.findelem(num3);
					if(x!=111)
					cout<<x<<endl;
				}
				break;
		}
	}
	return 0;
}

总结:题目虽然长,输入输出也长,但是先要整理明白题目到底要实现什么。
switch语句中的break容易忘记,忘记,则会输入一行输出两个结果
“ is not exist”在findelem函数中忘记了,导致输出有问题
关于如何在考试的有限时间内做出这样的题:
1 类内函数:思路清晰,操作一致,每种情况都要考虑
正常输出就return位置,非正常就return111并直接输出
2 main 函数:如果是双重选择,可以用一个switch语句,注意要写break 因为只要判断return的是111还是其他值,所以所有分支只要创建一个新的变量x,并且只有当x不是111时才输出

你可能感兴趣的:(7-1 集合的模拟实现(类模板) (40分))