[Original] Recently learned STL, don't find the Biginteger class in the C ++ library, so I wrote one

xiaoxiao2021-03-06  69

// big_integer.h # include #include #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 Digits; // Bool Sign; // True for Positive, False for Negitive Void Trim (); // Remove Zeros In Tail, But If The Value IS 0, Keep Only ONE: ) public:. explicit big_integer (long); // construct with a long integer explicit big_integer (string &) throw (NumberFormatException); // construct with a string, which must be all integers-bits // a sign can also be included if The Format Is Wrong, this Will Generate An Exception

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 #include #include #include #include #include # INCLUDE "BIG_INTEGER.H"

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 :: reverse_iterator it = digits.rbegin WHILE (Iter! = Digits.rend () && (* ipop_back () {DIGITS.POP_BACK (); Iter ;} IF (Digits.Size () == 0) {sign = true; Digits.push_back ('/ 0');}} // Construct with a string, Which Must Be All Integers-Bits // A Sign Can Also BE Included. If The Format Is Wrong, this Will Generate An Exception

BIG_INTEGER :: BIG_INTEGER (STRING & DEF) THROW (NUMBERFORMATEXCEPTION) {

For (string :: reverse_iterator it = def.rbegin (); it '9') {throw Numberformatexception (String ("Integer Format is not right");} Digits.push_back ((* iTer) - '0'));} f (Digits.empty ()) {throw numberFormatexception (String " Not right ");} Trim ();} BIG_INTEGER :: BIG_INTEGER (const Big_integer & op2): DIGITS (OP2.DIGITS) {Sign = op2.sign;}

// 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 :: const_iterator iter2; itr1 = op1.digits.begin (); item2 = op2.digits.begin (); char to_add = 0; // Give while (ore1! = OP1 .digits.end () &&im2! = op2.digits.end ()) {(* iter1) = (* iter1) (* iter2) to_add; to_add = (* iter1) / 10; (* iTer1) = (* ITER1)% 10; Iter1 ; Iter2 ;} while (ip1! = op1.digits.end ()) {(* iter1) = (* iter1) TO_ADD; to_ADD = (* ITER1) / 10; (* iTer1 )% = 10; iter1 ;} while (item2! = Op2.digits.end ()) {char val = (* iter2) to_add; to_add = VAL / 10; VAL% = 10; op1.digits.push_back (VAL ); ITer2 ;} if (to_add! = 0) {op1.digits.push_back (to_ad d);} Return OP1;} else {if (op1> zero) {return OP1 - = (-OP2);} else {return OP1 = (OP2 - (-OP1));}}

} 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 :: item iTer1; Vector :: const_iterator iter2 ITer1 = op1.digits.begin (); item2 = op2.digits.begin (); char to_substract = 0; // borrow

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 :: const_iterator iter2 = op2.digits.begin (); while (ip2! = op2.digits.end ()) { IF (* iTer2! = 0) {Deque Temp (op1.digits.begin (), op1.digits.end ()); char to_add = 0; Deque :: item iTer1 = Temp.Begin ); While (ore1! = Temp.end ()) {(* iter1) * = (* iter2); (* iter1) = to_ADD; to_ADD = (* ITER1) / 10; (* iTer1)% = 10; iTer1 ;} if (to_add! = 0) TEMP.PUSH_BACK (TO_ADD);

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 T2> 0 // Simply hand over the results of T1 / T2 to Result, you can deque temp; vector :: reverse_iterator it = t1.digits.rbegin ();

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 () op2.digits .size ()); // The length is also the same as a vector :: const_reverse_iterator iter1, item2; iter1 = op1.digits.rbegin (); item2 = op2.digits.rbegin (); while (Iter1! = op1.digits.rend ()) {if (op1.sign && * iter1 <* iter2) return true; if (! Op1.sign && * iter1> * iter2) Return True; Iter1 ; iTer2 ;} return false;} } Bool Operator == (const BIG_INTEGER & OP1, Const Big_integer & op2) {if (!! op1.sign! = !! op2.sign || op1.digits.size ()! = op2.digits.size () {Return False;} vector :: const_iterator iter1, iter2; itr1 = op1.digits.begin (); item2 = op2.digits.begin (); while (ore1! = op1.digits.end ()) {i f (* iTer1! = * ITER2) ​​Return False; Iter1 ; Iter2 ;} reburn true;} Bool Operator! = (const BIG_INTEGER & OP1, Const Big_integer & op2) {Return! (op1 == OP2);} Bool Operator> = Const Big_integer & op1, const BIG_INTEGER & OP2) {RETURN (OP1> OP2) || (OP1 == OP2);} Bool Operator <= (const BIG_INTEGER & OP1, Const Big_integer & op2) {RETURN (OP1 (const Big_integer & op1, const big_integer & op2) {return! (OP1 <= OP2);}

Ostream & Operator << (Ostream & Stream, Const Big_INTEGER & VAL) {// print the big_integer if (! val.sign) {stream << "-";} for (Vector :: const_reverse_iterator iter = val.digits.rbegin (); it! = val.digits.rend (); iter ) {stream << (char) ((* iter) '0');} Return Stream;} // Test_big_integer.cpp // Test Program #include "BIG_INTEGER.CPP" #include #include #include

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-110561.html

New Post(0)