Directory:Derek Elder/Programs/ReadBigInt

MyWikiBiz, Author Your Legacy — Thursday December 26, 2024
Jump to navigationJump to search
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int MAX = 5/*0*/;

struct BigInt
{
	int Digits[MAX];
	int numDigits;
};

bool ReadBigInt(BigInt& num);
//Purpose:Read a BigInt, deposit into a string and convert from characters to numeric data
//Precondition:None.
//Postcondition:1.Array is populated, data is valid.
//              2.BigInt is too large, loops until an Int is entered that is small enough.
void PrintBigInt(const BigInt& num);
//Purpose:Print a BigInt in the same way it was entered by reversing the way it was stored into the array.
//Precondition:BigInt num is valid.
//Postcondition:Data is printed on screen matching the way it was entered from the console.
bool AddBigInts(const BigInt& num1, const BigInt& num2, BigInt& sum);
//Purpose:Add two integers together into a sum.
//Precondition:BigInts num1 and num2 are valid.
//Postcondition:1.Return true if sum is valid.
//				2.Return false if there is overflow.
void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum);
//Purpose:Modularize AddBigInts function to make it more efficent.
//Precondition:BigInts num1 and num2 have been entered and contain good data.
//Postcondition:1.Sum has been populated by good data as long as num.numDigits <= MAX.
//				2.If there is overflow (num.numDigits > MAX) it will be indicated in AddBigInts.

void main()
{
	BigInt x,y,sum;
	bool moreNumbers = true;
	char update = ' ';

	while(moreNumbers)
	{
		ReadBigInt(x);
		ReadBigInt(y);
		cout<<"The BigInt entered is: ";
		PrintBigInt(x);
		cout<<"The BigInt entered is: ";
		PrintBigInt(y);
		AddBigInts(x,y,sum);
		cout<<"The sum is: ";
		PrintBigInt(sum);

		cout<<"Do you want to add more numbers? (Y or N): ";
		cin>>update;
		cin.ignore(50,'\n');
		cout<<"==========================================="<<endl;

		if(update == 'Y' || update == 'y')
		{
			moreNumbers = true;
		}
		else
		{
			moreNumbers = false;
		}
	}
	cout<<"Program Terminated on request."<<endl;
}

bool ReadBigInt(BigInt& num)
{
	int len = 0;
	char ch = ' ';
	int counter = 0;
	bool tooBig = false;
	string str = "";

	while(!tooBig) //Initialize tooBig, if integer is too big loop until BigInt entered is valid.
	{
		cout<<"Enter an integer: ";
		getline(cin,str);

		len = (int)str.length(); //Length of the string entered into len

		if (len > MAX)
		{
			cout<<"The number you entered is too big, try again!"<<endl;
			tooBig = false;
		}
		else
		{	
			num.numDigits = len;

			for(int i = len-1; i >= 0; i--)             //Reverse the string and position of numbers
			{
				ch = str[i];                        //Convert from char to numeric value
				num.Digits[counter] = ch - '0';     //char c = '2';
				counter++;			    //int n = c - '0';
			}
			tooBig = true;
		}
	}
	return true;
}

void PrintBigInt(const BigInt& num)
{
	int counter = num.numDigits - 1;

	if(num.numDigits > MAX)
	{
		cout<<"OVERFLOW";
	}
	else
	{
		for(int i = 0; i < num.numDigits; i++)
		{
			cout<<num.Digits[counter];
			counter--;
		}
	}
	cout<<endl;
}

bool AddBigInts(const BigInt& num1, const BigInt& num2, BigInt& sum)
{
	if(num1.numDigits >= num2.numDigits)
	{
		firstIntegerLarger(num1, num2, sum);
	}
	else
	{
		firstIntegerLarger(num2, num1, sum);
	}

	//If Overflow return false, otherwise return true
	if(sum.numDigits > MAX)
	{
		return false;
	}
	else
	{
		return true;
	}
}

void firstIntegerLarger(const BigInt& largest, const BigInt& smallest, BigInt& sum)
{
	int i;
	int remainder = 0;

	for(i = 0; i < smallest.numDigits; i++)
	{
		sum.Digits[i] = largest.Digits[i] + smallest.Digits[i] + remainder;

		if(sum.Digits[i] >= 10)
		{
			sum.Digits[i] = (sum.Digits[i] - 10);
			remainder = 1;
		}
		else
		{
			remainder = 0;
		}
	}

	for(; i < largest.numDigits; i++)
	{
		sum.Digits[i] = largest.Digits[i] + remainder;

		if(sum.Digits[i] >= 10)
		{
			sum.Digits[i] = (sum.Digits[i] - 10);
			remainder = 1;
		}
		else
		{
			remainder = 0;
		}	
	}

	if(i == largest.numDigits && remainder == 1)
	{
		sum.Digits[i] = 1;
		sum.numDigits = largest.numDigits + 1;
	}
	else
	{
		sum.numDigits = largest.numDigits;
	}
}