// big_integer.h # include
Using namespace std;
class NumberFormatException {private: string str; public: NumberFormatException (string); friend ostream & operator << (ostream &, const NumberFormatException &);}; class DividedByZeroException {private: string str; public: DividedByZeroException (string); friend ostream & operator << ( Ostream &, Const DividEzeroException &);
}; class big_integer {private: Vector
big_integer (const big_integer &); big_integer operator = (const big_integer & op2) {digits = op2.digits; sign = op2.sign; return (* this);} big_integer abs () const {if (sign) return * this; else return - (* this);
// binary operators
friend big_integer operator = (big_integer &, const big_integer &); friend big_integer operator - = (big_integer &, const big_integer &); friend big_integer operator * = (big_integer &, const big_integer &); friend big_integer operator / = (big_integer &, const big_integer &) throw (DividedByZeroException) ; friend big_integer operator% = (big_integer &, const big_integer &) throw (DividedByZeroException); friend big_integer operator (const big_integer &, const big_integer &); friend big_integer operator- (const big_integer &, const big_integer &); friend big_integer operator * (const big_integer &, const big_integer & ); friend big_integer operator / (const big_integer &, const big_integer &) throw (DividedByZeroException); friend big_integer operator% (const big_integer &, const big_integer &) throw (DividedByZeroException);
// unialess operatrs friend big_integer operator- (const big_integer &); // negative
friend big_integer operator (big_integer &); // v friend big_integer operator (big_integer &, int); // v friend big_integer operator - (big_integer &); // - v friend big_integer operator - (big_integer &, int); / / v--
friend bool operator> (const big_integer &, const big_integer &); friend bool operator <(const big_integer &, const big_integer &); friend bool operator == (const big_integer &, const big_integer &);! friend bool operator = (const big_integer &, const big_integer &); friend bool operator> = (const big_integer &, const big_integer &); friend bool operator <= (const big_integer &, const big_integer &); friend ostream & operator << (ostream &, const big_integer &); // print the big_integer}; const big_integer ZERO (0 ); const big_integer one (1); // Big_integer.cpp # include #include
Using namespace std;
NumberFormatException :: NumberFormatException (string s): str (s) {} ostream & operator << (ostream & stream, const NumberFormatException & exception) {stream << exception.str; return stream;}
DividedByZeroException :: DividedByZeroException (string s): str (s) {} ostream & operator << (ostream & stream, const DividedByZeroException & exception) {stream << exception.str; return stream;}
BIG_INTEGER :: BIG_INTEGER (LONG VAL) {// Construct with a long integer if (val> = 0) {siGN = true;} else {sign = false; val * = (-1);} do {Digits.push_back (char) (VAL% 10)); VAL / = 10;} while (val! = 0); Trim ();} void big_integer :: trim () {Vector
BIG_INTEGER :: BIG_INTEGER (STRING & DEF) THROW (NUMBERFORMATEXCEPTION) {
For (string :: reverse_iterator it = def.rbegin (); it
// binary operatorbig_integer operator = (BIG_INTEGER & OP1, Const Big_INTEGER & OP2) {if (!! op1.sign == !! op2.sign) {// only handle the same symbol, the same number is given - Processing Vector < Char> :: item :d1; vector
} BIG_INTEGER OPERATOR - = (BIG_INTEGER & OP1, Const Big_integer & op2) {IF (OP2 If (!! op1.sign == !! op2.sign) {// only processes the same symbol, the other number is given processing Vector While (iTer1! = op1.digits.end () &&im2! = op2.digits.end ()) {(* iter1) = (* iter1) - (* iter2) - to_substract; to_substract = 0; while ((* Iter1) <0) {TO_SUBSTRACT ; (* ITER1) = 10;} Iter1 ; iTer2 ;} while (i11! = op1.digits.end ()) {(* iTer1) = (* iter1) - to_substract; to_substract = 0; while (* iter1) <0) {TO_SUBSTRACT ; (* ITER1) = 10;}}} t1 ;} op1.trim (); return /} else {if (op1> zero) {RETURN OP1 = -OP2);} else {return OP1 = - (OP2 (-OP1));}}} BIG_INTEGER OPERATOR * = (BIG_INTEGER & OP1, Const Big_INTEGER & OP2) {BIG_INTEGER RESULT (0); if (op1 == Zero || Op2 == ZERO) {result = zero;} else {Vector INT NUM_OF_ZEROS = ITER2 - OP2.DIGITS.BEGIN (); while (num); num_of_zeros ---;} BIG_INTEGER TEMP2 (0); temp2.digits.pop_back (); Temp2.digits.insert (Temp2.digits.end (), Temp.begin (), Temp.end ()); Temp2.trim (); Result = Result Temp2;}}}}} result.sign = ((OP1 .sign && op2.sign) || (op1.sign && op2.sign));!!} op1 = result; return op1;} big_integer operator / = (big_integer & op1, const big_integer & op2) throw (DividedByZeroException) {if ( OP2 == ZERO) {Throw DividedByzeroException ("Responsion 0");} BIG_INTEGER T1 = op1.abs (), T2 = op2.abs (); if (T1 BIG_INTEGER TEMP2 (0); While (iter! = t1.digits.rend ()) {temp2 = Temp2 * Big_integer (10) BIG_INTEGER (* iter)); char s = '/ 0'; while TEMP2> = T2) {TEMP2 = TEMP2 - T2; S = S 1;} Temp.push_front (s); it ;} op1.digits.clear (); op1.digits.insert () , temp.begin (), temp1.trim (); op1.sign = ((op1.sign &&ot2.sign) || (! Op1.sign &&! op2.sign)); Return op1;} big_integer operator% = (big_integer & op1, const big_integer & op2) throw (DividedByZeroException) {return op1 - = ((op1 / op2) * op2);} big_integer operator (const big_integer & op1, const big_integer & op2) {big_integer temp ( OP1); TEMP = OP2; RETURN TEMP;} BIG_INTEGER OPERATOR- (Const Big_INTEGER & OP1, Const Big_INTEGER & OP2) {BIG_INTEGER TEMP (OP1); Temp - = OP2; Return Temp; BIG_INTEGER OPERATOR * (const Big_integer & op2) {BIG_INTEGER TEMP (OP1); TEMP * = OP2; RETURN TEMP; } BIG_INTEGER OPERATOR / (const BIG_INTEGER & OP1, Const Big_INTEGER & OP2) {BIG_INTEGER TEMP (OP1); TEMP / = OP2; Return Temp; BIG_INTEGER OPERATOR% (Const Big_integer & op2) throw (DividedByzeroException) {BIG_INTEGER TEMP (OP1); Temp% = OP2; Return Temp;} // uniary operatorbig_integer operator- (const big_integer & op) {// negative big_integer temp = BIG_INTEGER (OP); Temp.sign =! Temp.sign; return temp;} big_integer operator (big_integer & op) {// v op = ONE; return op;} big_integer operator (big_integer & op, int x) {// v big_integer temp (op); op; return temp;} big_integer operator - (BIG_INTEGER & OP) {// - v op - = - one; return op;} BIG_INTEGER OPERATOR - (BIG_INTEGER & OP, INT X) {// V - BIG_INTEGER TEMP (OP); --OP; Return Temp; Bool Operator <(const big_integer & op1, const big_integer & op2) {if (!! op1.sign! = !! op2.sign) Return! op1.sign && op2.sign; else {if (op1.digits.size ()! = op2.digits.size ()) Return (Op1.sign && op1.digits.size () Ostream & Operator << (Ostream & Stream, Const Big_INTEGER & VAL) {// print the big_integer if (! val.sign) {stream << "-";} for (Vector Return 0;}