1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
//Michael Ervin - ch9 Problem 11
//No validation, so enter only integers.
//delete [] varName will not work for some reason. need to fix
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void add(int arr1[], int arr2[], int sum[], int length, bool zerolen);
int main()
{
//declare all variables
int i;
int *num1, *num2, *temp1, *temp2, *sum;
bool zerolen = false;
char ch;
string inputNumber;
string::size_type len1;
string::size_type len2;
cout << "-------------------------------------------\n" <<
"This program allows you to and and numbers\n" <<
"larger than 2147483647 with the help of\n" <<
"*arrays and functions. The dynamic arrays\n" <<
"allow for you to add numbers with as many\n" <<
"digits as your computers memory allows\n" <<
"-------------------------------------------\n" << endl;
do
{
cout << "Enter the first number >> ";
//get user input
getline(cin, inputNumber);
len1 = inputNumber.length();
num1 = new int[len1]; //initialize array
//read in user input backwards
for (i = (len1 - 1); i >= 0; i--)
num1[i] = (static_cast<int>(inputNumber[(len1 - 1) - i]) - 48);
cout << "Enter the second number >> ";
//get user input
getline(cin, inputNumber);
len2 = inputNumber.length();
num2 = new int[len2];//initialize array
//read in user input backwards
for (i = (len2 - 1); i >= 0; i--)
num2[i] = (static_cast<int>(inputNumber[(len2 - 1) - i]) - 48);
cout << endl;
//determine size discrepancies, fill trailing space with 0's
if (len1 > len2)
{
sum = new int[len1];
temp2 = new int[len1];
memcpy(temp2, num2, (sizeof(int) * inputNumber.size()));
for (i = len2; i <= (len1 - 1); i++)
temp2[i] = 0;
add(num1, temp2, sum, len1, zerolen);
}
else if (len2 > len1)
{
sum = new int[len2];
temp1 = new int[len2];
memcpy(temp1, num1, (sizeof(int) * inputNumber.size()));
for (i = len1; i <= (len2 - 1); i++)
temp1[i] = 0;
add(temp1, num2, sum, len2, zerolen);
}
else if (len1 == len2)
{
zerolen = true;
sum = new int[len1 + 1];
sum[0] = 0; sum[1] = 0;
add(num1, num2, sum, len1, zerolen);
}
system("PAUSE");
cout << "Would you like to add another number(y/n)? ";
cin >> ch; system("CLS"); cin.clear(); cin.ignore(100, '\n');
}
while (ch =='y' || ch == 'Y');
return 0;
}
//function to add the digits of the two user input numbers and output the sum
void add(int arr1[],int arr2[], int sum[], int length, bool zerolen)
{
int i, x = 0, carryOver = 0;
length--;
if (zerolen == true)
if (length < 1)
x = 1;
else
x = 0;
for (i = 0; i <= length; i++)
{
if (arr1[i] + arr2[i] >= 10)
{
sum[length + x - i] = ((arr1[i] + arr2[i]) % 10) + carryOver;
carryOver = 1;
}
else
{
sum[length + x - i] = (arr1[i] + arr2[i]) + carryOver;
carryOver = 0;
}
if (zerolen == true)
sum[0] += carryOver;
}
cout << "The sum of the numbers is: ";
for (i = 0; i <= length + x; i++)
cout << sum[i];
cout << endl << endl;
}
|