// Arbitrarily large numbers // // A LargeNumber object should from any external point store its unsigned // number in positional code in the range [0..sig] of its number array. // This code should always lack leading 0s except when representing zero itself. // If the represented number is negative then the negative flag should be set // otherwise it should not be set. The max should be equal to one less than // the number array size and sig should never be greater than max. // // Author Matthew Caryl // Created 13.3.97 #include class LargeNumber { public: LargeNumber(void); LargeNumber(long n); LargeNumber(const LargeNumber& n); ~LargeNumber(void); char to_char(void) const; short to_short(void) const; int to_int(void) const; long to_long(void) const; bool even(void) const; bool odd(void) const; int length(void) const; // in bits bool bit(unsigned int p) const; // p'th bit (from zero) bool zero() const; // is zero bool sign(void) const; // is negative void truncate(int n); // reduce to lower n bits void complement(void); // * -1 bool operator==(const LargeNumber& n) const; bool operator!=(const LargeNumber& n) const; bool operator<(const LargeNumber& n) const; bool operator<=(const LargeNumber& n) const; bool operator>(const LargeNumber& n) const; bool operator>=(const LargeNumber& n) const; LargeNumber& operator=(const LargeNumber& n); LargeNumber& operator+=(const LargeNumber& n); LargeNumber& operator-=(const LargeNumber& n); LargeNumber& operator<<=(int n); LargeNumber& operator>>=(int n); LargeNumber& operator++(void); LargeNumber& operator--(void); LargeNumber operator++(int); LargeNumber operator--(int); LargeNumber& operator*=(const LargeNumber& n); LargeNumber& operator/=(const LargeNumber& n); LargeNumber& operator%=(const LargeNumber& n); // no change of sign for following operators LargeNumber& operator&=(const LargeNumber& n); LargeNumber& operator|=(const LargeNumber& n); LargeNumber& operator^=(const LargeNumber& n); LargeNumber operator+(const LargeNumber& n) const; LargeNumber operator-(const LargeNumber& n) const; LargeNumber operator*(const LargeNumber& n) const; LargeNumber operator/(const LargeNumber& n) const; LargeNumber operator%(const LargeNumber& n) const; // no change of sign for following operators LargeNumber operator&(const LargeNumber& n) const; LargeNumber operator|(const LargeNumber& n) const; LargeNumber operator^(const LargeNumber& n) const; LargeNumber operator<<(int n) const; LargeNumber operator>>(int n) const; friend ostream& operator<<(ostream& s, const LargeNumber& n); friend istream& operator>>(istream& s, LargeNumber& n); private: char* number; bool negative; int sig; int max; // unsigned operators bool smaller(const LargeNumber& n) const; // unsigned bool greater(const LargeNumber& n) const; // unsigned void expand(int n); // ensure n'th bit exits void contract(); // remove leading 0s void plus(const LargeNumber& n); // unsigned void minus(const LargeNumber& n); // unsigned };