IntegerSet

来源:互联网 发布:java面向过程编程 编辑:程序博客网 时间:2024/05/17 02:33

Program 4:
The Infamous IntegerSet


Create a class called IntegerSet. Each object of the class IntegerSet can hold integers in the range of 0 through 100. A set is represented internally as an array of bool's. Array element a[i] is true ("turned-on") if integer i is in the set . Array element a[k] is false ("turned-off") if integer k is not in the set . The default constructor should initialize the set to the so called "empty set" i.e. so all the elements would have an initial value of false. There are better ways to store the elements of the set, but for this homework stick to the array of booleans implementation.

Provide the member functions for the common set operations:

void IntegerSet::unionOf(const IntegerSet &set1, const IntegerSet &set2 ): set the values of an IntegerSet to the set-theoretic union of two existing sets ( ie an element of the IntegerSet object which the function is called on would be "turned on" ( = = true) if the same element in either of the passed in parameter sets is "turn-on", otherwise the element would be set to false).

void IntegerSet::intersectionOf( const IntegerSet &set1, const IntegerSet &set2 ): set the values of an IntegerSet to the set-theoretic intersection of two existing sets ( ie an element of the IntegerSet object which the function is called on would be "turned on" ( = = true ) if the same element is "on" in both of the passed in parameter sets, otherwise the element would be set to false).

bool IntegerSet::isEqualTo( const IntegerSet &set ): returns a boolean indicating whether the sets are equal.

void IntegerSet::insertElement( int num ): allows a new integer num to be inserted into the set. If num is not within the valid range of numbers for the set, the set should remain unchanged

void IntegerSet::deleteElement( int num ): removes an integer num from the set ( ie it "turns-off" that element in the array). If num is not in the set ( or out of the range ) the set should remain unchange

bool IntegerSet::isInSet( int num ): returns true if num is contained within the set, otherwise it returns false

void IntegerSet::printSet( ostream &ostr ): print the IntegerSet in the following fashion:

{ }// would represent an empty set
{ 2, 14, 52 }// would represent the set containing 2, 14, 52. Yes, the commas are required.

When printing the set do not include an new line at the end of the set, let the user of the class decide if they want a new line or not.

Provide a second constructor that will receive two arguments. The first arguement is an array of ints and the second argument is the number of int's contained within the array.

Your main program is going to test the IntegerSet class. It will prompt the user for numbers to be included within the set and then create IntegerSets with the user input. For set1 use the default constructor, and for set2 use the constructor that recieves the array. After creating the two sets, it should display the two sets, whether the sets are equal to each other, and then the intersection and union of the two sets ( to display the intersection and union you will need to create two new IntegerSets). Then again prompt the user for numbers to remove from set1 and numbers to insert into set2, then again show the display the sets, their equality, and their intersections and unions. Allow the user to loop through this process more than once.

Here is an example of what your program would look like:

The Infamous Integer Set Program.Note: when entering numbers please seperate them with spaces.Enter Set 1: 1 2 19 88Enter Set 2: 19 5 12 -10 Set 1 is {1,2,19,88}Set 2 is {5,12,19}Set 1 == Set 2: FalseIntersection of both sets is {19}Union of both sets is {1,2,5,12,19,88}Enter numbers to remove from Set 1: 1 50Enter numbers to insert into Set 2: 223 2Set 1 is {2,19,88}Set 2 is {2,5,12,19}Set 1 == Set 2: FalseIntersection of both sets is {2,19}Union of both sets is {2,5,12,19,88}To try another couple of sets press 'y':

Your main should not error check the integers but should pass them to the IntegerSet where only integers within the 0-100 range will be accepted and included. The main should only act as the user interface; prompt the user for the input, break the input down and pass it to the sets, and output explainatory text and the sets.

You must separate you program into the different files; interface ( class header ), implementation (class function definitions ), and program file ( main to run it all ). For handing in the program, also include a sample run of your program. In the sample run try a wide variety of numbers ( negative, > 100, ones already in the set when trying to add, and ones not in the set when trying to delete) to show that your program works in different situations.

You may overload operators to work with your class for extra credit. For example the << to print your set, the + to add int's to the set, - to subtract from the set, = = to check for equality.

Due: in one week.

Comments: Comments are a way of documenting a program (explaining who did what and how). All programs for the rest of the course are required to have the following header documentation ( filled out with your information) and inline documentation to explain any tricky pieces of code.

// File Name: Actual name of file (like prog4.cpp)// Author: Your Name// Email: name@someplace ( if you have one)// Assignment: #
// Description: Short description of the program (2-3 sentences)
#include <iostream>.......the rest of the program

Style: I will start to take off points for poor style in your code.

  • Think about making your code readable.
  • Avoid line wrap ( in almost all cases a line can be shorted so that it will not wrap around when it prints).
  • If possible, print your program from a developement environment (VC++, UltraEdit), because then the environment will show syntax highlighting in the printout. ( I will give a few extra points if you print from from an development environment).
原创粉丝点击