I need to modify this class so that integers in a range of -M to +M can be represented. M should be as large as possible. The class should use one data member, an int or long. I need to reserve several of the extreme values that can be stored in the data member as special values like UNKNOWN and TOO_BIG. I have to decide how many special values are needed and use values larger than +M or smaller than -M to store these special values. I'm not really sure where to start. Any advice?
#ifndef SIMPLENUM_H_
#define SIMPLENUM_H_
#include <iostream>
#include <exception>
#include <limits>
class SimpleNum
{
public:
// standard methods
//------------------
// post: this SimpleNum is UNKNOWN
SimpleNum();
// post: this SimpleNum is a copy of other
SimpleNum( const SimpleNum & other );
// post: this SimpleNum is UNKNOWN
~SimpleNum();
// post: this SimpleNum is a copy of other
const SimpleNum & operator=( const SimpleNum & other );
// other constructors
//--------------------
// post: this SimpleNum has the value x
// note: exception raised if x is negative
SimpleNum( constint & x );
// accessors
//-----------
// post: this SimpleNum has been printed to outfile
void
print( std::ostream & outfile ) const;
// post: true has been returned just if this SimpleNum is
// valid
bool
isValid() const;
// mutators
//----------
// pre: the value of this SimpleNum is n
// post: the value of this SimpleNum is n-other
// note: exception raised if n-other is negative
const SimpleNum &
operator-=( const SimpleNum & other );
private:
// data members
long store_;
};
// auxilliary functions
//----------------------
// post: number has been written to outfile
std::ostream &
operator<<( std::ostream & outfile, const SimpleNum & number );
// post: lhs - rhs has been returned
// note: exception raised if lhs-rhs is negative
// exception raised if either parameter is UNKNOWN
SimpleNum
operator-( const SimpleNum & lhs, const SimpleNum & rhs );
// Method definitions
//--------------------
// post: this SimpleNum is UNKNOWN
SimpleNum::SimpleNum()
: store_( -1 )
{
}
// post: this SimpleNum is a copy of other
// note: exception raised if other is INVALID
SimpleNum::SimpleNum( const SimpleNum & other )
{
if( other.isValid() )
{
store_ = other.store_;
}
else
{
throw std::exception::exception( "INVALID parameter in copy constructor" );
}
}
// post: this SimpleNum is KNKNOWN
SimpleNum::~SimpleNum()
{
store_ = -1;
}
// post: this SimpleNum is a copy of other
// note: exception raised if other is INVALID
const SimpleNum &
SimpleNum::operator=( const SimpleNum & other )
{
if( ! other.isValid() )
{
throw std::exception::exception( "INVALID parameter in assignment operator" );
}
if( this != & other )
{
store_ = other.store_;
}
return *this;
}
// post: this SimpleNum has the value x
// note: this SimpleNum is TOO_SMALL if x is negative
SimpleNum::SimpleNum( constint & x )
{
if( x < 0 )
{
// x is TOO_SMALL
store_ = - 2;
}
else
{
store_ = x;
}
}
// post: this SimpleNum has been printed to outfile
// note: invalid numbers are printed as special strings
void
SimpleNum::print( std::ostream & outfile ) const
{
if( store_ == -1 )
{
outfile << "#UNK";
}
elseif( store_ == - 2 )
{
outfile << "#NEG";
}
else
{
outfile << store_;
}
}
// post: true has been returned just if this SimpleNum is valid
bool
SimpleNum::isValid() const
{
return store_ >= 0 ;
}
// pre: the value of this SimpleNum is n
// post: the value of this SimpleNum is n-other
// note: exception raised if this SimpleNum or other is invalid
// exception raised if n-other is negative
const SimpleNum &
SimpleNum::operator-=( const SimpleNum & other )
{
if( isValid() && other.isValid() )
{
if( store_ >= other.store_ )
{
store_ -= other.store_;
}
else
{
// The sum is NEGATIVE
throw std::exception::exception( "NEGATIVE result operator-=" );
}
}
else
{
throw std::exception::exception( "INVALID number in operator-=" );
}
return *this;
}
// post: number has been written to outfile
std::ostream &
operator<<( std::ostream & outfile, const SimpleNum & number )
{
number.print( outfile );
return outfile;
}
// post: lhs - rhs has been returned
// note: exception raised if lhs-rhs is negative
// exception raised if either parameter is UNKNOWN
SimpleNum
operator-( const SimpleNum & lhs, const SimpleNum & rhs )
{
SimpleNum diff( lhs );
diff -= rhs;
return diff;
}
#endif