📄 classes.cpp
字号:
{ return ! ( *this == rhs ); }
void Pell::
set_position( int pos )
{
if ( pos <= 0 || pos > _max_elems ){
cerr << "!! invalid position: " << pos
<< " setting pos to default value of 1\n"
<< "If inadequate, invoke set_position(pos)\n";
pos = 1;
}
_beg_pos = pos;
}
void Pell::
set_length( int len )
{
if ( len <= 0 || len + _beg_pos - 1 > _max_elems ){
cerr << "!! invalid length for this object: " << len
<< " setting length to default value of 1\n"
<< "If inadequate, invoke set_length(len)\n";
len = 1;
}
_length = len;
}
bool Triangular::
operator==( const num_sequence &rhs ) const
{
return ( _beg_pos == rhs.beg_pos() ) &&
( _length == rhs.length() );
}
bool Triangular::
operator !=( const num_sequence &rhs ) const
{ return ! ( *this == rhs ); }
void Triangular::
set_position( int pos )
{
if ( pos <= 0 || pos > _max_elems ){
cerr << "!! invalid position: " << pos
<< " setting pos to default value of 1\n"
<< "If inadequate, invoke set_position(pos)\n";
pos = 1;
}
_beg_pos = pos;
}
void Triangular::
set_length( int len )
{
if ( len <= 0 || len + _beg_pos - 1 > _max_elems ){
cerr << "!! invalid length for this object: " << len
<< " setting length to default value of 1\n"
<< "If inadequate, invoke set_length(len)\n";
len = 1;
}
_length = len;
}
bool Square::
operator==( const num_sequence &rhs ) const
{
return ( _beg_pos == rhs.beg_pos() ) &&
( _length == rhs.length() );
}
bool Square::
operator !=( const num_sequence &rhs ) const
{ return ! ( *this == rhs ); }
void Square::
set_position( int pos )
{
if ( pos <= 0 || pos > _max_elems ){
cerr << "!! invalid position: " << pos
<< " setting pos to default value of 1\n"
<< "If inadequate, invoke set_position(pos)\n";
pos = 1;
}
_beg_pos = pos;
}
void Square::
set_length( int len )
{
if ( len <= 0 || len + _beg_pos - 1 > _max_elems ){
cerr << "!! invalid length for this object: " << len
<< " setting length to default value of 1\n"
<< "If inadequate, invoke set_length(len)\n";
len = 1;
}
_length = len;
}
bool Pentagonal::
operator==( const num_sequence &rhs ) const
{
return ( _beg_pos == rhs.beg_pos() ) &&
( _length == rhs.length() );
}
bool Pentagonal::
operator !=( const num_sequence &rhs ) const
{ return ! ( *this == rhs ); }
void Pentagonal::
set_position( int pos )
{
if ( pos <= 0 || pos > _max_elems ){
cerr << "!! invalid position: " << pos
<< " setting pos to default value of 1\n"
<< "If inadequate, invoke set_position(pos)\n";
pos = 1;
}
_beg_pos = pos;
}
void Pentagonal::
set_length( int len )
{
if ( len <= 0 || len + _beg_pos - 1 > _max_elems ){
cerr << "!! invalid length for this object: " << len
<< " setting length to default value of 1\n"
<< "If inadequate, invoke set_length(len)\n";
len = 1;
}
_length = len;
}
// is_elem() returns true if the element passed in a valid element
// in the object's sequence. For example, if the object represents
// a fibonacci sequence { 1,1, 2, 3, 5, 8, 13, 21, 34, 45}, beginning
// as position 3 for a length of 2, then
// Obj.is_elem( 1 ); // false
// Obj.is_elem( 3 ); // true
// Obj.is_elem( 5 ); // false
// Obj.is_elem( 4 ); // false
bool Fibonacci::
is_elem( unsigned int elem ) const
{
if ( ! check_integrity( _beg_pos, _length ))
return false;
return binary_search( begin(), end(), elem );
}
bool Pell::
is_elem( unsigned int elem ) const
{
if ( ! check_integrity( _beg_pos, _length ))
return false;
return binary_search( begin(), end(), elem );
}
bool Lucas::
is_elem( unsigned int elem ) const
{
if ( ! check_integrity( _beg_pos, _length ))
return false;
return binary_search( begin(), end(), elem );
}
bool Triangular::
is_elem( unsigned int elem ) const
{
if ( ! check_integrity( _beg_pos, _length ))
return false;
return binary_search( begin(), end(), elem );
}
bool Square::
is_elem( unsigned int elem ) const
{
if ( ! check_integrity( _beg_pos, _length ))
return false;
return binary_search( begin(), end(), elem );
}
bool Pentagonal::
is_elem( unsigned int elem ) const
{
if ( ! check_integrity( _beg_pos, _length ))
return false;
return binary_search( begin(), end(), elem );
}
// pos_elem() returns the position of an element within the sequence
// independent of what the object's position and length. The object
// simply is used to identify the sequence for which to return a position.
// If the element value is invalid, return 0. For example, if the object
// represents a fibonacci sequence, then
// Obj.pos_elem( 1 ); // returns 1 - ignore duplicate
// Obj.pos_elem( 32 ); // returns 9
// Obj.pos_elem( very-large-instance ); // your choice
// Obj.pos_elem( 4 ); // return 0
int Fibonacci::
pos_elem( unsigned int elem ) const
{
cout << "pos_elem( " << elem << " )\n";
iterator iter;
if ( _elems[ _elems.size()-1 ] < elem )
return _calc_pos( elem );
if (( iter = find( _elems.begin(), _elems.end(), elem ))
== _elems.end() )
return 0;
else return distance( _elems.begin(), iter )+1;
}
int Pell::
pos_elem( unsigned int elem ) const
{
cout << "pos_elem( " << elem << " )\n";
iterator iter;
if ( _elems[ _elems.size()-1 ] < elem )
return _calc_pos( elem );
if (( iter = find( _elems.begin(), _elems.end(), elem ))
== _elems.end() )
return 0;
else return distance( _elems.begin(), iter )+1;
}
int Lucas::
pos_elem( unsigned int elem ) const
{
cout << "pos_elem( " << elem << " )\n";
iterator iter;
if ( _elems[ _elems.size()-1 ] < elem )
return _calc_pos( elem );
if (( iter = find( _elems.begin(), _elems.end(), elem ))
== _elems.end() )
return 0;
else return distance( _elems.begin(), iter )+1;
}
int Triangular::
pos_elem( unsigned int elem ) const
{
cout << "pos_elem( " << elem << " )\n";
iterator iter;
if ( _elems[ _elems.size()-1 ] < elem )
return _calc_pos( elem );
if (( iter = find( _elems.begin(), _elems.end(), elem ))
== _elems.end() )
return 0;
else return distance( _elems.begin(), iter )+1;
}
int Square::
pos_elem( unsigned int elem ) const
{
cout << "pos_elem( " << elem << " )\n";
iterator iter;
if ( _elems[ _elems.size()-1 ] < elem )
return _calc_pos( elem );
if (( iter = find( _elems.begin(), _elems.end(), elem ))
== _elems.end() )
return 0;
else return distance( _elems.begin(), iter )+1;
}
int Pentagonal::
pos_elem( unsigned int elem ) const
{
cout << "pos_elem( " << elem << " )\n";
iterator iter;
if ( _elems[ _elems.size()-1 ] < elem )
return _calc_pos( elem );
if (( iter = find( _elems.begin(), _elems.end(), elem ))
== _elems.end() )
return 0;
else return distance( _elems.begin(), iter )+1;
}
int Fibonacci::
_calc_pos( unsigned int elem ) const
{
// presumption is that check_integrity() has passed
int pos = _elems.size()-1;
cout << "calc_pos invoked()!: elem: " << elem
<< " pos: " << pos
<< " at: " << _elems[ pos ]
<< "\n";
while (( pos < _max_elems ) &&
( _elems[ pos ] < elem ))
{
gen_elems( ++pos );
cout << " pos: " << pos
<< " at: " << _elems[ pos ] << endl;
}
return (( pos < _max_elems ) &&
( _elems[pos] == elem )) ? pos+1 : 0;
}
int Pell::
_calc_pos( unsigned int elem ) const
{
// presumption is that check_integrity() has passed
int pos = _elems.size()-1;
cout << "calc_pos invoked()!: elem: " << elem
<< " pos: " << pos
<< " at: " << _elems[ pos ]
<< "\n";
while (( pos < _max_elems ) &&
( _elems[ pos ] < elem ))
{
gen_elems( ++pos );
cout << " pos: " << pos
<< " at: " << _elems[ pos ] << endl;
}
return (( pos < _max_elems ) &&
( _elems[pos] == elem )) ? pos+1 : 0;
}
int Lucas::
_calc_pos( unsigned int elem ) const
{
// presumption is that check_integrity() has passed
int pos = _elems.size()-1;
cout << "calc_pos invoked()!: elem: " << elem
<< " pos: " << pos
<< " at: " << _elems[ pos ]
<< "\n";
while (( pos < _max_elems ) &&
( _elems[ pos ] < elem ))
{
gen_elems( ++pos );
cout << " pos: " << pos
<< " at: " << _elems[ pos ] << endl;
}
return (( pos < _max_elems ) &&
( _elems[pos] == elem )) ? pos+1 : 0;
}
int Triangular::
_calc_pos( unsigned int elem ) const
{
// presumption is that check_integrity() has passed
int pos = _elems.size()-1;
cout << "calc_pos invoked()!: elem: " << elem
<< " pos: " << pos
<< " at: " << _elems[ pos ]
<< "\n";
while (( pos < _max_elems ) &&
( _elems[ pos ] < elem ))
{
gen_elems( ++pos );
cout << " pos: " << pos
<< " at: " << _elems[ pos ] << endl;
}
return (( pos < _max_elems ) &&
( _elems[pos] == elem )) ? pos+1 : 0;
}
int Square::
_calc_pos( unsigned int elem ) const
{
// presumption is that check_integrity() has passed
int pos = _elems.size()-1;
cout << "calc_pos invoked()!: elem: " << elem
<< " pos: " << pos
<< " at: " << _elems[ pos ]
<< "\n";
while (( pos < _max_elems ) &&
( _elems[ pos ] < elem ))
{
gen_elems( ++pos );
cout << " pos: " << pos
<< " at: " << _elems[ pos ] << endl;
}
return (( pos < _max_elems ) &&
( _elems[pos] == elem )) ? pos+1 : 0;
}
int Pentagonal::
_calc_pos( unsigned int elem ) const
{
// presumption is that check_integrity() has passed
int pos = _elems.size()-1;
cout << "calc_pos invoked()!: elem: " << elem
<< " pos: " << pos
<< " at: " << _elems[ pos ]
<< "\n";
while (( pos < _max_elems ) &&
( _elems[ pos ] < elem ))
{
gen_elems( ++pos );
cout << " pos: " << pos
<< " at: " << _elems[ pos ] << endl;
}
return (( pos < _max_elems ) &&
( _elems[pos] == elem )) ? pos+1 : 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -