zoukankan      html  css  js  c++  java
  • 集合的模拟实现(类模板)

    题目:

    我们可以用一个类来模拟集合及集合运算,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 <typename T>
    class MySet {
    		T data[100];
    		int count=0;
    	public:
    		//template <typename T>
    		int addSet(T elem) {
    			int exit_flag=0;
    			if(count>=100)
    				//cout<<"Full Set."<<endl;
    				return -1;
    			for(int i=0; i<count; i++) {
    				if(data[i]==elem) {
    					exit_flag=1;
    				}
    			}
    			if(exit_flag)
    				return -2;
    			 
    			else {
    				data[count]=elem;
    				//cout<<count<<endl;
    				count++;
    				return count-1;
    			}
    		}
    		//template <typename T>
    		int deleSet(T elem) {
    			int exit_flag=0,loca;
    			for(int i=0; i<count; i++) {
    				if(data[i]==elem) {
    					loca=i;
    					exit_flag=1;
    				}
    			}
    			if(!exit_flag) {
    				return -1;
    			}
    			for(int i=loca; i<count; i++)
    				data[i]=data[i+1];
    			return loca;
    		}
    		//template <typename T>
    		int findElem(T elem) {
    			int exit_flag=0,loca;
    			for(int i=0; i<count; i++) {
    				if(data[i]==elem) {
    					exit_flag=1;
    					loca=i;
    					break;
    				}
    			}
    			if(!exit_flag)
    				return -1;
    			return loca;
    		}
    };
    int main() {
    	MySet <int> intSet;
    	MySet <double> douSet;
    	MySet <string> strSet;
    	int flag,i1,i2,temp;
    	double d1;
    	string s1;
    	for(;;) {
    		cin>>flag;
    		if(!flag)
    			return 0;
    		else if(flag==1) {
    			cin>>i1>>i2;//
    			if(i1==1) {
    				temp=intSet.addSet(i2);//
    				if(temp==-1)
    					cout<<"Full Set."<<endl;
    				else if(temp==-2)
    					cout<<i2<<" is already exist!"<<endl;
    				else
    					cout<<temp<<endl;
    			} else if(i1==2) {
    				temp=intSet.deleSet(i2);//
    				if(temp==-1)
    					cout<<i2<<" is not exist!"<<endl;//
    				else
    					cout<<temp<<endl;;
    			} else if(i1==3) {
    				temp=intSet.findElem(i2);//
    				if(temp==-1)
    					cout<<i2<<" is not exist!"<<endl;//
    				else
    					cout<<temp<<endl;
    			}
    		} else if(flag==2) {
    			cin>>i1>>d1;//
    			if(i1==1) {
    				temp=douSet.addSet(d1);//
    				if(temp==-1)
    					cout<<"Full Set."<<endl;
    				else if(temp==-2)
    					cout<<d1<<" is already exist!"<<endl;
    				else
    					cout<<temp<<endl;
    			} else if(i1==2) {
    				temp=douSet.deleSet(d1);//
    				if(temp==-1)
    					cout<<d1<<" is not exist!"<<endl;//
    				else
    					cout<<temp<<endl;;
    			} else if(i1==3) {
    				temp=douSet.findElem(d1);//
    				if(temp==-1)
    					cout<<d1<<" is not exist!"<<endl;//
    				else
    					cout<<temp<<endl;
    			}
    		}
    		else if(flag==3) {
    			cin>>i1>>s1;//
    			if(i1==1) {
    				temp=strSet.addSet(s1);//
    				if(temp==-1)
    					cout<<"Full Set."<<endl;
    				else if(temp==-2)
    					cout<<s1<<" is already exist!"<<endl;
    				else
    					cout<<temp<<endl;
    			} else if(i1==2) {
    				temp=strSet.deleSet(s1);//
    				if(temp==-1)
    					cout<<s1<<" is not exist!"<<endl;//
    				else
    					cout<<temp<<endl;
    			} else if(i1==3) {
                 temp=strSet.findElem(s1);//
                 if(temp==-1)
                 cout<<s1<<" is not exist!"<<endl;//
                 else
                 cout<<temp<<endl;
    			}
    		}
    	}
    }
    
  • 相关阅读:
    你敢说自己了解单例模式?
    关于线程池,那些你还不知道的事
    Dubbo透传traceId/logid的一种思路
    当BeanUtils遇到泛型
    Oval框架如何校验枚举类型的一种思路
    HttpClient(4.5.x)正确的使用姿势
    HttpClient官方sample代码的深入分析(连接池)
    Jaxb如何优雅的处理CData
    JAXB性能优化
    Jaxb对xml报文头的小修小改
  • 原文地址:https://www.cnblogs.com/pluie/p/12952800.html
Copyright © 2011-2022 走看看