### BigInt

I need to write a big int class starting from these two files that tests <, !=, -=, -, *=, and *. I really don't even know where to begin. Anyone have any advice?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221`` ``````#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; // mutator //---------- // 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 ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` ``````#include "SimpleNum01.h" #include using namespace std; int main() { SimpleNum x; cout << x << endl; SimpleNum neg( -1234 ); cout << neg << endl; x = 24; cout << x << endl; x = 204; x -= 107; cout << x << endl; while( true ) { cout << endl << "Enter two values: "; long one, two; cin >> one >> two; cout << "diff = snone - sntwo is "; try { SimpleNum snone( one ); SimpleNum sntwo( two ); SimpleNum diff = snone - sntwo; cout << diff << endl; } catch ( std::exception & ex ) { cout << ex.what() << endl; } cout << "snone - sntwo is "; try { SimpleNum snone( one ); SimpleNum sntwo( two ); //SimpleNum diff = snone - sntwo; cout << snone - sntwo << endl; } catch ( std::exception & ex ) { cout << ex.what() << endl; } cout << "snone - two is "; try { SimpleNum snone( one ); SimpleNum sntwo( two ); //SimpleNum diff = snone - sntwo; cout << snone - two << endl; } catch ( std::exception & ex ) { cout << ex.what() << endl; } cout << "one - sntwo is "; try { SimpleNum snone( one ); SimpleNum sntwo( two ); //SimpleNum diff = snone - sntwo; cout << one - sntwo << endl; } catch ( std::exception & ex ) { cout << ex.what() << endl; } } return 0; }``````
Topic archived. No new replies allowed.