面向对象技术C++平时作业.docVIP

  • 0
  • 0
  • 约6.93千字
  • 约 7页
  • 2019-04-03 发布于江苏
  • 举报
平时作业共2次 平时作业(1) 定义、实现并测试表示由整型数元素组成的集合类型IntSet。 需提供的操作至少应包括: 构造函数 析构函数 拷贝构造函数 插入元素 删除元素 清空集合 集合并 集合交 集合差 集合显示输出 集合显示输出的格式为{元素1, 元素2,…},空集的输出为{}。 /* intset.h */ #ifndef INTSET_H #define INTSET_H class IntSet { int cursize,maxsize; int *x; bool member(int t) const; public: IntSet(int m = 100);//? 构造函数 IntSet(const IntSet);//? 拷贝构造函数 ~IntSet();//? 析构函数 void insert(int t);//? 插入元素 void remove(int t);//? 删除元素 void clear();//? 清空集合 void print();//? 集合显示输出 IntSet setunion(const IntSet);//? 集合并 IntSet setdifference(const IntSet);//? 集合差 IntSet setintsection(const IntSet);//? 集合交 };#endif /* intset.cpp */ #include stdafx.h #include iostream #include cstdlib using namespace std; #include intset.h IntSet::IntSet(int m) { if (m1) exit(1); cursize=0; x=new int[maxsize=m]; } IntSet::~IntSet() { delete x; } IntSet::IntSet(const IntSet m) { cursize=m.cursize; x=new int[maxsize=m.maxsize]; for (int i=0;icursize;i++) { x[i]=m.x[i]; } } bool IntSet::member(int t) const { int l=0; int u=cursize-1; while (l=u) { int m=(u+l)/2; if (tx[m]) u=m-1; else if (tx[m]) l=m+1; else return true; } return false; } void IntSet::insert(int t) { if (member(t)) {return;} if (cursize=maxsize) {exit(1);} x[cursize++]=t; for (int i=cursize-1;i0;i--) { if (x[i]x[i-1]) { int temp=x[i]; x[i]=x[i-1]; x[i-1]=temp; } else{ break;}} } void IntSet::remove(int t) { int flag = 0; int pos; for (int i = 0; i cursize; i++) { if (t==x[i]) { flag = 1; pos = i; } } if (flag == 0) { cout该集合中不存在t这个元素,删除失败。endl; }else { int *temp = x; cursize--; x = new int[cursize]; for (int j = 0; j pos; j++) { x[j] = temp[j]; } for (int i = pos; i cursize; i++) { x[i] = temp[i+1]; } } } void IntSet::clear() { if (cursize=0) {return;} x = new int[maxsize]; cursize =0; } void IntSet::print() { cout {; if (cursize0) { for (int i=0;icursize;i++) { cout x[i]; if (i!=cursize-1) { cout

文档评论(0)

1亿VIP精品文档

相关文档