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
|
#include <iostream>
#include <string>
using namespace std;
class BigInt
{
friend BigInt operator+(const BigInt& num1, const BigInt& num2);
friend ostream& operator<<(ostream& os, const BigInt& num);
friend bool AddBigInt(const BigInt& Num1, const BigInt& Num2, BigInt& Sum);
friend bool AddWithLongestFirst(const BigInt& longer, const BigInt& shorter, BigInt& sum );
public:
static const int MAX_DIGITS = 5; // "nested" and "public" instead of "Global"
BigInt(); // set numDigits to 0 -- this means the BigInt is 0
BigInt(const string& goodString); // assume "goodString" is perfect - no blanks or non-digits
bool ReadBigInt(istream& is); // default stream arg; ReadBigInt() means use "cin"
void PrintBigInt( ostream& os) const;
bool StringToBigInt(const string& goodString, const BigInt& num);
// ExtractGoodString is "optional"
//bool ExtractGoodString( const string& input, string& output);
private:
int Digits[MAX_DIGITS];
int numDigit;
};
BigInt::BigInt():numDigit(0)
{
}
BigInt::BigInt(const string& goodString)
{
numDigit=goodString.length();
if(numDigit>BigInt::MAX_DIGITS)
return false;
int j=0;
for(int i=numDigit-1;i>=0;i--)
{
Digits[j]=goodString[i]-'0';
j++;
}
}
bool BigInt::ReadBigInt(istream& is)
{
string goodString;
BigInt num;
is>>goodString;
StringToBigInt(goodString,num);
return true;
}
bool StringToBigInt(const string& goodString, BigInt& num)
{
numDigit=goodString.length();
if(numDigit>BigInt::MAX_DIGITS)
return false;
int j=0;
for(int i=numDigit-1;i>=0;i--)
{
Digits[j]=goodString[i]-'0';
j++;
}
}
void BigInt::PrintBigInt(ostream& os) const
{
for(int i=numDigit-1;i>=0;i--)
os<<Digits[i];
os<<endl;
}
bool AddBigInt(const BigInt& num1, const BigInt& num2, BigInt& sum)
{
if(num1.numDigit>num2.numDigit)
return AddWithLongestFirst(num1,num2,sum);
else
return AddWithLongestFirst(num2,num1,sum);
}
bool AddWithLongestFirst(const BigInt& Long, const BigInt& Short, BigInt& Sum)
{
int carry=0;
int temp;
for(int i=0; i<Short.numDigit; i++)
{
temp=Long.Digits[i]+Short.Digits[i]+carry;
if(temp>=10)
{
Sum.Digits[i]= temp-10;
carry=1;
}
else
{
carry=0;
Sum.Digits[i]=temp;
}
}
for(int j=Short.numDigit; j<Long.numDigit; j++)
{
temp=Long.Digits[j]+carry;
if(temp>=10)
{
Sum.Digits[j]=temp-10;
carry=1;
}
else
{
Sum.Digits[j]=temp;
carry=0;
}
}
if(carry==1 && Long.numDigit < BigInt::MAX_DIGITS)
{
Sum.Digits[Long.numDigit]=1;
Sum.numDigit=Long.numDigit+1;
return true;
}
else if(Long.numDigit <= BigInt::MAX_DIGITS && carry==0)
{
Sum.numDigit=Long.numDigit;
return true;
}
else
return false;
}
BigInt operator +(const BigInt& num1, const BigInt& num2)
{
BigInt Sum;
if(AddBigInt(num1,num2,Sum))
return Sum;
else
return 0;
}
void main()
{
}
|