Start New Discussion within our Software Development Community

The function adds two large numeric string together, like "12345667" + "12345678" = ?

It does not have complete error checking, I'll leave that up to you.

It should work but I am prone to bugs, so do advise if bugs are found.

Included are also helper function that helps us add the large numeric string

#include <iostream>
#include <string>
#include <stack>

using namespace std;
//Acts as an helper function for large numeric string addition
void deleteLeadingZeros(string& num){

	//delete any starting 0's
	if(num[0] == '0'){
		unsigned int strtCpyIndex = 0;
		strtCpyIndex = num.find_first_not_of("0");
		string temp = num.substr(strtCpyIndex);
		num = temp;
	}
}
//adds leading 0's for the smaller string
void equalizeLength(string& num1 , string& num2, char pad = '0'){

	if(num1.size() < num2.size()){
		unsigned int diff = num2.size() - num1.size();
		string temp;
		while(diff--) //add starting zeros
			temp += pad;
		temp += num1;
		num1 = temp;
	}
	else if(num2.size() < num1.size()){
		unsigned int diff = num1.size() - num2.size();
		string temp;
		while(diff--) //add starting zeros
			temp += pad;
		temp += num2;
		num2 = temp;
	}
}

string addLargeNumbers(string num1, string num2)
{	

	//simple error check
	if(num1.empty())
		return num2;
	else if(num2.empty())
		return num1;
	
//check for valid information passed here if you want


	//holds out result of addition
	stack<short> addResult; 
	//addResult in string version
	string returnResult = "";	
	
	//delete any starting 0's	
	//example if num1 = 001
	//after the call below num = 1
	deleteLeadingZeros(num1);
	deleteLeadingZeros(num2);

	//match size by adding in leading 0's for the smaller number
	//example num1 = 100 and num2 = 50
	//after the call num1 = 100 and num2 = 050
	equalizeLength(num1,num2);
	
	int lastAddElem = num1.size()- 1;

	bool hasCarry = false;

	short result = 0; //holds result of each integer addition

	//start calculation
	for(int i = lastAddElem; i >= 0; --i)
	{
		result = (num1[i] - '0') + (num2[i] - '0') ; //convert to decimal and add
		
		if(result < 10 && !hasCarry)
			addResult.push(result);
		else
		{			
			if(hasCarry){
				result += 1; //account for the carry				
				hasCarry = result < 10 ? false : true;
				addResult.push(result%10);
			}
			else
			{
				hasCarry = true;
				addResult.push(result % 10 );
			}
		}

	}
	//check for highest bit carry
	if(hasCarry)
		addResult.push(result/10);

	//extract data
	while(addResult.size()){
		returnResult += (addResult.top() + '0');		
		addResult.pop();
	}

	return returnResult;
}

int main()
{		
	string num1,num2;
	while(true){
		cout<<"Enter 2 integer for addition : ";
		cin >> num1 >> num2;
		cout <<num1 << "+" << num2 <<" = "<<addLargeNumbers(num1,num2)<<endl;
	}
	
}

I forgot to mention that this adds 2 positive string numbers.

If you make a subtraction function, then you can easily modify the addition function to add negative numbers as well.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.