Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a class to calculate Big Numbers with strings.
The Add method isn't working. I think the logic is wrong. Any suggestions how to fix this? Thanks in advance.

C++
#include "NumberString.h"


NumberString::NumberString()
{
	this->_number = "0";
}

NumberString::NumberString(string s)
{
	this->_number = s;
}

NumberString::~NumberString()
{
}

void NumberString::Add(string number)
{
	string top = this->_number;
	string bottom = number;
	if (number.size() > top.size())
	{
		top = number;
		bottom = _number;
	}
	string sum;
	char t=0, b=0, l=0, c='0';
	size_t n = top.size();
	size_t m = bottom.size();
	for (size_t i = 0; i < n; i++)
	{
		t = top.at(n - i - 1);
		if (c != '0')
		{
			this->AddChars(t, c, &l, &c);
		}

		if (i < m)
		{
			b = bottom.at(m - i - 1);
			this->AddChars(t, b, &l, &c);
		}

		sum.insert(0, 1, l);		
	}

	if (c != '0')
	{
		sum.insert(0, 1, c);
	}

	this->_number = sum;
}

void NumberString::AddChars(char first, char second, char *last, char* carry)
{
	*carry = '0';
	char c = first + second - '0';
	if (c > '9')
	{
		*carry = '1';
		c -= 10;
	}
	*last = c;
}

void NumberString::Multiply(uint64_t number)
{
	NumberString n = this->_number;
	if (number == 0)
	{
		this->_number = "0";
		return;
	}
	else if (number == 1)
		return;

	for (uint64_t i = 0; i < number-1; i++)
	{
		this->Add(n);
	}
}

void NumberString::Print()
{
	cout << this->_number.c_str() << endl;
}

string NumberString::GetNumber()
{
	return this->_number;
}

void NumberString::Add(NumberString number)
{
	this->Add(number._number);
}
Posted
Comments
OriginalGriff 31-Mar-15 8:11am    
"isn't working. I think the logic is wrong"
That isn't very helpful - remember that we can't see your screen, access your HDD, or read your mind.
So tell us what you did to get results, tell us what that did do that you didn't expect, or did do that you didn't! Tell us if there are error messages, bad numbers, poor results. Give us information to help us help you!
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Your code doesn't properly propagate the carry that may occur here
Quote:
if (c != '0')
{
this->AddChars(t, c, &l, &c);
}


Moreover, it incorrectly always sums t with b (it should sum l with b when carry occurs).

In order to avoid cumbersome carry propagation, I would suggest to define
C++
char NumberString::AddCharsWithCarry( char first, char second, char & carry)
{
  char r = first - '0' + second - '0' + carry;
  if ( r > '9')
  {
    r -= 10;
    carry = '1';
  }
  else
  {
    carry = '0';
  }
  return r;
}


This method sums first, second and carry, returning the result (and updating the carry parameter). This way the loop inside your Add method would become very simple:
C++
for (size_t i = 0; i < n; i++)
{
  t = top.at(n - i - 1);
  b = i < m ? bottom.at(m -i -1) : '0';
  l = AddCharsWithCarry(t, b, c);

  sum.insert(0, 1, l);
}
 
Share this answer
 
Comments
Maciej Los 31-Mar-15 9:29am    
+5!
CPallini 31-Mar-15 11:47am    
Thank you. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900