Provide union, intersection, difference and symmetric difference operations. Also provide other necessary operations: initialization, assignment, insert (an element), erase (an element), (another set is a) subset, (an element is a) member, print (all elements), count (of elements), (is set) empty, clear (all elements).
到了这一次作业开始,我们终于开始写真正的类了。那么这一次的要求是写一个整数的集合。
具体要实现的操作有插入删除元素,求并集、交集、差集等等。判断集合是否为空,集合中元素个数,清空集合。
此外,还要写构造函数等等。以及学会写异常处理,知道抛出exception概念等等。
代码由三个部分组成
即所说的 类的implemention(.cpp)、specification(.h)和包含main函数的测试cpp
下面上代码:
SPECIFICATION:
#ifndef INTSET_H_INCLUDED #define INTSET_H_INCLUDED class intset { public: int *p; int cnt; int capcity; intset(int n); intset(); ~intset(); void print(); int count(); void insert(int num); intset setunion(intset& t); intset setintsection(intset& t); intset setdifference(intset& t); intset setsymmetricdifference(intset& t); void clear(); bool empty()const; void erase(int num); bool subset(intset &t); struct bad_intset { int errnum; }; }; #endif // INTSET_H_INCLUDED IMPLEMENTION
#include"intset.h" #include<iostream> #include<cstring> #include<algorithm> intset::intset(int n) { bad_intset bi; if(n<1){bi.errnum=1;throw bi;} p=new int[n]; capcity=n; cnt=0; } intset::intset() { p=new int[1000]; capcity=1000; cnt=0; } intset::~intset() { delete []p; } void intset::print() { std::cout<<'{'; for(int i=0; i<cnt-1; i++) std::cout<<p[i]<<','; if(cnt>0) std::cout<<p[cnt-1]; std::cout<<'}'<<'\n'; } int intset::count() { return cnt; } void intset::insert(int num) { for(int i=0;i<cnt;i++) if(num==p[i])return ; p[cnt]=num; cnt++; } intset intset::setunion(intset& t) { intset ans{cnt+t.cnt}; for(int i=0; i<cnt; i++) ans.insert(p[i]); for(int i=0; i<t.cnt; i++) { int flag=1; for(int j=0; j<ans.cnt; j++) if(t.p[i]==ans.p[j]) { flag=0; break; } if(flag)ans.insert(t.p[i]); } std::sort(ans.p,ans.p+ans.cnt); return ans; } intset intset::setintsection(intset& t) { intset ans{cnt+t.cnt}; for(int i=0; i<cnt; i++) { int flag=0; for(int j=0; j<t.cnt; j++) if(p[i]==t.p[j]) { flag=1; break; } if(flag)ans.insert(p[i]); } return ans; } intset intset::setdifference(intset& t) { intset ans{cnt+t.cnt}; for(int i=0; i<cnt; i++) { int flag=1; for(int j=0; j<t.cnt; j++) if(p[i]==t.p[j]) { flag=0; break; } if(flag)ans.insert(p[i]); } return ans; } intset intset::setsymmetricdifference(intset& t) { intset r1{t.setdifference(*this)}; intset r2{(*this).setdifference(t)}; return r1.setunion(r2); } void intset::clear() { cnt=0; } bool intset::empty()const { if(cnt)return false; return true; } void intset::erase(int num) { int loc=0; for(loc=0; loc<cnt; loc++) if(p[loc]==num)break; if(loc!=cnt) { for(int i=loc; i<cnt; i++) p[loc]=p[loc+1]; cnt--; } } bool intset::subset(intset& t) { int flag; for(int i=0; i<t.cnt; i++) { flag=0; for(int j=0; j<cnt; j++) if(t.p[i]==p[j]) { break; flag=1; } if(flag)break; } if(flag)return true; return false; }TEXT程序
//---------------------------------------------------------------------- // // intsetTest.cpp : Test program for Lab 6. // //---------------------------------------------------------------------- #include <iostream> using namespace std; #include "intset.h" // your header file for class intset int main(int argc,char**argv) try { // ***** test for initialization ***** intset a{100}; // an integer set with 100 maximum elements intset b{30}; // an integer set with 30 maximum elements // ***** test for printing ***** cout << "Set a after initialization: " << endl; a.print(); // display set cout << endl; // ***** test for counting ***** cout << "The number of elements in Set a : " << a.count() <<endl; cout << "Set b after initialization: " << endl; b.print(); // display set cout << endl; cout << "The number of elements in Set b : " << b.count() <<endl; cout << endl; // ***** test for inserting ***** for (int i{100}; i<150; i++) a.insert(i); // insert 50 elements from 100 to 149 for (int i{130}; i<160; i++) b.insert(i); // insert 30 elements from 130 to 159 cout << "Set a after insertion: " << endl; a.print(); // display set cout << endl; cout << "The number of elements of Set a : " << a.count() <<endl; cout << "Set b after insertion: " << endl; b.print(); // display set cout << endl; cout << "The number of elements of Set b : " << b.count() <<endl; cout << endl; // ***** test for union ***** cout << "Union of two integer sets a and b: " << endl; a.print(); // display set cout << " ∪ "; b.print(); // display set cout << " = "; intset r1{a.setunion(b)}; // compute union of two integer sets r1.print(); // display union result cout << endl; cout << endl; b.print(); // ***** test for intersection ***** cout << "intersection of two integer sets a and b: " << endl; a.print(); cout << " ∩ "; b.print(); cout << " = "; intset r2{a.setintsection(b)}; // compute intersection of two integer sets r2.print(); cout << endl; cout << endl; // ***** test for difference ***** cout << "difference of two integer sets a and b: " << endl; a.print(); cout << " - "; b.print(); cout << " = "; intset r3{a.setdifference(b)}; // compute difference of two integer sets r3.print(); cout << endl; cout << endl; cout << "difference of two integer sets b and a: " << endl; b.print(); cout << " - "; a.print(); cout << " = "; intset r4{b.setdifference(a)}; // compute difference of two integer sets r4.print(); cout << endl; cout << endl; // ***** test for symmetric difference ***** cout << "symmetric difference of two integer sets a and b: " << endl; a.print(); cout << " symmetric- "; b.print(); cout << " = "; intset r5{a.setsymmetricdifference(b)}; // compute symmetric difference of two integer sets r5.print(); cout << endl; cout << endl; // ***** test for assignment ***** cout << "Set a after assignment a=b : " << endl; a=b; a.print(); cout << endl; cout << endl; // ***** test for "clear" ***** cout << "Set b after erasing all elements : " << endl; b.clear(); b.print(); cout << endl; cout << endl; // ***** test for "empty" ***** if (a.empty()) cout << "Set a is empty " << endl; else cout << "Set a is not empty " << endl; cout << endl; if (b.empty()) cout << "Set b is empty " << endl; else cout << "Set b is not empty " << endl; cout << endl; // ***** test for "erase" ***** cout << "Set a after erasing an element 140 : " << endl; a.erase(140); a.print(); cout << endl; cout << endl; cout << "Set a after erasing an element 250 : " << endl; a.erase(250); a.print(); cout << endl; cout << endl; // ***** test for "subset" ***** if (a.subset(b)) cout << "Set b is a subset of a" << endl; else cout << "Set b is not a subset of a" << endl; cout << endl; cout << "insert an element 500 into set b : " << endl; b.insert(500); cout << "Set b : " << endl; b.print(); cout << endl; cout << endl; if (a.subset(b)) cout << "Set b is a subset of a" << endl; else cout << "Set b is not a subset of a" << endl; cout << endl; // ***** test for exception throwing ***** cout << "insert 500 elements from 1000 to 1499 into set a : " << endl; for (int i{1000}; i<1500; i++) a.insert(i); // insert 500 elements from 1000 to 1499*/ a.print(); return 0; } // ***** exception handling ***** catch(intset::bad_intset bi) // catch exceptions related to integer set { switch (bi.errnum) // # of exceptions { case 1: cerr << "bad intset: constructor parameter<1 , exit " << endl; // error of constructor parameter break; case 2: cerr << "bad intset: maximum elements reached, exit " << endl; // overflow of integer set break; } return 0; } 2.EOJ 2853
url:http://acm.ecnu.edu.cn/problem/2853/ 这道题在我们写了上述的类以后就显得简单了。
#include<bits/stdc++.h> class intset { public: int *p; int cnt; int capcity; intset(int n); ~intset(); void print(); void insert(int num); intset setunion(intset& t); intset setintsection(intset& t); intset setdifference(intset& t); }; intset::intset(int n) { p=new int[n]; capcity=n; cnt=0; } intset::~intset() { delete []p; } void intset::print() { std::sort(p,p+cnt); std::cout<<'{'; for(int i=0; i<cnt-1; i++) std::cout<<p[i]<<','; if(cnt>0) std::cout<<p[cnt-1]; std::cout<<'}'<<'\n'; } void intset::insert(int num) { for(int i=0; i<cnt; i++) if(num==p[i])return ; p[cnt]=num; cnt++; } intset intset::setunion(intset& t) { intset ans{cnt+t.cnt}; for(int i=0; i<cnt; i++) ans.insert(p[i]); for(int i=0; i<t.cnt; i++) { int flag=1; for(int j=0; j<ans.cnt; j++) if(t.p[i]==ans.p[j]) { flag=0; break; } if(flag)ans.insert(t.p[i]); } std::sort(ans.p,ans.p+ans.cnt); return ans; } intset intset::setintsection(intset& t) { intset ans{cnt+t.cnt}; for(int i=0; i<cnt; i++) { int flag=0; for(int j=0; j<t.cnt; j++) if(p[i]==t.p[j]) { flag=1; break; } if(flag)ans.insert(p[i]); } return ans; } intset intset::setdifference(intset& t) { intset ans{cnt+t.cnt}; for(int i=0; i<cnt; i++) { int flag=1; for(int j=0; j<t.cnt; j++) if(p[i]==t.p[j]) { flag=0; break; } if(flag)ans.insert(p[i]); } return ans; } using namespace std; int main() { int n,m,tmp; cin>>n>>m; intset a{n},b{m}; for(int i=0; i<n; i++) { cin>>tmp; a.insert(tmp); } for(int i=0; i<m; i++) { cin>>tmp; b.insert(tmp); } (a.setintsection(b)).print(); (a.setunion(b)).print(); (a.setdifference(b)).print(); return 0; }