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

7-3 集合的模拟实现(类模板) (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<iostream>
#include<string>
using namespace std;
template<class T>
class MySet
{
private:
	T data[100];
	int count;
public:
	MySet()
	{
		count = 0;
	}
	int addSet(T elem)
	{
		if (count == 100)
		{
			cout << "Full Set." << endl;
			return -1;
		}
		
		else
		{
			for (int i = 0; i < count; i++)
			{
				if (data[i] == elem)
				{
					cout <<elem<< " is already exist!" << endl;
					return -1;
				}
			}
			
			data[count] = elem;
			count++;
			return count-1;
			
		}
	}
	int deleSet(T elem)
	{
		for (int i = 0; i < count; i++)
		{
			if (data[i] == elem)
			{
				for (int j = i + 1; j < count; j++)
				{
					data[j - 1] = data[j];
					
				}
				count--;
				return i;
			}
		}
		cout <<elem<< " is not exist!" << endl;
		return -1;
	}
	int findElem(T elem)
	{
		for (int i = 0; i < count; i++)
		{
			if (data[i] == elem)
			{
				return i;
			}
		}
		cout <<elem<< " is not exist!" << endl;
		return -1;
	}
};
int main()
{
	MySet<int>intSet; 
	MySet<double> douSet;
	MySet<string> strSet;
	int flag, task;
	
	string a;
	double b;
	int c;
	while (1)
	{
		cin >> flag;
		if (flag == 0) break;
		cin >> task;
		switch (flag)
		{
		case 1:
			cin >> c;
			if (task == 1)
			{
				int temp = intSet.addSet(c);
				if(temp!=-1)
				cout << temp << endl;
			}
			if (task == 2)
			{
				int temp = intSet.deleSet(c);
				if ( temp!= -1)
					cout << temp << endl;
			}
			if (task == 3)
			{
				int temp = intSet.findElem(c);
				if (temp != -1)
				{
					cout << temp<<endl;
				}
			}
			break;
		case 2:
			cin >> b;
			if (task == 1)
			{
				int temp = douSet.addSet(b);
				if ( temp!= -1)
					cout <<temp << endl;
			}
			if (task == 2)
			{
				int temp = douSet.deleSet(b);
				if( temp != -1)
					cout << temp << endl;
			}
			if (task == 3)
			{
				int temp = douSet.findElem(b);
				if ( temp!= -1)
				{
					cout << temp<<endl;
				}
			}
			break;
		case 3:
			cin >> a;
			if (task == 1)
			{
				int temp = strSet.addSet(a);
				if ( temp!= -1)
					cout << temp << endl;
			}
			if (task == 2)
			{
				int temp = strSet.deleSet(a);
				if (temp!= -1)
					cout << temp << endl;
			}
			if (task == 3)
			{
				int temp = strSet.findElem(a);
				if ( temp!= -1)
				{
					cout << temp<<endl;
				}
			}
			break;
		}

	}
	return 0;
}
posted @ 2020-05-30 23:03  与谁  阅读(277)  评论(0编辑  收藏  举报