### Safer Number Class

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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222`` ``````#ifndef SIMPLENUM_H_ #define SIMPLENUM_H_ #include #include #include 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( const int & 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( const int & 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"; } else if( 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 ``````
Last edited on
After doing some research...I've realized that this is suppose to be the BigInt program. Still not really sure where to start though...
Topic archived. No new replies allowed.