📄 integerset.cpp
字号:
// Chapter 7 of C++ How to Program
// integerset.cpp
#include <iostream>
using std::cout;
using std::cin;
#include <iomanip>
#include <new>
using std::setw;
/* Write include directive for integerSet.h here */
// constructor
IntegerSet::integerset( int s )
{
size = s;
set = new int[ size ];
/* write call to emptySet */
} // end class IntegerSet constructor
// copy constructor
IntegerSet::IntegerSet( const IntegerSet &init )
{
size = init.size;
/* write statement to allocate sufficient memory */
emptySet();
for ( int i = 0; i < size; i++ )
/* write statement to copy elements of init */
} // end copy constructor
/* write a definition for emptySet */
// input set
void IntegerSet::inputSet()
{
int number;
// input set information
do {
cout << "Enter an element (-1 to end): ";
cin >> number;
// check number first
if ( validEntry( number ) )
set[ number ] = 1;
else if ( number != -1 )
cout << "Invalid Element\n";
} while ( number != -1 );
cout << "Entry complete\n";
} // end function inputSet
// print the set
void IntegerSet::setPrint() const
{
int x = 1;
bool empty = true; // assume set is empty
cout << '{';
for ( int u = 0; u < size; ++u )
if ( set[ u ] ) {
cout << setw( 4 ) << u << ( x % 10 == 0 ? "\n" : "" );
empty = false; // set is not empty
++x;
} // end if
if ( empty )
cout << setw( 4 ) << "---"; // display an empty set
cout << setw( 4 ) << "}" << '\n';
} // end function setPrint
// finds union of Integer sets
IntegerSet IntegerSet::unionOfIntegerSets( const IntegerSet &r )
{
IntegerSet temp( size > r.size ? size : r.size );
temp.emptySet();
int iterations = ( size < r.size ? size : r.size );
for ( int i = 0; i < iterations; i++ )
if ( set[ i ] == 1 || r.set[ i ] == 1 )
temp.set[ i ] = 1;
return temp;
} // end function unionOfIntegerSets
/* write definition for intersectionOfIntegerSets */
// insert element into set
void IntegerSet::insertElement( int k )
{
if ( validEntry( k ) )
set[ k ] = 1;
else
cout << "Invalid insert attempted!\n";
} // end function insertElement
/* write definition for deleteElement */
/* write definition for isEqualTo */
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -