1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #ifndef _BIGINT_H
#define _BIGINT_H
// author: Owen Astrachan
// 8/23/95, modified 7/5/96
//
// implements an arbitrary precision integer class
//
// constructors:
//
// BigInt() -- default constructor, value of integers is 0
// BigInt(int n) -- initialize to value of n (C++ int)
// BigInt(const string & s) -- initialize to value specified by s
// it is an error if s is an invalid integer, e.g.,
// "1234abc567". In this case the bigint value is garbage
// BigInt(const BigInt & b) -- copy constructor
//
//
// ***** arithmetic operators:
//
// all arithmetic operators +, -, *, /, % are overloaded both
// in form +=, -=, *=, /=, %=, and as binary operators
//
// multiplication and division also overloaded for *= int and /= int
// e.g., BigInt a *= 3 (mostly to facilitate implementation)
//
// ***** logical operators:
//
// bool operator == (const BigInt & lhs, const BigInt & rhs)
// bool operator != (const BigInt & lhs, const BitInt & rhs)
// bool operator < (const BigInt & lhs, const BigInt & rhs)
// bool operator <= (const BigInt & lhs, const BigInt & rhs)
// bool operator > (const BigInt & lhs, const BigInt & rhs)
// bool operator >= (const BigInt & lhs, const BigInt & rhs)
//
// ***** I/O operators:
//
// ostream & Print()
// prints value of BigInt (member function)
// ostream & operator << (ostream & os, const BigInt & b)
// stream operator to print value
//
#include <iostream.h>
#include <string> // for strings
#include "apvector.h" // for sequence of digits
class BigInt
{
public:
BigInt(); // default constructor, value = 0
BigInt(int); // assign an integer value
BigInt(const string &); // assign a string
BigInt(const BigInt &); // copy constructor
~BigInt(); // destructor
ostream & Print(ostream & os) const; // so << isn't a friend
BigInt & operator = (const BigInt &);
// operators: arithmetic, relational
BigInt & operator += (const BigInt &);
BigInt & operator -= (const BigInt &);
BigInt & operator *= (const BigInt &);
BigInt & operator *= (int num);
BigInt & operator /= (const BigInt &);
BigInt & operator /= (int num);
BigInt & operator %= (const BigInt &);
string toString() const; // convert to string
friend bool operator == (const BigInt &, const BigInt &);
friend bool operator < (const BigInt &, const BigInt &);
private:
enum BigError{
underflow, overflow, not_a_number,infinity,no_error
};
// private state/instance variables
bool myIsNegative;
apvector<char> myDigits;
int myNumDigits;
BigError myError;
// helper functions
bool IsNeg() const; // return true iff number is negative
int getDigit(int k) const;
void addSigDigit(int value);
void changeDigit(int digit, int value);
int size() const;
void Normalize();
void DivideHelp(const BigInt & lhs, const BigInt & rhs,
BigInt & quotient, BigInt & remainder);
};
// free functions
ostream & operator <<(ostream &, const BigInt &);
istream & operator >>(istream &, BigInt &);
BigInt operator +(const BigInt & lhs, const BigInt & rhs);
BigInt operator -(const BigInt & lhs, const BigInt & rhs);
BigInt operator *(const BigInt & lhs, const BigInt & rhs);
BigInt operator *(const BigInt & lhs, int num);
BigInt operator *(int num, const BigInt & rhs);
BigInt operator /(const BigInt & lhs, const BigInt & rhs);
BigInt operator /(const BigInt & lhs, int num);
BigInt operator %(const BigInt & lhs, const BigInt & rhs);
bool operator != (const BigInt & lhs, const BigInt & rhs);
bool operator > (const BigInt & lhs, const BigInt & rhs);
bool operator >= (const BigInt & lhs, const BigInt & rhs);
bool operator <= (const BigInt & lhs, const BigInt & rhs);
#endif // _BIGINT_H not defined |