i've been given a programming assignment to create a class called hugeint that can take up to 40 digit integers and do arithmatic operations on them.I'm having considerable difficulty with the problem and was hoping for a bit of help. My biggest issue right now is with the multiply operator. ill post what I have for the code so far, but i have to keep deleting and changing stuff as it does not work for large integers! Hope someone can point me in the right direction!

HugeInt HugeInt::operator *(HugeInt& rhs){ HugeInt temp; int x(0); int y(0); int z(0); int f(0); int digitCount(1); int timesAdded(1); int amountOverTen(0); char a; char b; char c; char parsedChar; long long addition(0); long long parsedInt; long long longInt; int parsedRhsVector(0); int parsedInputVector(0); int i(0); vector<long long> hugeVector; vector<int> rhsVector; vector<int> inputVector; vector<long long> multiplyVector; vector<int> otherMultiplyVector; vector<int>::iterator inputCounter; vector<int>::iterator rhsCounter; vector<long long>::iterator multiplyIterator; char Input [40]; char rhsInput[40]; for(i=40;i>=0;i--) { Input[i]=digits[i]; a=(Input[i]); x=a-'0'; rhsInput[i]=rhs.digits[i]; b=rhsInput[i]; y=b-'0'; //z=x*y; if(y>=0) { rhsVector.push_back(y); } if(x>=0) { inputVector.push_back(x); } } rhsCounter=rhsVector.begin(); while(rhsCounter< rhsVector.end()) { for(inputCounter=inputVector.begin(); inputCounter < inputVector.end(); inputCounter++) { parsedInputVector=*inputCounter; parsedRhsVector=*rhsCounter; z= (parsedInputVector * parsedRhsVector)+amountOverTen; amountOverTen=0; while(z>=10) { z=z-10; amountOverTen++; } stream<< z; if(inputCounter+1>= inputVector.end()) { z=amountOverTen; stream<<z; } } amountOverTen=0; inputCounter=inputVector.begin(); rhsCounter++; } for(multiplyIterator=multiplyVector.begin();multiplyIterator<multiplyVector.end();multiplyIterator++) { longInt=*multiplyIterator; addition=addition+longInt; } stringstream stream; string additionString; stream<<addition; additionString= stream.str(); for(i=0;i<additionString.length();i++) { parsedChar=additionString[i]; hugeVector.push_back(parsedChar); } vector<long long>:: iterator it; i=0; for ( it=hugeVector.begin() ; it < hugeVector.end(); it++ ) { parsedInt=*it; c=parsedInt; temp.digits[i]=c; i=i+1; } i=0; for(i=0;i<40;i++) { if(temp.digits[i]<=0) { temp.digits[i]='\0'; } } return temp;}

I have problems to understand your code which is far to complicated.

You should remember your basic school algorithms for multiplying numbers

123 * 98 =

1107
984
----------------
12054

and you will see that it would work with any count of digits.

So, you have to iterate on the digits of second HugeInt and multiply the first HugeInt operand by any digit number. You best add another operator* which takes a char and simply do

hugesums[i] = hugeop1 * hugeop1.getDigitAt(i);

where i is the current loop counter. Make i 0-based. The getDigitAt would return a char. e. g. '9' and I would count from the right, so that for i=0 you get the least significant digit.

To store the digits in HugeInt, don't use an array. A simple string member will do it.

hugesums in the above sample would be a

vector<HugeInt> hugesums(40, HugeInt(0));

With that the hugesums already is fully allocated and filled with huge integers of 0 value (the internal string is "0").

the operator*(HugeInt huge, char dig)

then would do a multiplication of dig with each digit of the huge operand, add the previous carry and add the remainder of mod10 to the result string. Then, save the new carry.

The total result is a string which could be turned to a HugeInt and be returned by value.

make another helper, which adds zero digits to the right of an HugeInt. You need it to multiply your partial sums by an arbitrary power of 10.

Finally use operator+ to calculate the total sum of the partial sums (hugesum) in aloop and the final result is waht you need as result of operator*.

0

Featured Post

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…