第六次上机作业Define a concrete class intset&&EOJ2853

 1. intset.h + intset.cpp  Implementing class IntSet.
 Define, implement, and test a set of integers – 
    class IntSet. 

 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
#include
#include
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; i0)
        std::cout<
TEXT程序

//----------------------------------------------------------------------
//
// intsetTest.cpp : Test program for Lab 6.
//
//----------------------------------------------------------------------

#include 
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() <

2.EOJ 2853

url:http://acm.ecnu.edu.cn/problem/2853/
这道题在我们写了上述的类以后就显得简单了。

#include
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; i0)
        std::cout<>n>>m;
    intset a{n},b{m};
    for(int i=0; i>tmp;
        a.insert(tmp);
    }
    for(int i=0; i>tmp;
        b.insert(tmp);
    }
    (a.setintsection(b)).print();
    (a.setunion(b)).print();
    (a.setdifference(b)).print();
    return 0;
}

你可能感兴趣的:(C++)