Solved

Operator overloading

Posted on 2009-03-30
1
373 Views
Last Modified: 2013-12-14
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;
 
 
 
 
}

Open in new window

0
Comment
Question by:jmckennon
1 Comment
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24049089
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 154
I can't set a breakpoint ? 5 124
Sync project in eclipse locally 2 127
Change local server setting in php 6 87
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question