LongInt LongInt::longMult(LongInt& otherLongInt) { LongInt theAnswer = LongInt(0); if (!this->eqZero() && !otherLongInt.eqZero()) { int thisSize = (int) this->myLongInt.size(); int otherSize = (int) otherLongInt.myLongInt.size(); LongInt multTemp; bool isMultTempValued = false; for (int j = 0; j < otherSize; j++) { // j is the iterator through otherLongInt long long mult2 = otherLongInt.myLongInt[j]; multTemp.setZero_(); for (int k = 0; k <= j-1; k++) { multTemp.myLongInt.push_back(0); } for (int i = 0; i < thisSize; i++) { // i is the iterator through this long long mult1 = this->myLongInt[i]; long long multResult = mult1 * mult2; // There might be overflow from the previous multiplication multResult += multTemp.myLongInt[j+i]; if (!isMultTempValued && (multResult > 0)) isMultTempValued = true; // We check for overflow long exceed = multResult / DIVISOR; if (exceed > 0) multResult %= DIVISOR; // If overflow take remainder multTemp.myLongInt[j+i] = (long) multResult; if ((i != thisSize-1) || (exceed > 0)) multTemp.myLongInt.push_back(exceed); } if (isMultTempValued) multTemp.signType = type_positive; theAnswer = theAnswer + multTemp; } } return theAnswer; }
LongInt LongInt::minusHelper(LongInt& newNumber) { long carryover = 0L; LongInt theAnswer; theAnswer.signType = type_positive; int i; long resultant; if (myLongInt.size() == newNumber.myLongInt.size()){ if (*this > newNumber){ for (i = 0; i < newNumber.myLongInt.size(); i++){ if (myLongInt[i] >= (newNumber.myLongInt[i] + carryover)) { resultant = myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } } else { for (i = 0; i < newNumber.myLongInt.size(); i++){ if (newNumber.myLongInt[i] >= (myLongInt[i] + carryover)) { resultant = newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } theAnswer.signType = type_negative; } } else if (myLongInt.size() > newNumber.myLongInt.size()){ for (i = 0; i < newNumber.myLongInt.size(); i++){ if (myLongInt[i] >= (newNumber.myLongInt[i] + carryover)) { resultant = myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } for (int j = i; j < myLongInt.size(); j++){ if (carryover == 1L && myLongInt[j] == 0L) { resultant = DIVISOR - carryover; carryover = 1L; } else { resultant = myLongInt[j] - carryover; carryover = 0L; } theAnswer.myLongInt.push_back(resultant); } } else { for (i = 0; i < myLongInt.size(); i++){ if (newNumber.myLongInt[i] >= (myLongInt[i] + carryover)) { resultant = newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } for (int j = i; j < newNumber.myLongInt.size(); j++){ if (carryover == 1L && newNumber.myLongInt[j] == 0L) { resultant = DIVISOR - carryover; carryover = 1L; } else { resultant = newNumber.myLongInt[j] - carryover; carryover = 0L; } theAnswer.myLongInt.push_back(resultant); } theAnswer.signType = type_negative; } // removing zeros bool zero = true; for (int k = (theAnswer.myLongInt.size() - 1); k >= 0 && zero; k--) { if (theAnswer.myLongInt[k] != 0L) { zero = false; } else { theAnswer.myLongInt.erase(theAnswer.myLongInt.end() - 1); } } if (zero) { theAnswer.setZero_(); } return theAnswer; }