All pastes #342435 Raw Edit

Something

public text v1 · immutable
·published 2007-02-06 08:55 UTC
rendered paste body
#include <iostream>
#include <string> 

using namespace std;

inline void fillArray( char input[]){ cin.getline( input,21 ); }
inline char toChar(int value)
{ 
	if(value>=0 && value <=9)
		return (char) (value + 48); 
	else
		return (char) value;
}
 inline int toInt(char value){ return (int) value - 48; }


void addArrays(char[] ,char[]);

int main(void)
{
	char input1[20];
	char input2[20];
		
	fillArray(input1);
	cin.clear();
	fillArray(input2);
	
	//this section always sends the largest sized of the 2 first.
	if(strlen(input2) < strlen(input1))
		addArrays(input1,input2);
	else 
		addArrays(input2,input1);

	return 0;
}

//the largest array is input1, while the smallest is input2.
void addArrays(char input1[], char input2[])
{
	int min = 0;
	int max = 0; 
	int diff = 0;
	bool carryVal = false;
	char answer[20];
	
	
	if ( strlen(input1) != strlen(input2) )
	{
		if(strlen(input2) < strlen(input1))
		{
			max = strlen(input1);
			min = strlen(input2);
		}
		else
		{
			max = strlen(input2);
			min = strlen(input1);
		}
		diff = max - min;
	}
	else
		min = max = strlen(input1);

	cout << "min: " << min << " max: " << max << " diff: " << diff << endl;

	if (diff == 0);
	{
		for( int x = max - 1 ; x > -1; x--)
		{
			int val = toInt(input1[x]) + toInt(input2[x]);
			cout << x << endl;
			if( carryVal == true )
			{
				val += 1;
				carryVal = false;
			}

			if( val >= 10)
			{
				answer[x] = toChar(val-10);
				carryVal = true;
			}
			else
				answer[x] = toChar(val);
		}
	}
	cout << "1: " << strlen(input1) << endl;
	cout << "2: " << strlen(input2) << endl;

	

	cout << "Carry Value: " << carryVal << endl;
	for (int x=0;x < max;x++)
		cout << x << ": " << answer[x] << "\n " ;
	cout << endl;
	cout << answer << endl;
}