用类设计集合:
存储有限的集合(简称有界集合类),数据不允许存储,会存满,实现集合的添加,判满,判空,是否存在,清空,排序,存储到文件,从文件读取。
重载运算符: 下标,+(求和),-(求差),<<(输出插入符),>>(输入插入符)
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<fstream>
#define N 20
template<class T>
class set
{
public:
set(int length=0)
{
this->length=length;
};
~set(){
}
void FileIn();
void Empty();
void Add();
void Input();
void print();
void deleteitem();
void search();
void BobbleSort();
void SetMerging(set<T> &A,set<T> &B);
//----------------------------------
friend set<T> operator + (set<T> &a,set<T> &b);
friend set<T> operator - (set<T> &a,set<T> &b);
T & operator [] (int i);
friend ostream & operator<<( ostream& output,const set<T> &c);
friend istream & operator>>( istream& putin,set<T> &c);
//---------------------------------
private:
T items[100];
int length;
};
//--------------------------------------------------------------------------运算符重载
template<class T>
istream & operator>>( istream& putin,set<T> &c)
{
int n;
cout<<"输入你要输入的字符数目:";
cin>>n;
for(int i=0;i<n;i++)
{
putin>>c.items[i];
c.length++;
}
return putin;
}
template<class T>
ostream & operator<<( ostream& output,const set<T> &c)
{
int len=c.length;
for(int i=0;i<len;i++)
output<<c.items[i]<<" ";
return output;
}
//----------------
template<class T>
set<T> operator + (set<T> &a,set<T> &b)
{
int len1=a.length;
int len2=b.length;
set<T> c;
for(int i=0;i<len1+len2;i++)
{
if(i<len1)
c.items[i]=a.items[i];
else
c.items[i]=b.items[i-len1];
c.length++;
}
return c;
}
//---------------
template<class T>
set<T> operator - (set<T> &a,set<T> &b)
{
int len1=a.length;
int len2=b.length;
set<T> c;
for(int k=0;k<len1;k++)
c.items[k]=a.items[k];
c.length=a.length;
int len3=c.length;
for(int i=0;i<len3;i++)
for(int j=0;j<len2;j++)
{
if(b.items[j]==c.items[i])
{
for(int k=i;k<len1-1;k++)
c.items[k]=c.items[k+1];
c.length--;
len3--;
i--;
break;
}
}
return c;
}
//-----------------------------
template<class T>
T & set<T>::operator [](int i)
{
if(i>100||i<0)
{
cout<<"error of i"<<endl;
exit(1);
}
return items[i];
}
//---------------------------------------------------------------------------运算符重载
//---------------------------------------------------------------------------C++文件
template<class T>
void set<T>::FileIn()
{
fstream infile;
infile.open("实验2.dat",ios::in);
if(!infile)
{
cout<<"实验2.dat can't open."<<endl;
abort();
}
char ch;
while(infile.get(ch))
{
items[length]=ch;
length++;
}
infile.close();
}
//--------------------------------------------------------------------------C++文件
template<class T>
void set<T>::Empty()
{
if(length==0)
cout<<"Is Empty."<<endl;
}
template<class T>
void set<T>::SetMerging(set &A,set &B)
{
int j=0;
int len;
len=A.length+B.length;
for(int i=A.length;i<len;i++)
{
A.items[i]=B.items[j];
j++;
A.length++;
}
}
//
template<class T>
void set<T>::BobbleSort()
{
int len=length;
int t;
for(int i=0;i<len-1;i++)
for(int j=i+1;j<len;j++)
{
if(items[i]>items[j])
{ t=items[i];
items[i]=items[j];
items[j]=t;
}
}
}
//
template<class T>
void set<T>::search()
{
int k=0;
int len=length;
char elem;
cout<<"输入你要查找的元素:";
cin>>elem;
int j=0;
for(int i=0;i<len;i++)
{
if(elem==items[i])
{
j++;
cout<<"第"<<j<<"个相等元素在位置:"<<i+1<<endl;
k++;
}
}
if(k==0)
cout<<"未找到";
cout<<endl;
}
//
template<class T>
void set<T>::deleteitem()
{
int x;
int len=length;
cout<<"输入你需要删除的元素位置(第几个元素):";
cin>>x;
for(int i=x;i<len;i++)
{
items[i-1]=items[i];
}
length--;
}
//
template<class T>
void set<T>::Input()
{
int n;
char elem;
length=0;
cout<<"输入你要输入的字符(运算符重载测试整形数)数目:";
cin>>n;
for(int i=0;i<n;i++)
{
cin>>elem;
items[i]=elem;
length++;
}
}
//
template<class T>
void set<T>::Add()
{
char elem;
int n;
cout<<"输入你需要继续增加的元素数目:";
cin>>n;
int len;
len=length;
for(int i=len;i<len+n;i++)
{
cin>>elem;
items[i]=elem;
length++;
}
}
//
template<class T>
void set<T>::print()
{
int len;
len=length;
for(int i=0;i<len;i++)
{
cout<<items[i]<<" ";
}
cout<<endl;
}
int main()
{
set<char> A;
set<char> B;
set<char> C;
cout<<"输入A"<<endl;
A.Input();
A.print();
B.FileIn();
cout<<"B通过文件输入为:"<<endl;
B.print();
//----------------------------------------------加
C=A+B;
cout<<"C=A+B:" ;
C.print();
//-----------
//-----------------------------------------------减
C=A-B;
cout<<"C=A-B:";
C.print();
//----------
//------------------------------------------下标运算
A[1]='c';
cout<<"把A[1]=c:"<<endl;
A.print();
//------------------
//------------------------------------------增加A,B,C;
cout<<"Add A:"<<endl;
A.Add();
cout<<"Add B"<<endl;
B.Add();
cout<<"Add C"<<endl;
C.Add();
//------------------------------------------输出加后数组
cout<<"A数组:";
A.print();
cout<<"B数组:";
B.print();
cout<<"C数组:";
C.print();
//-------------------
system("pause");
system("cls");
cout<<"A数组:";
A.print();
cout<<"B数组:";
B.print();
cout<<"C数组:";
C.print();
//------------------------------------------删-查-排序
cout<<"删除数组A的元素:";
A.deleteitem();
A.print();
cout<<"查找数组A的元素:";
A.search();
cout<<"排序数组A:";
A.BobbleSort();
A.print();
//---------------
/*SetMerging(A,B);//---------------------------- 试试,不用
cout<<"把数组B加到数组A后面"<<endl;
A.SetMerging(A,B);
A.print();
*/
//-----------------------------------------------运算符重载:
system("pause");
system("cls");
cout<<"\t\t函数重载:"<<endl;
set<int> D;
set<int> E;
set<int> F;
set<int> G;
cout<<"输入D:";
cin>>D;
cout<<"输入E:";
cin>>E;
//-------------
system("pause");
system("cls");
//
cout<<"D:" ;
cout<<D<<endl;
cout<<"E:";
cout<<E<<endl;
F=D+E;
cout<<"F=D+E:";
cout<<F<<endl;
G=D-E;
cout<<"G=D-E:";
cout<<G<<endl;
G[0]=99;
cout<<"把G[0]=g"<<endl;
cout<<"G=";
cout<<G<<endl;
//------------------------------------------------运算符重载
return 0;
}