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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
#include <iostream>
#include <algorithm>
#include <cmath>
#include "BigInteger.hpp"
// ------------------------------------------------------------------
//
// Returns the digit as the specified positon. If the position is greater
// that the number representation, a 0 is returned.
//
// ------------------------------------------------------------------
std::uint8_t BigInteger::getDigit(unsigned int position) const
{
if (position < m_digitCount)
{
return m_number[position];
}
return 0;
}
void BigInteger::setDigit(unsigned int position, uint8_t digit)
{
if (position < m_digitCount)
{
m_number[position] = digit;
}
else
{
if (position + 1 < m_sizeReserved)
{
m_digitCount = position + 1;
m_number[position] = digit;
}
else
{
while (m_sizeReserved <= position)
{
m_sizeReserved *= 2;
}
uint8_t* copy_m_number = new uint8_t[m_sizeReserved];
m_digitCount = position + 1;
for (unsigned int i = 0; i < m_digitCount; ++i)
{
if (m_number[i] < 10)
{
copy_m_number[i] = m_number[i];
m_number[i] = NULL;
}
else
{
copy_m_number[i] = 0;
}
}
delete[] m_number;
m_number = new uint8_t[m_sizeReserved];
for (unsigned int i = 0; i < m_digitCount; ++i)
{
m_number[i] = copy_m_number[i];
copy_m_number[i] = NULL;
}
delete[] copy_m_number;
m_number[position] = digit;
}
}
}
BigInteger BigInteger::operator= (const BigInteger& rOther) // overridden
{
for (unsigned int i = 0; i < m_digitCount; ++i)
{
this->m_number[i] = NULL;
}
this->m_sizeReserved = rOther.m_sizeReserved;
this->m_digitCount = rOther.m_digitCount;
delete[] this->m_number;
this->m_number = new uint8_t[this->m_sizeReserved];
for (unsigned int i = 0; i < m_digitCount; ++i)
{
this->setDigit(i, rOther.m_number[i]);
}
return *this;
}
BigInteger BigInteger::operator+(const BigInteger& rhs)
{
BigInteger result;
unsigned int length = (this->m_digitCount > rhs.m_digitCount) ? this->m_digitCount : rhs.m_digitCount;
result.m_digitCount = length;
while (result.m_digitCount > result.m_sizeReserved)
{
result.m_sizeReserved *= 2;
result.m_number = new std::uint8_t[result.m_sizeReserved];
for (unsigned int i = 0; i < result.m_digitCount; ++i)
{
result.setDigit(i, 0);
}
}
int carry = 0;
for (unsigned int digit = 0; digit < length; digit++)
{
int v1 = this->getDigit(digit);
int v2 = rhs.getDigit(digit);
int sum = v1 + v2 + carry;
int single = sum % 10;
carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;
result.setDigit(digit, single);
}
if (carry > 0)
{
result.setDigit(length, carry);
}
return result;
}
BigInteger BigInteger::operator+(const unsigned int& inboundInt)
{
BigInteger rhs(inboundInt);
BigInteger result;
unsigned int length = (this->m_digitCount > rhs.m_digitCount) ? this->m_digitCount : rhs.m_digitCount;
result.m_digitCount = length;
while (result.m_digitCount > result.m_sizeReserved)
{
result.m_sizeReserved *= 2;
result.m_number = new std::uint8_t[result.m_sizeReserved];
for (unsigned int i = 0; i < result.m_digitCount; ++i)
{
result.setDigit(i, 0);
}
}
int carry = 0;
for (unsigned int digit = 0; digit < length; digit++)
{
int v1 = this->getDigit(digit);
int v2 = rhs.getDigit(digit);
int sum = v1 + v2 + carry;
int single = sum % 10;
carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;
result.setDigit(digit, single);
}
if (carry > 0)
{
result.setDigit(length, carry);
}
return result;
}
BigInteger BigInteger::operator+=(const BigInteger& rhs)
{
BigInteger result = *this + rhs;
*this = result;
return *this;
}
BigInteger BigInteger::operator++(int)
{
BigInteger temp = *this;
BigInteger postfix(1);
*this += postfix;
return temp;
}
BigInteger BigInteger::operator*(const BigInteger& rhs)
{
BigInteger result;
const BigInteger& b = *this;
const BigInteger& t = rhs;
for (unsigned int bDigit = 0; bDigit < b.m_digitCount; bDigit++)
{
BigInteger temp(0);
int v1 = b.getDigit(bDigit);
int carry = 0;
for (unsigned int tDigit = 0; tDigit < t.m_digitCount; tDigit++)
{
int v2 = t.getDigit(tDigit);
int sum = v1 * v2 + carry;
int single = sum % 10;
carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;
temp.setDigit(bDigit + tDigit, single);
}
if (carry > 0)
{
temp.setDigit(bDigit + t.m_digitCount, carry);
}
result += temp;
}
return result;
}
BigInteger BigInteger::operator*=(const BigInteger& rhs)
{
BigInteger result = *this * rhs;
*this = result;
return *this;
}
BigInteger::operator double()
{
double result = 0;
for (unsigned int i = 0; i < this->m_digitCount; ++i)
{
result += (this->m_number[i] * pow(10, i));
}
return result;
}
bool BigInteger::operator<=(const BigInteger& rhs)
{
if (this->m_digitCount < rhs.m_digitCount) return true;
if (this->m_digitCount > rhs.m_digitCount) return false;
// If m_digitCount for both are equal check digit by digit
for (int digit = m_digitCount - 1; digit >= 0; digit--)
{
if (this->m_number[digit] < rhs.m_number[digit]) return true;
if (this->m_number[digit] > rhs.m_number[digit]) return false;
}
//if they are equal, return true
return true;
}
bool BigInteger::operator==(const BigInteger& rhs)
{
if (this->m_digitCount < rhs.m_digitCount) return false;
if (this->m_digitCount > rhs.m_digitCount) return false;
// If m_digitCount for both are equal check digit by digit
for (int digit = m_digitCount - 1; digit >= 0; digit--)
{
if (this->m_number[digit] < rhs.m_number[digit]) return false;
if (this->m_number[digit] > rhs.m_number[digit]) return false;
}
//if they are equal, return true
return true;
}
std::ostream& operator<<(std::ostream& os, const BigInteger rhs)
{
for (unsigned int digit = rhs.m_digitCount; digit > 0; digit--)
{
std::cout << static_cast<int>(rhs.m_number[digit - 1]);
}
return os;
}
|