使用动态内存分配创建集合联合

时间:2015-11-12 12:37:23

标签: c++ operator-overloading

好的,所以我几天都在考虑这个问题,我真的不确定如何解决这些问题。所以我需要做以下事情:

  • 创建一个union方法(即union(set)),为两个集合创建一个union。这个方法应该以某种方式调用元素方法,这个方法的规范如下所述
  • 创建一个运算符重载方法(+),它表示集合的并集(不确定它们为什么不同,但这就是规范所要求的)。此方法还必须调用union方法
  • element(int)检查元素是否是集合的一部分,我有一个方法,但我不确定它是否正确
  • 将元素添加到新集合的运算符重载方法

如果你能解决至少其中一个问题,我将非常感激,我真的很难理解如何解决这些规范。非常感谢你的帮助!

#ifndef SET_H
#define SET_H

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

class Set{

        friend ostream &operator<< ( ostream &, const Set &);
        friend istream &operator>> ( istream &, Set &);

        public:

                Set ( int = DEFAULTSIZE ); //default constructor
                Set ( const Set & ); //copy constructor
                Set ( int [], int, char ); //constructor passing array of integers, size, name of set

                ~Set(); //destructor

                //assignment operator
                const Set &operator= ( const Set &);
                //equality operator
                bool operator== ( const Set & ) const;
                //inequality operator
                bool operator!= ( const Set &s1) const{
                        return !(*this == s1);
                }
                //subscript operators
                int &operator[] ( int );
                int operator[] ( int ) const;

                //methods to find union, intersection, and difference of sets
                Set Union ( Set & );
                Set Intersect ( Set & );
                Set Difference ( Set & );

                Set operator+ ( Set & ); //to represent union of two sets
                Set operator^ ( Set & ); //to represent intersection of two sets
                Set operator- ( Set & ); //to represent difference between two sets

                bool element ( int );

        private:
                static const int DELIM = -999; // delimiter to signal end of input
                static const int DEFAULTSIZE = 10;
                int numOfElements;
                int psize; //physical size of array
                int *set; //pointer array to represent set

};
#endif

//SOURCE FILE

//default constructor
Set::Set ( int s ){

        if ( s > 0 )
                psize = s;
        else
                psize = DEFAULTSIZE;
        //allocate an array of specified size
        set = new int[ psize ];

        if(!set) {
                //send an error is system cannot allocate memory
                cout << "Cannot Allocate Memory, exiting program... " << endl;
                exit (1);
        }

        for ( int i = 0; i < psize; i++){
                set[i] = 0;
                numOfElements = 0;
        }

}

//copy constructor
Set::Set ( const Set &setToCopy): psize(setToCopy.psize){
        set = new int[psize];
        if(!set){
                cout << "Cannot Allocate Memory, exiting program..." << endl;
                exit (1);
        }
        for (int i = 0; i < psize; i++ ){
                set[i] = setToCopy.set[i];

                numOfElements = psize;
        }
}

Set::~Set(){
        if (set)
                delete [] set;
        set = NULL;
}

const Set &Set::operator= ( const Set &s1 ){
        if ( &s1 != this){
                if (numOfElements != s1.numOfElements){
                        delete [] set;
                        psize = numOfElements;
                        set = new int [psize];
                        if (!set){
                                cout << "Cannot Allocate memory, exiting program..." << endl;
                                exit (1);
                        }
                }
        }
        //assign contents of the array on the right to the contents of the array on the left
        for ( int i = 0; i < psize; i++ ){
                set[i] = s1.set[i];
                numOfElements = psize;
        }

        return (*this);
}

bool Set::operator== ( const Set &s1 ) const {
        bool validate = true;

        if ( numOfElements == s1.numOfElements ){
                for ( int i = 0; i < numOfElements; i++){
                        if ( set [i] != s1.set[i] ){
                                validate = false;
                                break;
                        }
                }
        }

        return (validate);
}

int &Set::operator[]( int subscript ){
        if ( subscript < 0 || subscript >= psize ) {
                cout << " Error, exiting program... " ;
                exit (1);
        }

        return set[subscript];
}
bool Set::element ( int n ) {
        bool validate = false;
        for ( int i = 0; i < psize; i++){
            if ( set[i] = n ) 
                validate = true;
        }                                                                       
        return (validate);
}

0 个答案:

没有答案