/* Run this program at the command line with 2 parameters that would represent the numbers. This should handle numbers of any length. This version does not handle decimal or negative numbers or any wrong input. */ using namespace std; #include <iostream> #include <vector> const int PARTLENGTH = 8; typedef vector<unsigned long> LongVec; //Function Declarations string multiply(string, string); // Multiply 2 numbers string removeLeadingZeros(string); // Remove leading zeros from a string long tenpower(int); // Function for getting 10 ^ value passed int numparts(string); // Find out how many sections the string has void fillVector(LongVec &, string); // Inserts the relevant values into the vector void multiply(LongVec &, LongVec &, int); // Multiply a vector with a digit and return it in another vector void adjust(LongVec &); // Adjust the arithmetic overflow of a number string getNumber(LongVec &); // Traverse through vector and append to buffer //End Function Declarations int main(int argc, char *argv[]) { //First 2 command-line arguments constitute the numbers string strFirst = removeLeadingZeros(string(argv[1])); string strSecond = removeLeadingZeros(string(argv[2])); cout << "The product is " << multiply(strFirst, strSecond) << endl; return 0; } string multiply(string strFirst, string strSecond) { int intFirstParts = numparts(strFirst); LongVec lvFirst (intFirstParts), lvTotal (intFirstParts + numparts(strSecond) + 2); fillVector(lvFirst, strFirst); int intSecondLength = strSecond.length(); for (int i = 0; i < intSecondLength; i++) { multiply(lvTotal, lvFirst, strSecond[i] - '0'); adjust(lvTotal); } return getNumber(lvTotal); } void multiply(LongVec &lvTotal, LongVec & lvOperand, int intDigit) { LongVec::iterator itOperand, itTotal; for (itOperand = lvOperand.begin(), itTotal = lvTotal.begin(); itTotal != lvTotal.end(); itTotal++) { *itTotal *= 10; if (itOperand != lvOperand.end()) *itTotal += ((*(itOperand++)) * intDigit); } } void adjust(LongVec & lvList) { long lngRemainder = 0; long lngDivider = tenpower(PARTLENGTH); for (LongVec::iterator itList = lvList.begin(); itList != lvList.end(); itList++) { *itList += lngRemainder; lngRemainder = *itList / lngDivider; *itList %= lngDivider; } } string getNumber(LongVec & lvList) { string strBuffer = ""; char strPrintfFormat[10]; sprintf(strPrintfFormat, "%%0%dld\0", PARTLENGTH); for (LongVec::reverse_iterator itList = lvList.rbegin(); itList != lvList.rend(); itList++) { char strPartValue[10]; sprintf(strPartValue, strPrintfFormat, *itList); strBuffer.append(strPartValue); } return removeLeadingZeros(strBuffer); } void fillVector(LongVec & lvHead, string strDigits) { string::reverse_iterator itDigits = strDigits.rbegin(); for (LongVec::iterator itHead = lvHead.begin(); itHead != lvHead.end(); itHead++) { *itHead = 0; for (int j = 0; j < PARTLENGTH && itDigits != strDigits.rend(); j++, itDigits++) { (*itHead) += (*itDigits - '0') * tenpower(j); } } } long tenpower(int intExponent) { return (long) pow(10.0, (double) intExponent); } string removeLeadingZeros(string strValue) { int intNonZeroPos = strValue.find_first_of(string("123456789")); if (intNonZeroPos == string::npos) return string("0"); else { int intLength = strValue.length(); return strValue.substr(intNonZeroPos, intLength - intNonZeroPos); } } int numparts(string strValue) { return (int) ceil(((double) strValue.length()) / PARTLENGTH); }
Copyright © 1999-2008 Krishna Kumar, All Rights Reserved.