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
|
#include <iostream>
using namespace std;
#include "HugeInteger.h"
HugeInteger::HugeInteger()
{
op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
op2[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
result[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//for (int i = 0; i <= MAX2; i++)
//{
// op1[i] = 0;
//}
}
//HugeInteger HugeInteger::operator+(const HugeInteger& c) const
//{
// HugeInteger Result;
// Result.op1 = Result.add(this->op1, c.op2, c.result);
// return Result;
//}
HugeInteger HugeInteger::add(const int HugeInteger &)
{
HugeInteger object;
int carry;
for (int i = MAX2; i >= 0; i--)
{
object.result[i] = object.op1[i] + object.op2[i] + carry;
carry = 0;
while (result[i] >= 10)
{
object.op1[i] -= 10;
carry++;
}
}
}
HugeInteger HugeInteger:: mult(const int HugeInteger &)
{
int mult_array[MAX] = {0};
int addzero_ctr;
int lastDigit;
int firstDigit;
const int num = 10;
int product_holder;
for (int m = (MAX2); m >= 0; m--) // This 'for' loop essentially sums together the results of the below 'for' loop to carry out the multiplication
{
for (int i = (MAX2); i >= 0; i--) // This 'for' loop multiplies all of op1 by the last digit of op2 and puts the value into result[i]
{
product_holder = op1[i] * op2[m];
object.op1[i] = product_holder;
lastDigit = product_holder % num;
object.result[i] = firstDigit;
product_holder -= lastDigit;
firstDigit = product_holder / num;
object.result[i] += lastDigit;
}
if (addzero_ctr != 0) // solution to below
{
for (int i = 0; i <= (MAX2 - addzero_ctr); i++)//This is a lot of overhead when addzero_ctr = 0
{
object.op1[i] = object.result[i+addzero_ctr];
}
}
for (int i = addzero_ctr; i > 0; i--) // Since we need to carry a zero during multiplication, the contents of the array must shift to the left to make room for the zeroes
{
object.result[MAX-i] = 0;
}
// cout << "------------------\n"; // Use this comment section to check which two numbers the system adds
// cout << "These are the two numbers being added: \n";
// for (int i = 0; i <= MAX2; i++)
// {
// cout << mult_array[i];
// }
// cout << " + ";
// for (int i = 0; i <= MAX2; i++)
// {
// cout << result[i];
// }
// cout << endl;
add(object);
addzero_ctr++;
for (int i = 0; i <= MAX2; i++)
{
mult_array[i] = object.op1[i];
// cout << "mult[i] = " << mult_array[i] << endl; // Use this comment section to check what result[i] is immediately before m is decremented
}
}
}
void HugeInteger::display() // Displays result of array as a single integer
{
bool eraseZeroes = true;
cout << "Answer is: ";
for (int i = 0; i <= MAX2; i++)
{
if (result[i] != 0 || eraseZeroes == false)
{
cout << result[i];
eraseZeroes = false; // The boolean variable eraseZeroes is used to erase superflous zeroes (like 0009 instead of 9)
}
}
if (eraseZeroes) // If the user multiplies by zero (or adds zero and zero), we need to cout a zero.
{
cout << 0;
}
cout << endl;
}
//void HugeInteger::create_array(int num1, int num2, int op1[], int op2[]) // Converts int into an array of 40 digits
//{
// for (int i = MAX2; i >= 0; i--)
// {
// lastDigit = (num1 % num);
// op1[i] = lastDigit;
// num1 -= lastDigit;
// num1 = (num1 / num);
// }
// lastDigit = 0;
// for (int i = MAX2; i >= 0; i--)
// {
// lastDigit = (num2 % num);
// op2[i] = lastDigit;
// num2 -= lastDigit;
// num2 = (num2 / num);
// }
//}
|