C++代码实现定义一个元素类型为int、元素个数不受限制的集合类Intset,该类具有下面的接口 class IntSet public: IntSet();中?IntSet(const IntSet&s);~IntSet();bool is_empty()const;//判断是否为空集 int size()const;//获取元素个数 bool is element(int e)const;//判断e是否属于集合 bool is subset(const IntSet&s) const;//判断集合是否包含于s bool is_equal(const IntSet&s)const;//判断集合是否相等 bool insert(int e);将e加入到集合中,成功返回true,失败返回false bool remove(int e);//把e从集合中删除,成功返回true,失败返回false IntSet union2(const IntSet&s)const;//计算集合的并集 Intset intersection2(const IntSet&s)const;//计算集合的交集 Intset difference2(const IntSet&s)const;//计算集合的差 };;需写 3个文件,类的定义在.h中,类的实现在 .cpp中,主函数在main .cpp中 • 所有的数据成员私有
时间: 2024-02-19 08:57:53 浏览: 68
好的,下面是Intset类的定义:
Intset.h
```c++
#ifndef INTSET_H
#define INTSET_H
class IntSet {
public:
IntSet();
IntSet(const IntSet& s);
~IntSet();
bool is_empty() const;
int size() const;
bool is_element(int e) const;
bool is_subset(const IntSet& s) const;
bool is_equal(const IntSet& s) const;
bool insert(int e);
bool remove(int e);
IntSet union2(const IntSet& s) const;
IntSet intersection2(const IntSet& s) const;
IntSet difference2(const IntSet& s) const;
private:
int* data;
int count;
int capacity;
void resize();
void copy(const IntSet& s);
};
#endif
```
Intset.cpp
```c++
#include "IntSet.h"
#include <algorithm>
#include <cstring>
IntSet::IntSet() : data(nullptr), count(0), capacity(0) {}
IntSet::IntSet(const IntSet& s) : data(nullptr), count(0), capacity(0) {
copy(s);
}
IntSet::~IntSet() {
delete[] data;
}
bool IntSet::is_empty() const {
return count == 0;
}
int IntSet::size() const {
return count;
}
bool IntSet::is_element(int e) const {
for (int i = 0; i < count; i++) {
if (data[i] == e) {
return true;
}
}
return false;
}
bool IntSet::is_subset(const IntSet& s) const {
for (int i = 0; i < s.count; i++) {
if (!is_element(s.data[i])) {
return false;
}
}
return true;
}
bool IntSet::is_equal(const IntSet& s) const {
if (count != s.count) {
return false;
}
for (int i = 0; i < count; i++) {
if (!s.is_element(data[i])) {
return false;
}
}
return true;
}
bool IntSet::insert(int e) {
if (is_element(e)) {
return false;
}
if (count == capacity) {
resize();
}
data[count++] = e;
return true;
}
bool IntSet::remove(int e) {
for (int i = 0; i < count; i++) {
if (data[i] == e) {
std::memcpy(data + i, data + i + 1, (count - i - 1) * sizeof(int));
count--;
return true;
}
}
return false;
}
IntSet IntSet::union2(const IntSet& s) const {
IntSet result;
for (int i = 0; i < count; i++) {
result.insert(data[i]);
}
for (int i = 0; i < s.count; i++) {
result.insert(s.data[i]);
}
return result;
}
IntSet IntSet::intersection2(const IntSet& s) const {
IntSet result;
for (int i = 0; i < count; i++) {
if (s.is_element(data[i])) {
result.insert(data[i]);
}
}
return result;
}
IntSet IntSet::difference2(const IntSet& s) const {
IntSet result;
for (int i = 0; i < count; i++) {
if (!s.is_element(data[i])) {
result.insert(data[i]);
}
}
return result;
}
void IntSet::resize() {
int new_capacity = std::max(2 * capacity, 1);
int* new_data = new int[new_capacity];
std::memcpy(new_data, data, count * sizeof(int));
delete[] data;
data = new_data;
capacity = new_capacity;
}
void IntSet::copy(const IntSet& s) {
count = s.count;
capacity = count;
data = new int[capacity];
std::memcpy(data, s.data, count * sizeof(int));
}
```
main.cpp
```c++
#include "IntSet.h"
#include <iostream>
void print_set(const IntSet& s) {
std::cout << "{ ";
for (int i = 0; i < s.size(); i++) {
std::cout << s.data[i] << " ";
}
std::cout << "}\n";
}
int main() {
IntSet s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
IntSet s2(s1);
s2.remove(2);
s2.insert(5);
std::cout << "s1: ";
print_set(s1);
std::cout << "s2: ";
print_set(s2);
IntSet s3 = s1.union2(s2);
std::cout << "s1 union s2: ";
print_set(s3);
IntSet s4 = s1.intersection2(s2);
std::cout << "s1 intersection s2: ";
print_set(s4);
IntSet s5 = s1.difference2(s2);
std::cout << "s1 difference s2: ";
print_set(s5);
return 0;
}
```
阅读全文