Solved

Operator overloading

Posted on 2009-03-30
1
371 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now