大作业题(二)

本人大一新生,这是根据老师布置的作业自己写的代码,有不足之处请指正。
题目描述:
定义并实现一个整数集合类int_set,集合类中cur_size表明当前集合中有几个整数,集合中最多含max_size个整数,存放集合元素的数组是动态的。要求提供的方法有:
(1)增加一个整数到集合中;
(2)从集合中去掉一个元素;
(3)判断一个元素是否在集合中;
(4)重载<<运算符,输入集合;重载>>运算符输出集合;
(5)分别实现集合的交、并、差运算。
 

头文件:int_set.h

#ifndef _INT_SET_H_
#define _INT_SET_H_
#include <iostream>
using std::ostream;
using std::istream;
class int_set {
private:
	int max_size;
	int cur_size;
	int* set;
public:
	int_set();//集合类的默认构造函数
	int_set(int _max_size);//带一个参数的集合类的构造函数
	int_set(const int_set&);//集合类的复制构造函数
	~int_set();//集合类的析构函数
	bool insert(int);//增加一个整数到集合中
	bool erase(int);//从集合中去掉一个元素
	int contain(int) const;//判断一个元素是否在集合中,返回-1,证明不存在,否则返回该元素的下标
	void operator =(const int_set &);//赋值号的重载
	friend ostream & operator << (ostream&, const int_set&);//重载提取(<<)运算符
	friend istream & operator >> (istream&, int_set&);//重载插入(>>)运算符
	int_set operator *(const int_set &);//交集运算符号,求一个集合与另一个集合的交集
	int_set operator +(const int_set &);//并集运算符号,求一个集合与另一个集合的并集
	int_set operator -(const int_set &);//差集运算符号,求一个集合与另一个集合的差集
};
#endif

类的实现:int_set.cpp

#include "int_set.h"
#include <string>
#include <sstream>
using std::string;
using std::stringstream;
using std::getline;
using std::cout;
using std::cin;
using std::endl;
int_set::int_set():max_size(100) , cur_size(0) {//集合类的默认构造函数
	set = new int[max_size]();
}
int_set::int_set(int _max_size):max_size(_max_size), cur_size(0) {//带一个参数的集合类的构造函数
	set = new int[max_size]();
}
int_set::int_set(const int_set &s):max_size(s.max_size), cur_size(s.cur_size) {//集合类的复制构造函数
	set = new int[max_size]();
	int i;
	for (i = 0; i < cur_size; i++) set[i] = s.set[i];
}
int_set::~int_set() {//集合类的析构函数
	delete[] set;
}
bool int_set::insert(int v) {//增加一个整数到集合中
	if (cur_size + 1 > max_size) {
		cout << "集合已经达到最大储存限度,插入" << v << "失败。" << endl;
		return false;
	}
	if (contain(v) != -1) return true;
	if (cur_size == 0) {
		set[0] = v;
		cur_size++;
		return true;
	}
	int i, j = cur_size;
	for (i = 0; set[i] < v&&i < cur_size; i++);
	for (; j > i; j--) set[j] = set[j - 1];
	set[i] = v;
	cur_size++;
	return true;
}
bool int_set::erase(int v) {//从集合中去掉一个元素
	if (contain(v) == -1) {
		cout << "集合中不存在" << v << "元素" << endl;
		return false;
	}
	int i = contain(v);
	for (; i < cur_size; i++) set[i] = set[i + 1];
	set[cur_size] = 0;
	cur_size--;
	return true;
}
int int_set::contain(int v) const {//判断一个元素是否在集合中,返回-1,证明不存在,否则返回该元素的下标
	int i;
	for (i = 0; i < cur_size; i++)
		if (set[i] == v) break;
	if (i == cur_size) return -1;
	return i;
}
void int_set::operator=(const int_set &s) {//赋值号的重载
	delete[] set;
	max_size = s.max_size;
	cur_size = s.cur_size;
	set = new int[max_size]();
	int i;
	for (i = 0; i < cur_size; i++) set[i] = s.set[i];
}
ostream & operator<<(ostream & out, const int_set & s) {//重载提取(<<)运算符
	int i;
	for (i = 0; i < s.cur_size; i++) out << s.set[i] << ' ';
	out << endl;
	return out;
}
istream & operator>>(istream & in, int_set & s) {//重载插入(>>)运算符
	string str;
	getline(in, str);
	stringstream sstream(str);
	int v;
	while (sstream >> v) s.insert(v);
	return in;
}
int_set int_set::operator*(const int_set &s) {//int_set类的交集运算
	int max = (max_size > s.max_size) ? max_size : s.max_size;
	int_set b(max);//创建一个空集合b
	int i;
	for (i = 0; i < cur_size; i++)//遍历原集合中的元素,如果原集合中的某个元素在s集合中存在,那么向b中插入这元素
		if (s.contain(set[i]) != -1)
			b.insert(set[i]);
	return b;
}
int_set int_set::operator+(const int_set &s) {//int_set类的并集运算
	int max = max_size + s.max_size;
	int_set b(max);//创建一个空的集合b
	int i;
	for (i = 0; i < s.cur_size; i++) //遍历两个集合,向b中插入两个集合中的元素
		b.insert(s.set[i]);
	for (i = 0; i < cur_size; i++)
		b.insert(set[i]);
	return b;
}
int_set int_set::operator-(const int_set &s) {//int_set类的差集运算
	//差集的求法,将原集合中包含的s集合的元素删去
	int_set b = *this;
	int i;
	for (i = cur_size - 1; i != -1; i--)
		if (s.contain(set[i]) != -1) b.erase(set[i]);
	return b;
}

测试程序:main.cpp

#include "int_set.h"
using std::cout;
using std::cin;
int main() {
	int_set set1, set2, set3;
	cout << "给定一个集合set1,它的默认元素为1,2,7,9\n";
	cout << "给定一个集合set2,它的默认元素为8,9,10\n";
	set1.insert(1);
	set1.insert(2);
	set1.insert(7);
	set1.insert(9);
	set2.insert(8);
	set2.insert(9);
	set2.insert(10);
	cout << "下面调试运算符>>\n";
	cout << "输入一个集合set3\n";
	cin >> set3;
	cout << "下面调试运算符<<\n";
	cout << "set1的元素为" << set1;
	cout << "set2的元素为" << set2;
	cout << "set3的元素为" << set3;
	cout << "调试并集(+)运算符:求set1和set3的并集,结果储存在set4中\n";
	int_set set4 = set3 + set1;
	cout << "set4的元素为" << set4;
	cout << "调试交集(*)运算符:求set2和set1的交集,结果储存在set4中\n";
	set4 = set1 * set2;
	cout << "set4的元素为" << set4;
	cout << "调试差集(-)运算符:求set3和set2的差集,结果储存在set4中\n";
	set4 = set3 - set2;
	cout << "set4的元素为" << set4;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43773570/article/details/85477068