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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
const int MAX_DIGITS = 100;
//prototypes
void inputVLI(int vli[], int& size);
void outputVLI(int vli[], int size);
void addVLI(int vli1[], int vli2[], int vli3[],
int size1, int size2, int& size3);
int compVLI (int vli1[], int vli2[],
int size1, int size2);
/*
Write a main function that:
Declares three VLI: vli1, vli2 and vli3
Enters values for two VLIs : vli1 and vli2 (using inputVLI function)
Outputs vli1 and vli2 (using outputVLI function)
Calculates vli3 (vli3 = vli1 + vli2, using addVLI function)
Compares vli1 and vli3 (using compVLI function)
*/
//main function
void main()
{
int vli1[MAX_DIGITS], vli2[MAX_DIGITS], vli3[MAX_DIGITS+1]; //+1 is for a possible carried digit
int size1, size2, size3;
cout << "Step 1: INPUT\n"
<< "------------------------------------------------------------------\n";
cout << "A very long integer is a number of up to 100 decimal digits. \n"
<< "Please enter a very long integer: \n";
inputVLI(vli1, size1);
cout << endl << "Please enter a second very long integer: \n";
inputVLI(vli2, size2);
cout << "Step 2: OUTPUT\n"
<< "------------------------------------------------------------------\n";
outputVLI(vli1, size1);
cout << endl;
outputVLI(vli2, size2);
cout << endl;
cout << "Step 3: ADDITION\n"
<< "-------------------------------------------------------------------\n";
addVLI(vli1,vli2,vli3,size1,size2,size3);
outputVLI(vli3,size3);
}
//Function definitions
void inputVLI(int vli[], int& size)
{
string vli1_string;
bool no_error = true;
do //enters the VLI as a string
{
cin >> vli1_string;
cout << endl;
size = vli1_string.length(); //claculates the size using .length
for (int number = 0; number < size ; number++)
{
if (!isdigit((char)vli1_string[number])) //checks if string is entirely numbers
{
cout << "Your input was not all numbers. Input must be only numbers! \n\n"
<< "Try again...\n";
no_error = false;
break;
}
else if (size >= MAX_DIGITS) //verifies that string is within max digits
{
cout << "Your input is over 100 digits long!\n"
<< "Please enter a number with less than 100 digits... \n\n";
no_error = false;
break;
}
else // reassigns each character as an integer value
{
//takes each character of the array and converts it to an integer,
//by subtracting the char '0'.
no_error = true;
vli[number] = vli1_string[number] - '0';
}
}
}
while (no_error == false); //keeps re-running until there is no errors...
}
void outputVLI(int vli[], int size)
{
//simply takes each value of the array and outputs it with a space behind it.
for(int number = 0; number < size; number++)
{
cout << vli[number] << " ";
}
cout << endl;
}
void addVLI(int vli1[], int vli2[], int vli3[],
int size1, int size2, int& size3)
{
int vli1_int, vli2_int; //uses the integer changed arrays
int carried = 0; //realized I needed a way to figure out the carried numbers
//Determines which of the two numbers is bigger, to make the size of VLI3.
if(size1 > size2)
{
size3 = size1;
}
else
{
size3 = size2;
}
//cycles through each index, until it reaches the size of vli3.
for(int number = 0; number <= size3; number++)
{
if (size1 - number >= 0)
vli1_int = vli1[size1 - number];
else
vli1_int = 0;
//takes the numbers from right to left, unless there are none, then defaults to zero
//this is to avoid an error with unknown array values
if (size2 - number >= 0)
vli2_int = vli2[size2 - number];
else
vli1_int = 0;
//also takes the numbers from right to left, unless none exist, then defaults to zero
//this is to avoid an error with unknown array values
//States that if the value for the cell is over 9, it should be split into stored and carried digit.
if (vli1_int + vli2_int + carried > 9)
{
vli3[size3 - number]= (vli1_int + vli2_int + carried)%10;
carried = (vli1_int + vli2_int + carried)/10;
}
else
{
vli3[size3 - number]= (vli1_int + vli2_int + carried);
carried = 0;
}
//If final number is over 9, it expands the array, and then shifts all values to the right.
//It then places the carried digit in the new space created.
if (vli3[0] > 9)
{
++size3;
for (int number = 0; number <= size3; number++)
vli3[size3 - number] = vli3[size3 - number + 1];
vli3[0]= 1;
vli3[1]= vli3[1]%10;
}
}
}
int compVLI (int vli1[], int vli2[],
int size1, int size2)
{
return 0;
}
|