// BigUn.cpp // Ed Smith // Dr. England // CS142, SP 2003 // 4/26/03 // // interface for BigUn datatype: // designed to hold large unsigned values // #include "BigUn.h" // public toString Function string BigUn::toString (void){ return toString(bigNum); } // overloaded addition operator string BigUn::operator+ (BigUn& other){ unsigned a, b, tempLength, tempSum, carry = 0; SlistT sumList; if ((getLength()) > (other.getLength())) tempLength = getLength(); else tempLength = other.getLength(); bigNum.reset(); other.bigNum.reset(); for (unsigned i = 0; i < tempLength; i++){ if (!bigNum.get(a)) a = 0; if (!other.bigNum.get(b)) b = 0; cout << "a " << a << " b " << b << " carry " << carry << endl; tempSum = a + b + carry; carry = tempSum / 10; tempSum = tempSum % 10; sumList.insert(tempSum); sumList.move(); bigNum.move(); other.bigNum.move(); } if (!(carry == 0)) sumList.insert(carry); string s = toString(sumList); return s; } // overloaded assignment operator // assign other BigUn to BigUn BigUn& BigUn::operator= (BigUn& other){ string s = toString(other.bigNum); *this = s; return (*this); } // overloaded assignment opperator // assign string of digits to BigUn BigUn& BigUn::operator= (string strNum){ unsigned temp; bigNum.clean(); for (int i = 0; i < strNum.length(); i++){ temp = strNum[i] - '0'; bigNum.insert(temp); } return (*this); } // overloaded multiplication operator string BigUn::operator* (unsigned num){ SlistT multList; unsigned a, tempMult, carry = 0; bigNum.reset(); for (unsigned i = 0; i < getLength(); i++){ bigNum.get(a); tempMult = (a * num) + carry; carry = tempMult / 10; tempMult = tempMult % 10; cout << "a " << a << " num " << num << " carry " << carry << " tempMult " << tempMult << endl; multList.insert(tempMult); multList.move(); bigNum.move(); } while (!(carry == 0)){ multList.insert(carry % 10); carry = carry / 10; } string s = toString(multList); return (s); } // accessors unsigned BigUn::getLength (void) const{ return (bigNum.length()); } // print function void BigUn::print(void){ cout << toString (bigNum); } // convert the bigNum slist to a string string BigUn::toString (SlistT& list){ unsigned digit; string s; list.reset(); while (list.get(digit)){ s = (char) (digit + '0') + s; list.move(); } return s; }