[Classes and Objects]IntegerSet

Introduction

Create class IntegerSet for which each object can hold integers in the range 0 through 100.
A set is represented internally as an array of ones and zeros.
Array element a[ i ] is 1 if integer i is in the set.
Array element a[ j ] is 0 if integer j is not in the set.

The default constructor initializes a set to the so-called “empty-set,” i.e., a set whose array representation contains all zeros.

Provide member functions for the common set operations.

For example, a unionOfSets member function (already provided) creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third array’s is set to 1 if that element is 1 in either or both of the existing sets, and an element of the third set’s array is set to 0 if that element is 0 in each of the existing sets).

Provide an intersectionOfSets member function which creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set’s array is set to 0 if that element is 0 in either or both of the existing sets, and an element of the third set’s array is set to 1 if that element is 1 in each of the existing sets).
An insertElement member function (already provided) inserts a new integer k into a set (by setting a[ k ] to 1). If k is invalid, print “Invalid insert attempted!\n”.
Provide a deleteElement member function that deletes integer m (by setting a[ m ] to 0). If m is invalid, print

“Invalid delete attempted\n”. A printSet member function (already provided) prints a set as a list of numbers separated by spaces. Print only those elements which are present in the set (i.e., their position in the array has a value of 1). Print "—"without quotation marks for an empty set.

Provide an isEqualTo member function that determines whether two sets are equal. Provide an additional constructor that receives an array of integers and the size of that array and uses the array to initialize a set object.

Sample Output

Enter set A:
Enter an element (-1 to end): 45
Enter an element (-1 to end): 76
Enter an element (-1 to end): 34
Enter an element (-1 to end): 6
Enter an element (-1 to end): -1
Entry complete
Enter set B:
Enter an element (-1 to end): 34
Enter an element (-1 to end): 8
Enter an element (-1 to end): 93
Enter an element (-1 to end): 45
Enter an element (-1 to end): -1
Entry complete
Union of A and B is:
{ 6 8 34 45 76 93 }
Intersection of A and B is:
{ 34 45 }
Set A is not equal to set B
Inserting 77 into set A...
Set A is now:
{ 6 34 45 76 77 }
Deleting 77 from set A...
Set A is now:
{ 6 34 45 76 }
Invalid insert attempted!
Invalid insert attempted!
Set e is:
{ 1 2 9 25 45 67 99 100 }

From 劳思

IntegerSet.h
// Lab 2: IntegerSet.h
// Header file for class IntegerSet
#ifndef INTEGER_SET_H
#define INTEGER_SET_H
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
using std::setw;
class IntegerSet {
     public:
          // default constructor
          IntegerSet() {
                emptySet();  // set all elements of set to 0
          }  // end IntegerSet constructor
 
          IntegerSet(int s[], int l);  // constructor that takes an initial set
          IntegerSet unionOfSets(const IntegerSet&);
          IntegerSet intersectionOfSets(const IntegerSet&);
          void emptySet();  // set all elements of set to 0
          void inputSet() {  // read values from user
              int number;
 
              do {
                  cout << "Enter an element (-1 to end): ";
                  cin >> number;
 
                  if (validEntry(number))
                      set[ number ] = 1;
                  else if (number != -1)
                      cerr << "Invalid Element\n";
              } while (number != -1);   // end do...while
 
              cout << "Entry complete\n";
          }
          void insertElement(int element);
          void deleteElement(int element);
          void printSet() const {
              int x = 1;
              bool empty = true;  // assume set is empty
              cout << '{';
              for (int u = 0; u < 101; u++) {
                if (set[ u ]) {
                     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
                     empty = false;  // set is not empty
                    x++;
                }  // end if
              }  // end for
 
              if (empty)
                  cout << setw(4) << "---";  // display an empty set
              cout << setw(4) << "}" << '\n';
          }
          bool isEqualTo(const IntegerSet&) const;
 
     private:
          int set[ 101 ];  // range of 0 - 100
          // determines a valid entry to the set
          int validEntry(int x) const {
              return (x >= 0 && x <= 100);
          }  // end function validEntry
};  // end class IntegerSet
#endif
 
SetText.cpp
// Lab 2: SetTest.cpp
// Driver program for class IntegerSet.
#include <iostream>
using std::cout;
 
#include "IntegerSet.h"  // IntegerSet class definition
 
int main() {
    IntegerSet a;
    IntegerSet b;
    IntegerSet c;
    IntegerSet d;
 
    cout << "Enter set A:\n";
    a.inputSet();
    cout << "\nEnter set B:\n";
    b.inputSet();
    c = a.unionOfSets(b);
    d = a.intersectionOfSets(b);
    cout << "\nUnion of A and B is:\n";
    c.printSet();
    cout << "Intersection of A and B is:\n";
    d.printSet();
 
    if (a.isEqualTo(b))
        cout << "Set A is equal to set B\n";
    else
        cout << "Set A is not equal to set B\n";
 
    cout << "\nInserting 77 into set A...\n";
    a.insertElement(77);
    cout << "Set A is now:\n";
    a.printSet();
 
    cout << "\nDeleting 77 from set A...\n";
    a.deleteElement(77);
    cout << "Set A is now:\n";
    a.printSet();
 
    const int arraySize = 10;
    int intArray[ arraySize ] = { 25, 67, 2, 9, 99, 105, 45, -5, 100, 1 };
    IntegerSet e(intArray, arraySize);
 
    cout << "\nSet e is:\n";
    e.printSet();
 
    cout << endl;
}  // end main
IntegerSet.cpp
#include "IntegerSet.h"

void IntegerSet::emptySet()
{
	for(int i = 0;i <= 100;++i) 
        set[i]=0;
}
IntegerSet::IntegerSet(int s[], int l)
{
	emptySet();
	for(int i=0;i<l;++i)
    {
		if ( validEntry(s[i]) ) 
            set[s[i]] = 1;
		else 
            printf("Invalid insert attempted!\n");
	}
}
IntegerSet IntegerSet::unionOfSets(const IntegerSet& other)
{
	int a[101] = {0},len = 0;
	for(int i = 0;i <= 100;++i) 
    {
		if (set[i] == 1 || other.set[i] == 1)
        {
			a[len++] = i;
		}
	}

	IntegerSet ans(a,len);
	return ans;
}
IntegerSet IntegerSet::intersectionOfSets(const IntegerSet& other)
{
	int a[101] = {0},len = 0;;
	for(int i = 0;i <= 100;++i)
    {
		if (set[i] == 1 && other.set[i] == 1)
        {
			a[len++] = i;
		}
	}
	IntegerSet ans(a,len);
	return ans;
}
void IntegerSet::insertElement(int e)
{
	if (validEntry(e))
    {
			set[e] = 1;
	}
	else 
        printf("Invalid insert attempted!\n");
}
void IntegerSet::deleteElement(int e)
{
	if (validEntry(e))
    {
		set[e] = 0;
	}
	else printf("Invalid delete attempted\n");
}
bool IntegerSet::isEqualTo(const IntegerSet & other) const
{
 	for(int i = 0;i <= 100;++i)
    {
 		if (set[i] != other.set[i]) 
            return false;
 	}
 	return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值