1. 程式人生 > >集合的模擬實現(函數模板)

集合的模擬實現(函數模板)

delete bcd break 一行 其中 執行 eset 數據類型 turn

我們可以用一個數組來模擬集合,add運算用以實現集合元素的增加,delete運算用於實現集合元素的刪除,find運算用以實現集合元素的查找,但是目前集合元素類型未知,可以是int、char、double等基本數據類型,也可以是String、Time、Student等對象類型,要求采用模板函數實現集合元素的增加、刪除和查找功能。

三個模板函數如下:

int addSet(T * myset, T elem,int len)

int deleSet(T * myset, T elem, int len)

int findElem(T * myset, T elem, int len)

其中,addSet向集合中添加一個元素,deleSet從集合中刪除一個元素,findElem判斷elem是否是集合成員,三個函數分別返回元素插入位置,刪除位置和存在位置。

主函數有如下數據成員 :

int intSet[100]

double douSet[100]

String StrSet[100] 分別是int類型、double類型、String的數組集合。

int intLen, douLen, strLen分別是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!

------------------------------------------------------------------------------------------------------

                   參考代碼

------------------------------------------------------------------------------------------------------

  1 #include<iostream>
  2 using namespace std;
  3 //實現add函數模版 
  4 template<typename classType>
  5 int add(classType set[],classType n,int place)//傳人數組和元素
  6 {
  7     int i,flag=0;
  8     for(i=0;i<place;i++)
  9     {
 10         if(set[i]==n)//已經存在的情況  
 11         {
 12             cout<<n<<" is already exist!"<<endl;
 13             flag=1;
 14             break;
 15         }
 16     }
 17     if(i==100)   cout<<"Full Set."<<endl;//數組已滿的情況 
 18     else  if(flag==0)//正常情況 
 19     {
 20         place++;
 21         set[i]=n;
 22         cout<<i<<endl;
 23     }
 24     return place;
 25 }
 26 //實現delete
 27 template<typename classType>
 28 int deleted(classType set[],classType n,int place)
 29 {
 30     int i,flag=0,j;
 31     for(i=0;i<place;i++)
 32     {
 33         if(set[i]==n)
 34         {
 35             flag=1;
 36             cout<<i<<endl;
 37             break;
 38         }
 39     }
 40     if(flag)
 41     {
 42         place--;
 43         for(j=i;j<place;j++)
 44         {
 45             set[j]=set[j+1];
 46         }
 47     }
 48     else cout<<n<<" is not exist!"<<endl;
 49     return place;
 50 }
 51 //實現find
 52 template<typename classType>
 53 void find(classType set[],classType n,int place)
 54 {
 55     int i,flag=0;
 56     for(i=0;i<place;i++)
 57     {
 58         if(set[i]==n)
 59         {
 60             cout<<i<<endl;
 61             flag=1;
 62             break;
 63         }
 64     }
 65     if(flag==0)   cout<<n<<" is not exist!"<<endl;
 66 }
 67 int main()
 68 {
 69     //數組模擬集合 
 70     int intSet[100];
 71     double douSet[100];
 72     string StrSet[100];
 73     //記錄集合長度 
 74     int intLen,douLen,StrLen;
 75     //add.find 
 76     int classType,workType;
 77     int intPlace=0,douPlace=0,strPlace=0;
 78     cin>>classType;
 79     while(classType!=0)
 80     {
 81         switch(classType)
 82         {
 83             case 1://int
 84                     {
 85                         int helloIn;
 86                         cin>>workType>>helloIn;
 87                         switch(workType)
 88                         {
 89                             //插入 
 90                             case 1:intPlace=add(intSet,helloIn,intPlace);break;
 91                             //刪除         
 92                             case 2:intPlace=deleted(intSet,helloIn,intPlace);break;
 93                             //查找        
 94                             case 3:find(intSet,helloIn,intPlace);break;    
 95                         }
 96                         break;
 97                     }
 98             case 2://double
 99                     {
100                         double helloDou;
101                         cin>>workType>>helloDou;
102                         switch(workType)
103                         {
104                             //插入 
105                             case 1:douPlace=add(douSet,helloDou,douPlace);break;
106                             //刪除         
107                             case 2:douPlace=deleted(douSet,helloDou,douPlace);break;
108                             //查找        
109                             case 3:find(douSet,helloDou,douPlace);break;
110                         }
111                         break;
112                     }
113             case 3://string        
114                     {
115                         string helloStr;
116                         cin>>workType>>helloStr;
117                         switch(workType)
118                         {
119                             //插入 
120                             case 1:strPlace=add(StrSet,helloStr,strPlace);break;
121                             //刪除         
122                             case 2:strPlace=deleted(StrSet,helloStr,strPlace);break;
123                             //查找        
124                             case 3:find(StrSet,helloStr,strPlace);break;                        
125                         }
126                         break;
127                     }
128         }
129         cin>>classType;
130     }    
131     return 0;
132 }

集合的模擬實現(函數模板)