实验一:集合类设计与实现

#include
#include
#include
#include<stdlib.h>
using namespace std;

class A//自定义类
{
friend ostream& operator<<(ostream& os,A& c){

	cout << c.data<<" ";

return os;

}
friend bool operator==(A& a1, A& a2)
{
return a1.data == a2.data;
}
public:
A(int d=0)
{
data = d;
}
public:
void SetData(int d);
private:
int data;
};
void A::SetData(int d)
{
data = d;
}

template<class T,int MaxSet=5>
class TSet{
public:
friend TSet operator+(TSet& b,T c){//运算符+重载: 相当于添加一个
for (int i = 0; i<b.ptr; i++) if (c == b.date[i]) return b;
if (b.ptr >= MaxSet) return b;
b.date[b.ptr] = c; b.ptr++;
return b;
}
friend TSet operator-(TSet& b,T c){//运算符-重载:相当于删除一个
int p=b.ptr;
for (int i = 0; i<p; i++)
if (c == b.date[i])
{
for (int j = i; j<p- 1; j++)
{
b.date[j] = b.date[j + 1];
}
b.ptr–;
}
return b;
}

int& operator[](int index){//下标运算符[]重载

   return date[index];
}

friend ostream& operator<<(ostream& os,TSet& c){//输出运算符<<重载
for (int i = 0; i<c.ptr; i++)
cout << c.date[i]<<" ";
return os;
}
TSet(){
ptr = 0;

}
 void Add(T a){         //添加一个
	for (int i = 0; i<ptr; i++) if (a == date[i]) return;
	if (ptr >= MaxSet) return;
	date[ptr] = a;
	ptr++;
}
void Add(T a[],int c){//添加数组中部分数,如从下标0开始的5个数

	for (int j = 0; j<c; j++)
	{
	
	date[ptr] = a[j]; ptr++; 
		add(a[j]);
	}
}
void modify(int i, T a){//修改某个数
	date[i] = a;
}
void del(T a){//删除某个数
     int p=ptr;
	for (int i = 0; i<p; i++)
	if (a == date[i])
	{   
	for (int j = i; j<p- 1; j++)
		{
			date[j] = date[j + 1];
	}
	 ptr--;
	}
}
void sort(){//集合元素排序
	int t=0,p;
	p=ptr;
	for (int i = 0; i<p - 1; i++)
	{for (int j = 0; j<p - i-1; j++){
		if (date[j]>date[j + 1])
		{
			t = date[j];
			date[j] = date[j + 1];
			date[j + 1] = t;
		}
	}}
}
void show(){//展示集合类里的元素
	for (int i = 0; i<ptr; i++)
		cout << date[i] << " " ;
	cout<<endl;
}
int count;
void writeFile(){//集合存到文件中
fstream outstrm;
outstrm.open("C:/Users/26037/Desktop/file1.txt", ios::out);
if (!outstrm)
{
	cout << "file1.dat can't open\n";
	abort();
}
for(int i=0;i<ptr;i++){
outstrm << date[i]<<" ";
}
count=i;
outstrm.close();

} 
void readFile(TSet& b){//从文件读创建集合类,上面的逆过程
     fstream instrm;
instrm.open("C:/Users/26037/Desktop/file1.txt", ios::in);
if (!instrm)
{
	cout << "file1.dat can't open.\n";
	abort();
}
cout << endl << "read from file::::   ";
for(int i=0;i<count;i++)
{
	T t;
	instrm >> t;
	b.Add(t);
}
instrm.close();

b.show();
}
private:
T date[MaxSet];
int ptr;
};
int main()
{TSet<A,4> st0;
st0.Add(A(1));
st0.Add(A(1));
st0.Add(A(2));
st0.Add(A(3));
st0.Add(A(4));
st0.Add(A(5));
st0.show();

TSet<int,5> st1;
st1.Add(1);
st1.Add(1);
st1.Add(2);
st1.Add(3);
st1.Add(4);
st1.show();

TSet<double,2> st2;
st2.Add(1.1);
st2.Add(1.1);
st2.Add(2.1);
st2.Add(3.1);
st2.show();

TSet<char,2> st3;
st3.Add(‘a’);
st3.Add(‘a’);
st3.Add(‘b’);
st3.Add(‘c’);
st3.show();

st3.writeFile();

TSet<char,2> st4;
st3.readFile(st4);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>